Skip to content

Commit 1fee2fe

Browse files
author
최한울
committed
jest 가 작동하지 않는 문제를 해결한다.
- npx jest 입력 시 테스트 코드에서 import 를 사용 할 수 없다는 오류 메세지가 계속 노출됨(참고이미지: https://i.stack.imgur.com/kmWMC.png) - App 파일에 있는 css 임포트를 제거하니 다른 오류메세지가 찍힘. ReferenceError: document is not defined 라는 에러가 나타나서 해결해보려 시도 - jest.config.js 파일의 설정에 testEnvironment 를 추가하여 해결(참고: testing-library/react-testing-library#422)
1 parent 893e311 commit 1fee2fe

File tree

9 files changed

+8144
-13030
lines changed

9 files changed

+8144
-13030
lines changed

babel.config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,12 @@ module.exports = {
1010
],
1111
'@babel/preset-react',
1212
],
13+
plugins: [
14+
[
15+
'@babel/plugin-transform-react-jsx',
16+
{
17+
runtime: 'automatic',
18+
},
19+
],
20+
],
1321
};

jest.config.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module.exports = {
2+
testEnvironment: "jest-environment-jsdom",
3+
setupFilesAfterEnv: [
4+
'jest-plugin-context/setup',
5+
'./jest.setup',
6+
],
7+
coverageThreshold: {
8+
global: {
9+
branches: 100,
10+
functions: 100,
11+
lines: 100,
12+
statements: 100,
13+
},
14+
},
15+
};

jest.setup.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import '@testing-library/jest-dom';

package-lock.json

Lines changed: 7268 additions & 10944 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,23 @@
5050
},
5151
"devDependencies": {
5252
"@babel/core": "^7.14.6",
53+
"@babel/plugin-transform-modules-commonjs": "^7.14.5",
5354
"@babel/preset-env": "^7.14.7",
5455
"@babel/preset-react": "^7.14.5",
5556
"@testing-library/jest-dom": "^5.14.1",
5657
"@testing-library/react": "^11.2.7",
5758
"@types/jest": "^26.0.23",
5859
"babel-jest": "^27.0.6",
5960
"babel-loader": "^8.2.2",
61+
"babel-plugin-syntax-class-properties": "^6.13.0",
6062
"eslint": "^7.2.0",
6163
"eslint-config-airbnb": "18.2.1",
6264
"eslint-plugin-import": "^2.22.1",
6365
"eslint-plugin-jsx-a11y": "^6.4.1",
6466
"eslint-plugin-react": "^7.21.5",
6567
"eslint-plugin-react-hooks": "^1.7.0",
6668
"jest": "^27.0.6",
69+
"jest-plugin-context": "^2.9.0",
6770
"webpack-cli": "^4.7.2"
6871
}
6972
}

src/App.jsx

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,6 @@
1-
import React, { useState, useEffect } from 'react';
2-
3-
import './App.css';
1+
import React from 'react';
42

53
function App() {
6-
const [appName, setAppName] = useState(null);
7-
8-
useEffect(() => {
9-
fetch('api/group')
10-
.then((res) => res.json())
11-
.then((data) => setAppName(data.appName));
12-
}, []);
13-
14-
return (
15-
<div className="App">
16-
<header className="App-header">
17-
{appName ? `Hello ${appName}` : 'Hello World'}
18-
</header>
19-
</div>
20-
);
214
}
225

236
export default App;

src/App.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { render } from '@testing-library/react';
22

3+
import '@testing-library/jest-dom';
4+
35
import App from './App';
46

57
describe('App', () => {

webpack.config.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
const path = require('path');
2+
13
module.exports = {
4+
entry: path.resolve(__dirname, 'src/index.jsx'),
25
module: {
36
rules: [
47
{
@@ -8,4 +11,12 @@ module.exports = {
811
},
912
],
1013
},
14+
resolve: {
15+
extensions: ['.js', '.jsx'],
16+
},
17+
devServer: {
18+
historyApiFallback: {
19+
index: 'index.html',
20+
},
21+
},
1122
};

yarn.lock

Lines changed: 835 additions & 2068 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)