Skip to content

Commit 42ca7f9

Browse files
committed
chore(components): RTL에 jestMatcher, userEvent 셋업
1 parent 8c82a00 commit 42ca7f9

31 files changed

+619
-42
lines changed

.pnp.cjs

+291-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

.yarnrc.yml

+6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ packageExtensions:
2020
dependencies:
2121
"@babel/plugin-syntax-flow": "*"
2222
"@babel/plugin-transform-react-jsx": "*"
23+
"@testing-library/user-event@*":
24+
dependencies:
25+
"@testing-library/dom": "*"
26+
"eslint-plugin-jest-dom@*":
27+
dependencies:
28+
"@testing-library/dom": "*"
2329
# No matching package in the dependency tree; you may not need this rule anymore.
2430
# eslint-plugin-i@*:
2531
# dependencies:

commitlint.config.cjs

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
module.exports = {
22
extends: ['@commitlint/config-conventional'],
3+
rules: {
4+
'subject-case': [0],
5+
},
36
};

components/.eslintrc.cjs

+13-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ module.exports = {
33
'plugin:react/recommended',
44
'plugin:react/jsx-runtime',
55
'plugin:react-hooks/recommended',
6+
'plugin:testing-library/react',
7+
'plugin:jest-dom/recommended',
68
],
79
parserOptions: {
810
ecmaFeatures: {
@@ -13,8 +15,18 @@ module.exports = {
1315
react: {
1416
version: 'detect',
1517
},
18+
'import/resolver': {
19+
typescript: {
20+
// ESLint의 실행 기준이 root임
21+
project: './components/tsconfig.json',
22+
},
23+
},
1624
},
17-
plugins: ['react-refresh'],
25+
plugins: [
26+
'testing-library',
27+
'jest-dom',
28+
'react-refresh'
29+
],
1830
rules: {
1931
'react-refresh/only-export-components': 'warn',
2032
},

components/__tests__/example.test.tsx

+21-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
1-
import { render, screen, within } from '@testing-library/react';
1+
import App from '@src/App';
2+
import Home from '@src/Home';
3+
import { screen, within } from '@testing-library/react';
24
import { expect, test } from 'vitest';
35

4-
import Home from '../src/Home';
6+
import { testRender } from './utilities';
57

6-
test('Example', () => {
7-
render(<Home />);
8+
test('Home', () => {
9+
testRender(<Home />);
810
const main = within(screen.getByRole('main'));
9-
expect(
10-
main.getByRole('heading', { level: 1, name: /welcome to next\.js!/i }),
11-
).toBeDefined();
11+
const h1 = main.getByRole('heading', {
12+
level: 1,
13+
name: /welcome to next\.js!/i,
14+
});
15+
16+
expect(h1).toBeInTheDocument();
17+
});
18+
19+
test('App', async () => {
20+
const { testUser } = testRender(<App />);
21+
const button = screen.getByRole('button');
22+
23+
await testUser.click(button);
24+
25+
expect(button).toHaveTextContent('count is 1');
1226
});

components/__tests__/setup.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import matchers from '@testing-library/jest-dom/matchers';
2+
import { expect } from 'vitest';
3+
4+
expect.extend(matchers);

components/__tests__/utilities.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { render as renderComponent } from '@testing-library/react';
2+
import userEvent from '@testing-library/user-event';
3+
import type { ReactElement } from 'react';
4+
5+
type RenderOptions = Parameters<typeof renderComponent>[1];
6+
7+
export const testRender = (ui: ReactElement, options?: RenderOptions) => {
8+
return {
9+
...renderComponent(ui, options),
10+
testUser: userEvent.setup(),
11+
};
12+
};

components/package.json

+8
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,30 @@
2727
"@storybook/react": "^7.0.24",
2828
"@storybook/react-vite": "^7.0.24",
2929
"@storybook/testing-library": "^0.0.14-next.2",
30+
"@testing-library/dom": "^9.3.1",
31+
"@testing-library/jest-dom": "^5.17.0",
3032
"@testing-library/react": "^14.0.0",
33+
"@testing-library/user-event": "^14.4.3",
3134
"@types/react": "^18.0.37",
3235
"@types/react-dom": "^18.0.11",
36+
"@types/testing-library__jest-dom": "^5.14.8",
3337
"@vitejs/plugin-react": "^4.0.0",
3438
"@vitest/coverage-v8": "^0.33.0",
3539
"eslint": "*",
3640
"eslint-config-react-app": "^7.0.1",
41+
"eslint-plugin-jest-dom": "^5.0.1",
3742
"eslint-plugin-react": "^7.32.2",
3843
"eslint-plugin-react-hooks": "^4.6.0",
3944
"eslint-plugin-react-refresh": "^0.3.4",
4045
"eslint-plugin-storybook": "^0.6.12",
46+
"eslint-plugin-testing-library": "^5.11.0",
47+
"jest-environment-jsdom": "^29.6.1",
4148
"jsdom": "^22.1.0",
4249
"prop-types": "^15.8.1",
4350
"storybook": "^7.0.24",
4451
"typescript": "*",
4552
"vite": "^4.3.9",
53+
"vite-tsconfig-paths": "^4.2.0",
4654
"vitest": "^0.33.0"
4755
}
4856
}

components/src/main.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import './index.css';
22

3+
import App from '@src/App';
34
import { createRoot } from 'react-dom/client';
45

5-
import App from './App.tsx';
6-
76
createRoot(document.getElementById('root') as HTMLElement).render(<App />);

components/src/stories/Page.stories.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const LoggedOut: Story = {};
2121
export const LoggedIn: Story = {
2222
play: async ({ canvasElement }) => {
2323
const canvas = within(canvasElement);
24-
const loginButton = await canvas.getByRole('button', {
24+
const loginButton = canvas.getByRole('button', {
2525
name: /Log in/i,
2626
});
2727
await userEvent.click(loginButton);

components/tsconfig.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
/* Bundler mode */
88
"resolveJsonModule": true,
99
"isolatedModules": true,
10-
"jsx": "react-jsx"
10+
"jsx": "react-jsx",
11+
"paths": {
12+
"@src/*": ["./src/*"]
13+
}
1114
},
1215
"include": ["**/*.ts", "**/*.tsx"]
1316
}

components/vite.config.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import react from '@vitejs/plugin-react';
22
import { defineConfig } from 'vite';
3+
import tsconfigPaths from 'vite-tsconfig-paths';
34

4-
export default defineConfig({
5-
plugins: [react()],
6-
});
5+
const config = {
6+
plugins: [react(), tsconfigPaths()],
7+
};
8+
9+
export default defineConfig(config);

components/vitest.config.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import react from '@vitejs/plugin-react';
2-
import { defineConfig, type UserConfig } from 'vite';
3-
import type { InlineConfig } from 'vitest';
1+
import path from 'node:path';
42

5-
type ViteConfig = UserConfig & { test: InlineConfig };
3+
import react from '@vitejs/plugin-react';
4+
import tsconfigPaths from 'vite-tsconfig-paths';
5+
import { defineConfig } from 'vitest/config';
66

7-
const config: ViteConfig = {
8-
plugins: [react()],
7+
const config = {
8+
plugins: [react(), tsconfigPaths()],
99
test: {
10+
setupFiles: path.resolve(__dirname, './__tests__/setup.ts'),
1011
environment: 'jsdom',
1112
},
1213
};

0 commit comments

Comments
 (0)