Skip to content

Commit acad25b

Browse files
committed
chore(shared): Migrate tests to vitest
1 parent 160dc93 commit acad25b

38 files changed

+291
-750
lines changed

packages/shared/customJSDOMEnvironment.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

packages/shared/jest.config.js

Lines changed: 0 additions & 28 deletions
This file was deleted.

packages/shared/jest.setup.ts

Lines changed: 0 additions & 45 deletions
This file was deleted.

packages/shared/package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,9 @@
148148
"lint:attw": "attw --pack . --profile node16",
149149
"lint:publint": "publint",
150150
"publish:local": "pnpm yalc push --replace --sig",
151-
"test": "jest && vitest",
152-
"test:cache:clear": "jest --clearCache --useStderr",
153-
"test:ci": "jest --maxWorkers=70%",
154-
"test:coverage": "jest --collectCoverage && open coverage/lcov-report/index.html"
151+
"test": "vitest",
152+
"test:ci": "vitest --maxWorkers=70%",
153+
"test:coverage": "vitest --collectCoverage && open coverage/lcov-report/index.html"
155154
},
156155
"dependencies": {
157156
"@clerk/types": "workspace:^",

packages/shared/src/__tests__/apiUrlFromPublishableKey.test.ts renamed to packages/shared/src/__tests__/apiUrlFromPublishableKey.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { describe, expect, test } from 'vitest';
2+
13
import { apiUrlFromPublishableKey } from '../apiUrlFromPublishableKey';
24

35
describe('apiUrlFromPublishableKey', () => {

packages/shared/src/__tests__/browser.test.ts renamed to packages/shared/src/__tests__/browser.spec.ts

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
2+
13
import { inBrowser, isValidBrowser, isValidBrowserOnline, userAgentIsRobot } from '../browser';
24

35
describe('inBrowser()', () => {
46
afterEach(() => {
5-
jest.restoreAllMocks();
7+
vi.restoreAllMocks();
68
});
79

810
it('returns true if window is defined', () => {
911
expect(inBrowser()).toBe(true);
1012
});
1113
it('returns false if window is undefined', () => {
12-
const windowSpy = jest.spyOn(global, 'window', 'get');
14+
const windowSpy = vi.spyOn(global, 'window', 'get');
1315
// @ts-ignore - Test
1416
windowSpy.mockReturnValue(undefined);
1517
expect(inBrowser()).toBe(false);
@@ -21,16 +23,23 @@ describe('isValidBrowser', () => {
2123
let webdriverGetter: any;
2224

2325
beforeEach(() => {
24-
userAgentGetter = jest.spyOn(window.navigator, 'userAgent', 'get');
25-
webdriverGetter = jest.spyOn(window.navigator, 'webdriver', 'get');
26+
userAgentGetter = vi.spyOn(window.navigator, 'userAgent', 'get');
27+
// Define webdriver property if it doesn't exist
28+
if (!('webdriver' in window.navigator)) {
29+
Object.defineProperty(window.navigator, 'webdriver', {
30+
configurable: true,
31+
get: () => false,
32+
});
33+
}
34+
webdriverGetter = vi.spyOn(window.navigator, 'webdriver', 'get');
2635
});
2736

2837
afterEach(() => {
29-
jest.restoreAllMocks();
38+
vi.restoreAllMocks();
3039
});
3140

3241
it('returns false if not in browser', () => {
33-
const windowSpy = jest.spyOn(global, 'window', 'get');
42+
const windowSpy = vi.spyOn(global, 'window', 'get');
3443
// @ts-ignore - Test
3544
windowSpy.mockReturnValue(undefined);
3645

@@ -99,11 +108,27 @@ describe('isValidBrowserOnline', () => {
99108
let connectionGetter: any;
100109

101110
beforeEach(() => {
102-
userAgentGetter = jest.spyOn(window.navigator, 'userAgent', 'get');
103-
webdriverGetter = jest.spyOn(window.navigator, 'webdriver', 'get');
104-
onLineGetter = jest.spyOn(window.navigator, 'onLine', 'get');
111+
userAgentGetter = vi.spyOn(window.navigator, 'userAgent', 'get');
112+
// Define webdriver property if it doesn't exist
113+
if (!('webdriver' in window.navigator)) {
114+
Object.defineProperty(window.navigator, 'webdriver', {
115+
configurable: true,
116+
get: () => false,
117+
});
118+
}
119+
webdriverGetter = vi.spyOn(window.navigator, 'webdriver', 'get');
120+
onLineGetter = vi.spyOn(window.navigator, 'onLine', 'get');
121+
// Define connection property if it doesn't exist
122+
// @ts-ignore
123+
if (!('connection' in window.navigator)) {
124+
// @ts-ignore
125+
Object.defineProperty(window.navigator, 'connection', {
126+
configurable: true,
127+
get: () => ({ downlink: 10, rtt: 100 }),
128+
});
129+
}
105130
// @ts-ignore
106-
connectionGetter = jest.spyOn(window.navigator, 'connection', 'get');
131+
connectionGetter = vi.spyOn(window.navigator, 'connection', 'get');
107132
});
108133

109134
it('returns TRUE if connection is online, navigator is online, has disabled webdriver, and not a bot', () => {

packages/shared/src/__tests__/buildAccountsBaseUrl.test.ts renamed to packages/shared/src/__tests__/buildAccountsBaseUrl.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { expect, test } from 'vitest';
2+
13
import { buildAccountsBaseUrl } from '../buildAccountsBaseUrl';
24

35
test.each([
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { describe, expect, it } from 'vitest';
2+
3+
import type { Color } from '@clerk/types';
4+
5+
import { colorToSameTypeString, hexStringToRgbaColor, stringToHslaColor, stringToSameTypeColor } from '../color';
6+
7+
describe('stringToHslaColor(color)', function () {
8+
const hsla = { h: 195, s: 1, l: 0.5 };
9+
const cases: Array<[string, Color | null]> = [
10+
['', null],
11+
['transparent', { h: 0, s: 0, l: 0, a: 0 }],
12+
['#00bfff', hsla],
13+
['00bfff', hsla],
14+
['rgb(0, 191, 255)', hsla],
15+
['rgba(0, 191, 255, 0.3)', { ...hsla, a: 0.3 }],
16+
];
17+
18+
it.each(cases)('.stringToHslaColor(%s) => %s', (a, expected) => {
19+
expect(stringToHslaColor(a)).toEqual(expected);
20+
});
21+
});
22+
23+
describe('hexStringToRgbaColor(color)', function () {
24+
const cases: Array<[string, Color | null]> = [
25+
['#00bfff', { r: 0, g: 191, b: 255 }],
26+
['00bfff', { r: 0, g: 191, b: 255 }],
27+
];
28+
29+
it.each(cases)('.hexStringToRgbaColor(%s) => %s', (a, expected) => {
30+
expect(hexStringToRgbaColor(a)).toEqual(expected);
31+
});
32+
});
33+
34+
describe('stringToSameTypeColor(color)', function () {
35+
const cases: Array<[string, Color | null]> = [
36+
['', ''],
37+
['invalid', ''],
38+
['12ff12', '#12ff12'],
39+
['#12ff12', '#12ff12'],
40+
['1ff', '#1ff'],
41+
['transparent', 'transparent'],
42+
['rgb(100,100,100)', { r: 100, g: 100, b: 100, a: undefined }],
43+
['rgba(100,100,100,0.5)', { r: 100, g: 100, b: 100, a: 0.5 }],
44+
['rgb(100,100,100)', { r: 100, g: 100, b: 100, a: undefined }],
45+
['rgba(100,100,100,0.5)', { r: 100, g: 100, b: 100, a: 0.5 }],
46+
['hsl(244,66%,33%)', { h: 244, s: 0.66, l: 0.33, a: undefined }],
47+
['hsla(244,66%,33%,0.5)', { h: 244, s: 0.66, l: 0.33, a: 0.5 }],
48+
['hsl(244,66%,33)', ''],
49+
['hsla(244,66%,33,0.5)', ''],
50+
];
51+
52+
it.each(cases)('.stringToSameTypeColor(%s) => %s', (a, expected) => {
53+
expect(stringToSameTypeColor(a)).toEqual(expected);
54+
});
55+
});
56+
57+
describe('colorToSameTypeString(color)', function () {
58+
const cases: Array<[Color, string]> = [
59+
['', ''],
60+
['invalid', ''],
61+
['#12ff12', '#12ff12'],
62+
['#12ff12', '#12ff12'],
63+
['#1ff', '#1ff'],
64+
[{ r: 100, g: 100, b: 100, a: undefined }, 'rgb(100,100,100)'],
65+
[{ r: 100, g: 100, b: 100, a: 0.5 }, 'rgba(100,100,100,0.5)'],
66+
[{ h: 100, s: 0.55, l: 0.33, a: undefined }, 'hsl(100,55%,33%)'],
67+
[{ h: 100, s: 1, l: 1, a: 0.5 }, 'hsla(100,100%,100%,0.5)'],
68+
];
69+
70+
it.each(cases)('.colorToSameTypeString(%s) => %s', (a, expected) => {
71+
expect(colorToSameTypeString(a)).toEqual(expected);
72+
});
73+
});
File renamed without changes.

packages/shared/src/__tests__/date.test.ts renamed to packages/shared/src/__tests__/date.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { describe, expect, it } from 'vitest';
2+
13
import type { RelativeDateCase } from '../date';
24
import { addYears, dateTo12HourTime, differenceInCalendarDays, formatRelative } from '../date';
35

0 commit comments

Comments
 (0)