Skip to content

Commit b0c79fd

Browse files
committed
Replace Happy DOM with Vitest Browser Mode
1 parent 46626b4 commit b0c79fd

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

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

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

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

320-
await user.type(hourInput, '{arrowright}');
313+
await userEvent.type(hourInput, '{arrowright}');
321314

322315
expect(minuteInput).toHaveFocus();
323316
});
@@ -334,7 +327,7 @@ describe('TimeInput', () => {
334327
) as HTMLSpanElement;
335328
const separatorKey = separator.textContent as string;
336329

337-
await user.type(hourInput, separatorKey);
330+
await userEvent.type(hourInput, separatorKey);
338331

339332
expect(minuteInput).toHaveFocus();
340333
});
@@ -345,7 +338,7 @@ describe('TimeInput', () => {
345338

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

348-
await user.type(select, '{arrowright}');
341+
await userEvent.type(select, '{arrowright}');
349342

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

360-
await user.type(minuteInput, '{arrowleft}');
353+
await userEvent.type(minuteInput, '{arrowleft}');
361354

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

371-
await user.type(hourInput, '{arrowleft}');
364+
await userEvent.type(hourInput, '{arrowleft}');
372365

373366
expect(hourInput).toHaveFocus();
374367
});
@@ -380,7 +373,7 @@ describe('TimeInput', () => {
380373
const hourInput = customInputs[0] as HTMLInputElement;
381374
const minuteInput = customInputs[1];
382375

383-
await user.type(hourInput, '4');
376+
await userEvent.type(hourInput, '4');
384377

385378
expect(minuteInput).toHaveFocus();
386379
});
@@ -392,7 +385,7 @@ describe('TimeInput', () => {
392385
const hourInput = customInputs[0] as HTMLInputElement;
393386
const minuteInput = customInputs[1];
394387

395-
await user.type(hourInput, '03');
388+
await userEvent.type(hourInput, '03');
396389

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

443-
await user.type(hourInput, '1');
436+
await userEvent.type(hourInput, '1');
444437

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

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)