Skip to content

Commit dc98ddf

Browse files
authored
Use vitest browser mode (#167)
* chore: config file を統合する * chore: nodejs の型定義が抜けていた * ci: use vitest browser mode instead of jsdom * opt in to browser mode * chore(deps): add node types * ci: setup browser * ci: split browser test job * too much escape * fix eslint rule * ci: cache on main
1 parent 57ddf40 commit dc98ddf

File tree

10 files changed

+430
-309
lines changed

10 files changed

+430
-309
lines changed

.github/workflows/ci.yml

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,48 @@ jobs:
4545
run: pnpm run typecheck
4646

4747
- name: Run Vitest
48-
run: pnpm run test
48+
run: pnpm run test:node
49+
50+
browser-test:
51+
name: Run browser tests
52+
runs-on: ubuntu-24.04
53+
timeout-minutes: 10
54+
55+
steps:
56+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
57+
with:
58+
persist-credentials: false
59+
60+
- name: Setup Node.js and pnpm
61+
uses: ./.github/workflows/composite/setup
62+
63+
- name: Get installed Playwright version
64+
id: playwright-version
65+
run: echo "version=$(pnpm exec playwright --version | grep -oE '[0-9]+\.[0-9]+\.[0-9]+')" >> "$GITHUB_OUTPUT"
66+
67+
- name: Restore Playwright Browser Cache
68+
uses: actions/cache/restore@1bd1e32a3bdc45362d1e726936510720a7c30a57 # v4.2.0
69+
id: playwright-cache
70+
with:
71+
path: ~/.cache/ms-playwright
72+
key: '${{ runner.os }}-playwright-${{ steps.playwright-version.outputs.version }}'
73+
restore-keys: ${{ runner.os }}-playwright-
74+
75+
- name: Install Playwright Browsers
76+
if: steps.playwright-cache.outputs.cache-hit != 'true'
77+
run: pnpm exec playwright install --with-deps
78+
79+
- name: Install Playwright system dependencies
80+
if: steps.playwright-cache.outputs.cache-hit == 'true'
81+
run: pnpm exec playwright install-deps
82+
83+
- name: Save Playwright Browser Cache if main branch
84+
if: github.ref_name == 'main'
85+
id: save-pnpm-cache
86+
uses: actions/cache/save@1bd1e32a3bdc45362d1e726936510720a7c30a57 # v4.2.0
87+
with:
88+
path: ~/.cache/ms-playwright
89+
key: ${{ steps.playwright-cache.outputs.cache-primary-key }}
90+
91+
- name: Run test
92+
run: pnpm run test:browser

app/components/input/DateRangePicker.test.tsx renamed to app/components/input/DateRangePicker.browser.test.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { getFormProps, useForm } from "@conform-to/react";
2+
import { userEvent } from "@vitest/browser/context";
23
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
34

4-
import { render, screen } from "../../../test/test-react";
5-
import { userEvent } from "../../../test/vitest-setup";
5+
import { render } from "../../../test/test-react";
66
import { DateRangePicker } from "./DateRangePicker";
77

88
type Form = {
@@ -22,8 +22,6 @@ type PageProps = {
2222

2323
beforeEach(() => {
2424
vi.useFakeTimers({ shouldAdvanceTime: true });
25-
// GitHub Actions では TZ が UTC になっているため、テスト時の TZ も UTC に設定する
26-
process.env.TZ = "UTC";
2725
});
2826

2927
afterEach(() => {
@@ -60,7 +58,7 @@ describe("DateRangePicker", () => {
6058
test("convert で指定した処理を用いて UI の値をフォームに反映できる", async () => {
6159
// 確実に 2025 年 1 月のカレンダーを表示するため、システム時刻を固定する
6260
vi.setSystemTime(new Date("2025-01-15T00:00:00Z"));
63-
render(<Page defaultValue={{ start: null, end: null }} />);
61+
const screen = render(<Page defaultValue={{ start: null, end: null }} />);
6462

6563
const button = screen.getByRole("button", { name: "Date Range" });
6664
await userEvent.click(button);
@@ -80,7 +78,7 @@ describe("DateRangePicker", () => {
8078
});
8179

8280
test("convert で指定した処理を用いてフォームの値を UI に反映できる", () => {
83-
render(
81+
const screen = render(
8482
<Page
8583
defaultValue={{
8684
start: "2025-01-09T15:00:00.000Z",

eslint.config.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -147,24 +147,18 @@ const config = [
147147
},
148148
},
149149
{
150-
files: ["**/*.{test,spec}.{js,jsx,ts,tsx}"],
150+
files: ["**/*.browser.test.{js,jsx,ts,tsx}"],
151151
rules: {
152152
"@typescript-eslint/no-restricted-imports": [
153153
"error",
154154
{
155155
paths: [
156156
{
157157
allowTypeImports: true,
158-
name: "@testing-library/react",
158+
name: "vitest-browser-react",
159+
importNames: ["render"],
159160
message:
160-
"Please import from '~/test/test-react' instead of '@testing-library/react'.",
161-
},
162-
{
163-
allowTypeImports: true,
164-
name: "@testing-library/user-event",
165-
importNames: ["default"],
166-
message:
167-
"Please import 'userEvent' from '~/test/test-react' instead of '@testing-library/user-event'.",
161+
"Please import from '<root>/test/test-react' instead of 'vitest-browser-react'.",
168162
},
169163
],
170164
},

package.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
"typecheck": "pnpm run typegen && tsc --noEmit",
1515
"typegen": "react-router typegen",
1616
"orval": "orval",
17-
"test": "vitest",
18-
"test:run": "vitest run",
19-
"check": "pnpm typecheck && pnpm build && pnpm lint && pnpm format:ci && pnpm test:run"
17+
"test": "TZ=UTC vitest --run",
18+
"test:node": "pnpm run test --project node",
19+
"test:browser": "pnpm run test --project browser",
20+
"check": "pnpm typecheck && pnpm build && pnpm lint && pnpm format:ci && pnpm test"
2021
},
2122
"dependencies": {
2223
"@conform-to/dom": "1.5.1",
@@ -52,14 +53,12 @@
5253
"@react-router/fs-routes": "7.6.0",
5354
"@svgr/core": "8.1.0",
5455
"@svgr/plugin-jsx": "8.1.0",
55-
"@testing-library/dom": "10.4.0",
56-
"@testing-library/jest-dom": "6.6.3",
57-
"@testing-library/react": "16.3.0",
58-
"@testing-library/user-event": "14.6.1",
5956
"@types/eslint-plugin-jsx-a11y": "6.10.0",
57+
"@types/node": "22.17.0",
6058
"@types/react": "19.0.10",
6159
"@types/react-dom": "19.0.4",
6260
"@vitejs/plugin-react": "4.4.1",
61+
"@vitest/browser": "3.2.4",
6362
"autoprefixer": "10.4.21",
6463
"eslint": "9.26.0",
6564
"eslint-config-flat-gitignore": "2.1.0",
@@ -71,9 +70,9 @@
7170
"eslint-typegen": "2.1.0",
7271
"globals": "16.1.0",
7372
"jiti": "2.4.2",
74-
"jsdom": "26.1.0",
7573
"msw": "2.8.2",
7674
"orval": "7.9.0",
75+
"playwright": "1.54.2",
7776
"pnpm": "10.10.0",
7877
"postcss": "8.5.3",
7978
"postcss-preset-mantine": "1.17.0",
@@ -86,7 +85,8 @@
8685
"unplugin-icons": "22.1.0",
8786
"vite": "6.3.5",
8887
"vite-tsconfig-paths": "5.1.4",
89-
"vitest": "3.1.3"
88+
"vitest": "3.2.4",
89+
"vitest-browser-react": "1.0.1"
9090
},
9191
"engines": {
9292
"node": "^22.0.0"

0 commit comments

Comments
 (0)