Skip to content

Commit 2e092e9

Browse files
committed
Replace Happy DOM with Vitest Browser Mode
1 parent b9b079e commit 2e092e9

File tree

10 files changed

+185
-124
lines changed

10 files changed

+185
-124
lines changed

.github/workflows/ci.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ jobs:
5252

5353
- name: Install dependencies
5454
run: yarn --immutable
55+
env:
56+
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: true
5557

5658
- name: Build package
5759
run: yarn build
@@ -91,6 +93,15 @@ jobs:
9193
restore-keys: |
9294
${{ runner.os }}-${{ env.cache-name }}
9395
96+
- name: Cache ~/.cache/ms-playwright
97+
id: playwright-cache
98+
uses: actions/cache@v4
99+
env:
100+
cache-name: playwright-cache
101+
with:
102+
path: ~/.cache/ms-playwright
103+
key: ${{ runner.os }}-${{ env.cache-name }}-${{ hashFiles('**/yarn.lock') }}
104+
94105
- name: Use Node.js
95106
uses: actions/setup-node@v4
96107
with:
@@ -102,5 +113,9 @@ jobs:
102113
- name: Install dependencies
103114
run: yarn --immutable
104115

116+
- name: Install Playwright browsers
117+
if: steps.playwright-cache.outputs.cache-hit != 'true'
118+
run: yarn workspace react-time-picker playwright install chromium-headless-shell
119+
105120
- name: Run tests
106121
run: yarn unit

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
!**/.yarn/versions
1919

2020
# Project-generated directories and files
21+
__screenshots__
2122
coverage
2223
dist
2324
node_modules

.yarnrc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
enableScripts: false
2+
13
logFilters:
24
- code: YN0076
35
level: discard

