Skip to content

Commit 4e7cb80

Browse files
committed
Replace Happy DOM with Vitest Browser Mode
1 parent 1895faa commit 4e7cb80

File tree

10 files changed

+182
-114
lines changed

10 files changed

+182
-114
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-date-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-date-picker/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,17 @@
109109
"@testing-library/dom": "^10.0.0",
110110
"@testing-library/jest-dom": "^6.0.0",
111111
"@testing-library/react": "^16.0.0",
112-
"@testing-library/user-event": "^14.5.0",
113112
"@types/node": "*",
114113
"@types/react": "*",
115114
"@types/react-dom": "*",
115+
"@vitest/browser": "^3.2.3",
116116
"cpy-cli": "^5.0.0",
117-
"happy-dom": "^15.10.2",
117+
"playwright": "^1.51.1",
118118
"react": "^18.2.0",
119119
"react-dom": "^18.2.0",
120120
"typescript": "^5.5.2",
121121
"vitest": "^3.2.3",
122-
"vitest-canvas-mock": "^0.2.2"
122+
"vitest-browser-react": "^1.0.1"
123123
},
124124
"peerDependencies": {
125125
"@types/react": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0",

packages/react-date-picker/src/DateInput.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 DateInput from './DateInput.js';
66

@@ -25,13 +25,6 @@ describe('DateInput', () => {
2525
className: 'react-date-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(<DateInput {...defaultProps} />);
3730

@@ -310,7 +303,7 @@ describe('DateInput', () => {
310303
const monthInput = customInputs[0] as HTMLInputElement;
311304
const dayInput = customInputs[1];
312305

313-
await user.type(monthInput, '{arrowright}');
306+
await userEvent.type(monthInput, '{arrowright}');
314307

315308
expect(dayInput).toHaveFocus();
316309
});
@@ -327,7 +320,7 @@ describe('DateInput', () => {
327320
) as HTMLSpanElement;
328321
const separatorKey = separator.textContent as string;
329322

330-
await user.type(monthInput, separatorKey);
323+
await userEvent.type(monthInput, separatorKey);
331324

332325
expect(dayInput).toHaveFocus();
333326
});
@@ -338,7 +331,7 @@ describe('DateInput', () => {
338331
const customInputs = container.querySelectorAll('input[data-input]');
339332
const yearInput = customInputs[2] as HTMLInputElement;
340333

341-
await user.type(yearInput, '{arrowright}');
334+
await userEvent.type(yearInput, '{arrowright}');
342335

343336
expect(yearInput).toHaveFocus();
344337
});
@@ -350,7 +343,7 @@ describe('DateInput', () => {
350343
const monthInput = customInputs[0];
351344
const dayInput = customInputs[1] as HTMLInputElement;
352345

353-
await user.type(dayInput, '{arrowleft}');
346+
await userEvent.type(dayInput, '{arrowleft}');
354347

355348
expect(monthInput).toHaveFocus();
356349
});
@@ -361,7 +354,7 @@ describe('DateInput', () => {
361354
const customInputs = container.querySelectorAll('input[data-input]');
362355
const monthInput = customInputs[0] as HTMLInputElement;
363356

364-
await user.type(monthInput, '{arrowleft}');
357+
await userEvent.type(monthInput, '{arrowleft}');
365358

366359
expect(monthInput).toHaveFocus();
367360
});
@@ -373,7 +366,7 @@ describe('DateInput', () => {
373366
const monthInput = customInputs[0] as HTMLInputElement;
374367
const dayInput = customInputs[1];
375368

376-
await user.type(monthInput, '4');
369+
await userEvent.type(monthInput, '4');
377370

378371
expect(dayInput).toHaveFocus();
379372
});
@@ -385,7 +378,7 @@ describe('DateInput', () => {
385378
const monthInput = customInputs[0] as HTMLInputElement;
386379
const dayInput = customInputs[1];
387380

388-
await user.type(monthInput, '03');
381+
await userEvent.type(monthInput, '03');
389382

390383
expect(dayInput).toHaveFocus();
391384
});
@@ -433,7 +426,7 @@ describe('DateInput', () => {
433426
const customInputs = container.querySelectorAll('input[data-input]');
434427
const monthInput = customInputs[0] as HTMLInputElement;
435428

436-
await user.type(monthInput, '1');
429+
await userEvent.type(monthInput, '1');
437430

438431
expect(monthInput).toHaveFocus();
439432
});

packages/react-date-picker/src/DatePicker.spec.tsx

Lines changed: 2 additions & 2 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 DatePicker from './DatePicker.js';
66

@@ -407,7 +407,7 @@ describe('DatePicker', () => {
407407
it('closes Calendar component when clicked outside', async () => {
408408
const { container } = render(<DatePicker isOpen />);
409409

410-
userEvent.click(document.body);
410+
await userEvent.click(document.body);
411411

412412
await waitForElementToBeRemovedOrHidden(() =>
413413
container.querySelector('.react-date-picker__calendar'),

packages/react-date-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-date-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-date-picker/vitest.setup.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { afterEach } from 'vitest';
22
import { cleanup } from '@testing-library/react';
33
import '@testing-library/jest-dom/vitest';
4-
import 'vitest-canvas-mock';
54

65
afterEach(() => {
76
cleanup();

0 commit comments

Comments
 (0)