Skip to content

Commit 4575a91

Browse files
committed
Replace Happy DOM with Vitest Browser Mode
1 parent 43a6836 commit 4575a91

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

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

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

314-
await user.type(monthInput, '{arrowright}');
307+
await userEvent.type(monthInput, '{arrowright}');
315308

316309
expect(dayInput).toHaveFocus();
317310
});
@@ -328,7 +321,7 @@ describe('DateInput', () => {
328321
) as HTMLSpanElement;
329322
const separatorKey = separator.textContent as string;
330323

331-
await user.type(monthInput, separatorKey);
324+
await userEvent.type(monthInput, separatorKey);
332325

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

342-
await user.type(yearInput, '{arrowright}');
335+
await userEvent.type(yearInput, '{arrowright}');
343336

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

354-
await user.type(dayInput, '{arrowleft}');
347+
await userEvent.type(dayInput, '{arrowleft}');
355348

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

365-
await user.type(monthInput, '{arrowleft}');
358+
await userEvent.type(monthInput, '{arrowleft}');
366359

367360
expect(monthInput).toHaveFocus();
368361
});
@@ -374,7 +367,7 @@ describe('DateInput', () => {
374367
const monthInput = customInputs[0] as HTMLInputElement;
375368
const dayInput = customInputs[1];
376369

377-
await user.type(monthInput, '4');
370+
await userEvent.type(monthInput, '4');
378371

379372
expect(dayInput).toHaveFocus();
380373
});
@@ -386,7 +379,7 @@ describe('DateInput', () => {
386379
const monthInput = customInputs[0] as HTMLInputElement;
387380
const dayInput = customInputs[1];
388381

389-
await user.type(monthInput, '03');
382+
await userEvent.type(monthInput, '03');
390383

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

437-
await user.type(monthInput, '1');
430+
await userEvent.type(monthInput, '1');
438431

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

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)