packages/react-time-picker/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,17 +117,17 @@
117117
"@testing-library/dom": "^10.0.0",
118118
"@testing-library/jest-dom": "^6.0.0",
119119
"@testing-library/react": "^16.0.0",
120-
"@testing-library/user-event": "^14.5.0",
121120
"@types/node": "*",
122121
"@types/react": "*",
123122
"@types/react-dom": "*",
123+
"@vitest/browser": "^3.2.3",
124124
"cpy-cli": "^5.0.0",
125-
"happy-dom": "^15.10.2",
125+
"playwright": "^1.51.1",
126126
"react": "^18.2.0",
127127
"react-dom": "^18.2.0",
128128
"typescript": "^5.5.2",
129129
"vitest": "^3.2.3",
130-
"vitest-canvas-mock": "^0.2.2"
130+
"vitest-browser-react": "^1.0.1"
131131
},
132132
"peerDependencies": {
133133
"@types/react": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0",

packages/react-time-picker/src/TimeInput.spec.tsx

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { beforeEach, describe, expect, it, vi } from 'vitest';
1+
import { userEvent } from '@vitest/browser/context';
2+
import { describe, expect, it, vi } from 'vitest';
23
import { fireEvent, render } from '@testing-library/react';
3-
import { userEvent } from '@testing-library/user-event';
44

55
import TimeInput from './TimeInput.js';
66

@@ -25,13 +25,6 @@ describe('TimeInput', () => {
2525
className: 'react-time-picker__inputGroup',
2626
};
2727

28-
let user: ReturnType<typeof userEvent.setup>;
29-
beforeEach(() => {
30-
user = userEvent.setup({
31-
advanceTimers: vi.advanceTimersByTime.bind(vi),
32-
});
33-
});
34-
3528
it('renders a native input and custom inputs', () => {
3629
const { container } = render(<TimeInput {...defaultProps} />);
3730

@@ -316,7 +309,7 @@ describe('TimeInput', () => {
316309
const hourInput = customInputs[0] as HTMLInputElement;
317310
const minuteInput = customInputs[1];
318311

319-
await user.type(hourInput, '{arrowright}');
312+
await userEvent.type(hourInput, '{arrowright}');
320313

321314
expect(minuteInput).toHaveFocus();
322315
});
@@ -333,7 +326,7 @@ describe('TimeInput', () => {
333326
) as HTMLSpanElement;
334327
const separatorKey = separator.textContent as string;
335328

336-
await user.type(hourInput, separatorKey);
329+
await userEvent.type(hourInput, separatorKey);
337330

338331
expect(minuteInput).toHaveFocus();
339332
});
@@ -344,7 +337,7 @@ describe('TimeInput', () => {
344337

345338
const select = container.querySelector('select') as HTMLSelectElement;
346339

347-
await user.type(select, '{arrowright}');
340+
await userEvent.type(select, '{arrowright}');
348341

349342
expect(select).toHaveFocus();
350343
});
@@ -356,7 +349,7 @@ describe('TimeInput', () => {
356349
const hourInput = customInputs[0];
357350
const minuteInput = customInputs[1] as HTMLInputElement;
358351

359-
await user.type(minuteInput, '{arrowleft}');
352+
await userEvent.type(minuteInput, '{arrowleft}');
360353

361354
expect(hourInput).toHaveFocus();
362355
});
@@ -367,7 +360,7 @@ describe('TimeInput', () => {
367360
const customInputs = container.querySelectorAll('input[data-input]');
368361
const hourInput = customInputs[0] as HTMLInputElement;
369362

370-
await user.type(hourInput, '{arrowleft}');
363+
await userEvent.type(hourInput, '{arrowleft}');
371364

372365
expect(hourInput).toHaveFocus();
373366
});
@@ -379,7 +372,7 @@ describe('TimeInput', () => {
379372
const hourInput = customInputs[0] as HTMLInputElement;
380373
const minuteInput = customInputs[1];
381374

382-
await user.type(hourInput, '4');
375+
await userEvent.type(hourInput, '4');
383376

384377
expect(minuteInput).toHaveFocus();
385378
});
@@ -391,7 +384,7 @@ describe('TimeInput', () => {
391384
const hourInput = customInputs[0] as HTMLInputElement;
392385
const minuteInput = customInputs[1];
393386

394-
await user.type(hourInput, '03');
387+
await userEvent.type(hourInput, '03');
395388

396389
expect(minuteInput).toHaveFocus();
397390
});
@@ -439,7 +432,7 @@ describe('TimeInput', () => {
439432
const customInputs = container.querySelectorAll('input[data-input]');
440433
const hourInput = customInputs[0] as HTMLInputElement;
441434

442-
await user.type(hourInput, '1');
435+
await userEvent.type(hourInput, '1');
443436

444437
expect(hourInput).toHaveFocus();
445438
});

packages/react-time-picker/src/TimePicker.spec.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import { userEvent } from '@vitest/browser/context';
12
import { describe, expect, it, vi } from 'vitest';
23
import { act, fireEvent, render, waitFor, waitForElementToBeRemoved } from '@testing-library/react';
3-
import { userEvent } from '@testing-library/user-event';
44

55
import TimePicker from './TimePicker.js';
66

@@ -410,19 +410,19 @@ describe('TimePicker', () => {
410410
it('closes Clock component when clicked outside', async () => {
411411
const { container } = render(<TimePicker isOpen />);
412412

413-
userEvent.click(document.body);
413+
await userEvent.click(document.body);
414414

415415
await waitForElementToBeRemovedOrHidden(() =>
416416
container.querySelector('.react-time-picker__clock'),
417417
);
418418
});
419419

420-
it('does not close Clock clicked outside with shouldCloseClock function returning false', () => {
420+
it('does not close Clock clicked outside with shouldCloseClock function returning false', async () => {
421421
const shouldCloseClock = () => false;
422422

423423
const { container } = render(<TimePicker isOpen shouldCloseClock={shouldCloseClock} />);
424424

425-
userEvent.click(document.body);
425+
await userEvent.click(document.body);
426426

427427
const clock = container.querySelector('.react-clock');
428428

packages/react-time-picker/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"skipLibCheck": true,
1414
"strict": true,
1515
"target": "es2018",
16+
"types": ["@vitest/browser/matchers"],
1617
"verbatimModuleSyntax": true
1718
},
1819
"exclude": ["dist"]

packages/react-time-picker/vitest.config.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { defineConfig } from 'vitest/config';
22

33
export default defineConfig({
44
test: {
5-
environment: 'happy-dom',
6-
server: {
7-
deps: {
8-
inline: ['vitest-canvas-mock'],
9-
},
5+
browser: {
6+
enabled: true,
7+
headless: true,
8+
instances: [{ browser: 'chromium' }],
9+
provider: 'playwright',
1010
},
1111
setupFiles: 'vitest.setup.ts',
1212
watch: false,

packages/react-time-picker/vitest.setup.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
1-
import { afterEach, beforeEach } from 'vitest';
1+
import { afterEach } from 'vitest';
22
import { cleanup } from '@testing-library/react';
33
import '@testing-library/jest-dom/vitest';
4-
import 'vitest-canvas-mock';
5-
6-
// Workaround for a bug in Vitest 3 or happy-dom
7-
const IntlNumberFormat = Intl.NumberFormat;
8-
9-
beforeEach(() => {
10-
Intl.NumberFormat = IntlNumberFormat;
11-
});
124

135
afterEach(() => {
146
cleanup();

0 commit comments

Comments
 (0)