๊ธฐ๋ฅ ์ ์์ฒญํ๊ฑฐ๋ ๋ฒ๊ทธ๋ฅผ ๋ณด๊ณ ํ์๊ฒ ์ต๋๊น?
๋ฒ๊ทธ ์ ๊ณ
ํ์ฌ ํ๋์ ๋ฌด์์
๋๊น?
ํ์ฌ ์ฌ๋ฌ ํ๋ก์ ํธ์์ ๋์ผํ ํ
์คํธ ์ค์ ์ ์ฌ์ฉํ๊ณ ์์ต๋๋ค. ๋ชจ๋ ๋ด ์์ฉ๊ตฌ์์ ๊ฐ์ ธ์์ต๋๋ค. ์ด ๋ฌธ์ ๋ ์ต๊ทผ์ ๋ชจ๋ ๋ฐ์ํ์ง๋ง ์์ง ์ถ์ ํ ์ ์์ต๋๋ค. Jest๊ฐ ๋ด package.json์ babel ๊ตฌ์ฑ์ ๋๋ฝํ๊ณ ํ
์คํธ ์ค์ํธ๊ฐ '์๊ธฐ์น ์์ ํ ํฐ ๊ฐ์ ธ์ค๊ธฐ'๋ก ์คํจํ๋ ๊ฒ ๊ฐ์ต๋๋ค. ์ผ์ฃผ์ผ ์ ๊น์ง๋ง ํด๋ ์ด ๊ตฌ์ฑ์ ์ฌ๋ฌ ํ๋ก์ ํธ์์ ์ฑ๊ณต์ ์ผ๋ก ์๋ํ๊ธฐ ๋๋ฌธ์ ํ๊ท๋ผ๊ณ ๊ฐ์ ํ๊ณ ๋ณด๊ณ ํด์ผ ํ๋ค๊ณ ์๊ฐํ์ต๋๋ค.
.babelrc ํ์ผ์ ์ถ๊ฐํ๋ฉด ํ ์คํธ ์ค์ํธ๊ฐ ํ๋์ ํ ์คํธ๋ฅผ ์ ์ธํ ๋ชจ๋ ํ ์คํธ์ ํต๊ณผํฉ๋๋ค. ์ฌ๊ธฐ์ ๋์ผํ Unexpected Token Import๊ฐ ๋ฐ์ํฉ๋๋ค. ์ด๋ ๋ชจ๋ ES6 ๊ฐ์ ธ์ค๊ธฐ๋ฅผ ์ฌ์ฉํ๋ 12๊ฐ์ ํต๊ณผ ํ ์คํธ๊ฐ ์๋ค๋ ์ ์ ๊ณ ๋ คํ๋ฉด ์ด์ํ๊ฒ ๋ณด์ ๋๋ค. .babelrc๊ฐ ์์ผ๋ฉด 14๊ฐ์ ํ ์คํธ๊ฐ ๋ชจ๋ ์คํจํฉ๋๋ค.
ํ์ฌ ๋์์ด ๋ฒ๊ทธ์ธ ๊ฒฝ์ฐ ์ฌํํ๋ ๋จ๊ณ๋ฅผ ์ ๊ณตํ๊ณ ๊ฐ๋ฅํ ๊ฒฝ์ฐ npm install
๋ฐ npm test
ํ ์ ์๋ GitHub์ ์ต์ ์ ์ฅ์๋ฅผ ์ ๊ณตํ์ญ์์ค.
๊ทธ๊ฒ์ ๋ด์์ ์ฌํ ํ ์์๋ ๋ณด์ผ๋ฌ ์ค์นํ์ฌ npm run setup
์คํ npm run test
.
์์๋๋ ๋์์ ๋ฌด์์
๋๊น?
์ด์ ์ ์๋ํ๋ Jest ๊ตฌ์ฑ์ด ๊ณ์ ์๋ํ ๊ฒ์ผ๋ก ์์ํฉ๋๋ค. ๋ด ์ ์ฒ๋ฆฌ๊ธฐ๊ฐ ๋ ์ด์ ์ด์ ์ฒ๋ผ babel-jest์ ํจ๊ป ์คํ๋์ง ์๋ ๊ฒ ๊ฐ์ต๋๋ค.
--debug
Jest๋ฅผ ๋ค์ ์คํํ๊ณ ์ธ์ํ ์ ์ฒด ๊ตฌ์ฑ์ ์ ๊ณตํฉ๋๋ค.
jest version = 16.0.2
test framework = jasmine2
config = {
"scriptPreprocessor": "/Users/myMac/Developer/works-in-progress/open-source-maintaining/scalable-react-boilerplate/config/testing/preprocessor.js",
"moduleFileExtensions": [
"js",
"jsx",
"json"
],
"moduleDirectories": [
"node_modules",
"bower_components",
"shared"
],
"rootDir": "/Users/myMac/Developer/works-in-progress/open-source-maintaining/scalable-react-boilerplate",
"name": "-Users-myMac-Developer-works-in-progress-open-source-maintaining-scalable-react-boilerplate",
"setupFiles": [],
"testRunner": "/Users/myMac/Developer/works-in-progress/open-source-maintaining/scalable-react-boilerplate/node_modules/jest-jasmine2/build/index.js",
"automock": false,
"bail": false,
"browser": false,
"cacheDirectory": "/var/folders/nq/7kdqy15d3m399326f7wtb_6w0000gn/T/jest",
"clearMocks": false,
"coveragePathIgnorePatterns": [
"/node_modules/"
],
"coverageReporters": [
"json",
"text",
"lcov",
"clover"
],
"globals": {},
"haste": {
"providesModuleNodeModules": []
},
"mocksPattern": "__mocks__",
"moduleNameMapper": {},
"modulePathIgnorePatterns": [],
"noStackTrace": false,
"notify": false,
"preset": null,
"preprocessorIgnorePatterns": [
"/node_modules/"
],
"resetModules": false,
"testEnvironment": "jest-environment-jsdom",
"testPathDirs": [
"/Users/myMac/Developer/works-in-progress/open-source-maintaining/scalable-react-boilerplate"
],
"testPathIgnorePatterns": [
"/node_modules/"
],
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.jsx?$",
"testURL": "about:blank",
"timers": "real",
"useStderr": false,
"verbose": null,
"watch": false,
"cache": true,
"watchman": true,
"testcheckOptions": {
"times": 100,
"maxSize": 200
}
}
React-Native ๋ฐ Jest์ ๋์ผํ ๋ฌธ์ ๊ฐ ์์ต๋๋ค.
๋ค์์ ์ฌ์ฉํ์ฌ ์ ํ๋ก์ ํธ๋ฅผ ์์ํ๋ ๊ฒฝ์ฐ:
๋ฐ์ ๋ค์ดํฐ๋ธ ์ด๊ธฐํ AwesomeProject
CD ์ด์ธํ๋ก์ ํธ
๊ทธ๋ฐ ๋ค์ npm install native-base --save์ ๊ฐ์ ๋ชจ๋์ ์ค์นํ์ญ์์ค.
๊ธฐ๋ณธ ๊ธฐ๋ฐ์์ index.ios.js ํ์ผ๋ก ๊ตฌ์ฑ ์์ ๊ฐ์ ธ์ค๊ธฐ
npm ํ
์คํธ์์ ๋ค์ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
../test/node_modules/native-base/index.js:4
'./Components/vendor/react-native-drawer'์์ ์๋ ๊ฐ์ ธ์ค๊ธฐ;
^^^^^^
SyntaxError: ์๊ธฐ์น ์์ ํ ํฐ ๊ฐ์ ธ์ค๊ธฐ
http://facebook.github.io/jest/docs/tutorial-react-native.html#transformignorepatterns -customization ์ฐธ์กฐ
๋์ผํ ๋ฌธ์ ๊ฐ ์์ต๋๋ค. @cpojer๊ฐ ์ ๊ณตํ ๋งํฌ๊ฐ ๋์์ด ๋์ง ์๋ ๊ฒ ๊ฐ์ต๋๋ค ๐
์๋
ํ์ธ์! 'transformIgnorePatterns'๋ ....mmmm...ํด์ผ ํฉ๋๋ค - ๋ฌด์)
๊ทธ๋ฌ๋ node_modules์ ํจํค์ง์์ ๊ฐ์ ธ์ค๊ธฐ๋ฅผ ์ด๋ป๊ฒ ์ฌ์ฉํ ์ ์์ต๋๊น?
์ด ์ํ์ด ๋์์ด ๋์์ต๋๋ค :)
{
"jest": {
"verbose": true,
"moduleDirectories": ["node_modules", "src"]
},
}
UPD1
ํ์ฌ ๋ด Jest ๊ตฌ์ฑ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
"jest": {
"verbose": true,
"transform": {
"^.+\\.js$": "babel-jest",
"^.+\\.(css|scss|less)$": "jest-css-modules"
},
"globals": {
"NODE_ENV": "test"
},
"moduleFileExtensions": [
"js",
"jsx"
],
"moduleDirectories": [
"node_modules",
"src/frontend",
"src/shared"
]
},
... ๊ทธ๋ฆฌ๊ณ ๊ทธ๊ฒ์ ๋๋ฅผ ์ํด ์ผํ๊ณ ์์ต๋๋ค :)
์๋ถ ์ธ์ฌ, Pavel Tretyakov
์๋ ํ์ธ์ ์ฌ๋ฌ๋ถ, ๋๋ ์ฌ์ ํ์ด ๋ฌธ์ ๋ฅผ ๊ฒช๊ณ ์์ง๋ง ๋ฐ์ ๋ค์ดํฐ๋ธ๋ฅผ ์ฌ์ฉํ๊ณ ์์ง ์์ต๋๋ค. ๋ฌธ์ ๋ ํญ์ jest --no-cache๋ฅผ ์ฌ์ฉํ์ฌ ์์ ๋ฉ๋๋ค. ์ด ์ค๋ฅ๋ฅผ ๋์ง์ง ์๊ณ ์บ์๋ฅผ ์ฌ์ฉํ์ฌ ๋๋ด์ ํ ์ ์๋ ๋ฐฉ๋ฒ์ด ์์ต๋๊น?
์ด ๋ณํ์ด ๋ด create-react-app ์ ํ๋ฆฌ์ผ์ด์ ์์ ์๋ํ๋๋ก ํ ์ ์์ต๋๋ค. ES6 ๊ฐ์ ธ์ค๊ธฐ๊ฐ ํฌํจ๋ ๋ชจ๋ ํ ์คํธ ํ์ผ์์ ํญ์ "์๊ธฐ์น ์์ ํ ํฐ ๊ฐ์ ธ์ค๊ธฐ"๊ฐ ํ์๋ฉ๋๋ค. ๋ด๊ฐ ์ฐพ์ ์ ์๋ ๋ชจ๋ ์ ์์ ์๋ํ์ต๋๋ค. no-cache๋ ์ํฅ์ด ์์ต๋๋ค. ๋ถ๋ช ํ ๋ณํ์์ ์ด๋ฌํ ํ์ผ์ ๋ฌด์ํ ์ ์์ต๋๋ค(๋ณํํด์ผ ํฉ๋๋ค!).
๋๋ ํฌ๊ธฐํ๋ค. ํ์ํ ์ ๋ณด๋ฅผ ์ ๊ณตํ๊ฒ ๋์ด ๊ธฐ์ฉ๋๋ค.
์ ๋ ์ด ๋ฌธ์ ๋ฅผ ๊ฒช๊ณ ์๋ ๊ฒ ๊ฐ์ต๋๋ค.
์ฌ๊ธฐ๋ ๋ง์ฐฌ๊ฐ์ง
์ฌ๊ธฐ๋ ๋ง์ฐฌ๊ฐ์ง
Node์์ ES ๋ชจ๋์ด ์ง์๋์ง ์๊ณ ๋ธ๋ผ์ฐ์ ๋ก ๋ฐฐ์กํ ๋ ๋น๋ ํ์ดํ๋ผ์ธ(์นํฉ, ๋กค์ )์ผ๋ก ์ปดํ์ผํ๋ ๊ฒ์ ์๋์ ์ผ๋ก ์ค๋จํ๊ธฐ ๋๋ฌธ์ ์ด ๋ฌธ์ ๊ฐ ๋ฐ์ํ ๊ฐ๋ฅ์ฑ์ด ๋์ต๋๋ค.
babelrc์ "ํ ์คํธ" ํ๊ฒฝ์ ์ํด ES ๋ชจ๋์์ CommonJS๋ก ์ปดํ์ผ์ ํ์ฑํํด์ผ ํฉ๋๋ค( https://babeljs.io/docs/usage/babelrc/#env -option ๋ฐ https://babeljs.io/docs/ ์ฐธ์กฐ). ํ๋ฌ๊ทธ์ธ/transform-es2015-modules-commonjs/
{
"env": {
"production": {
"plugins": ["transform-es2015-modules-commonjs"]
}
}
}
๋ต๋ณ ๊ฐ์ฌํฉ๋๋ค, cpojer. ์ด์ jest๊ฐ ์ ์ญ์ ์ผ๋ก ์๋ํ์ง๋ง ๋ ธ๋ ์คํฌ๋ฆฝํธ๋ฅผ ์ฌ์ฉํ๋ ๋์์๋ ์๋ํ์ง ์์๊ธฐ ๋๋ฌธ์ ์ด์ ๋ ธ๋ ํ๊ฒฝ ์ค์ ์ ์ ๊ฑฐํ๊ณ ๊ธฐ๋ณธ ๋ ธ๋ ํ๊ฒฝ์ผ๋ก ์คํํ์ต๋๋ค. ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ๊ฒ ๊ฐ์์ผ๋ฏ๋ก ๋ด ๋ฌธ์ ๋ node-env ์ค์ ์ ์์๋ ๊ฒ ๊ฐ์ต๋๋ค. ๊ทธ๋ฌ๋ ์ฌ์ ํ ๋ชจ๋ํฐ๋งํด์ผ ํ ์ฌํญ์ ๋๋ค.
ํธ์ง: ์ด๊ฒ์ ์ ํ ์๋ํ์ง ์์์ต๋๋ค. @cpojer ์ ์์ ์ ๋งค๋ ฅ์ฒ๋ผ ์๋ํ์ต๋๋ค. ๊ฐ์ฌ ํด์.
@cpojer ์ ์์ ์ฌํญ์ด ์ ์๊ฒ
๊ต์ฅํฉ๋๋ค, ๋ํ ์ ๋ฅผ ์ํด ์ผํ์ต๋๋ค, ๊ฐ์ฌํฉ๋๋ค!
์๋นํ ๊ฐ๋จํ ๋ฐ์ ๋ค์ดํฐ๋ธ ์ฑ์์ ๋น์ทํ ๋ฌธ์ ๊ฐ ์์ต๋๋ค.
npm test
๋๋ jest
ํ๋ฉด 'SyntaxError: Unexpected token import'
๊ฐ ๋ฐํ๋ฉ๋๋ค.
.babelrc
{
"presets": ["react-native"]
}
.ํจํค์ง.json
{
"name": "BeeAppy",
"version": "0.1.0",
"private": true,
"devDependencies": {
"babel-jest": "^20.0.3",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-preset-react-native": "^2.0.0",
"enzyme": "^2.8.2",
"eslint": "^4.0.0",
"eslint-plugin-react": "^7.1.0",
"jest": "^20.0.4",
"react-addons-test-utils": "^15.6.0",
"react-dom": "^15.6.1",
"react-test-renderer": "^16.0.0-alpha.6"
},
"scripts": {
"start": "react-native start",
"android": "react-native run-android",
"ios": "react-native run-ios",
"test": "node node_modules/jest/bin/jest.js --watch"
},
"jest": {
"preset": "react-native"
},
"dependencies": {
"react": "16.0.0-alpha.6",
"react-native": "^0.44.0",
"react-navigation": "^1.0.0-beta.11"
}
}
@cpojer ๊ฐ ์ ์ํ env ์ค๋ํซ์ด
์ ๋ง ๋ปํ๋ค๋ฉด ์ฃ์กํฉ๋๋ค!
์ด๋ค ๋์์ด ๋ ๊ฒ์
๋๋ค, ๊ฐ์ฌํฉ๋๋ค
@JessicaBarclay .babelrc ๊ตฌ์ฑ์ ๋ค์ด๊ฐ์ผ ํฉ๋๋ค. ์ด์ ๊ฐ์ด:
"env": {
"development": {
"plugins": ["transform-es2015-modules-commonjs"]
}
}
์ ๊ฐ์ฌํฉ๋๋ค @rlage ! ์ ๋ ฌ:)
FWIW, ์ค์ ๋ก @cpojer ๊ฐ ์ ์ํ ๊ฒ๊ณผ ๊ฐ์ ํ
์คํธ ํ๊ฒฝ์์ ์ฌ๋ฐ๋ฅธ presets
๋ฐ plugins
.babelrc
์ค์ ํ์ง๋ง NODE_ENV=test
๋ฅผ ํฌํจํ๋ ๊ฒ์ ์์์ต๋๋ค. package.json
.
์ด์ ์๋ "์๊ธฐ์น ์์ ํ ํฐ ๊ฐ์ ธ์ค๊ธฐ"๊ฐ ์คํจํ์ต๋๋ค.
"scripts": {
...
"test": "jest",
}
ํ์ผ์ ์ฑ๊ณต์ ์ผ๋ก ๋ณํํ ํ:
"scripts": {
...
"test": "NODE_ENV=test jest",
}
@jlyman
Jest ๋ฌธ์๋ฅผ ๊ธฐ๋ฐ์ผ๋ก Jest๊ฐ ๊ธฐ๋ณธ์ ์ผ๋ก ํ ์คํธํ๋๋ก NODE_ENV๋ฅผ ์ค์ ํ๊ธฐ ๋๋ฌธ์ ํ์ํ์ง ์์์ผ ํฉ๋๋ค.
์ฐธ๊ณ : Babel์ env ์ต์ ์ ์ฌ์ฉํ์ฌ ๋ ๋ณต์กํ Babel ๊ตฌ์ฑ์ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ Jest๊ฐ ์๋์ผ๋ก NODE_ENV๋ฅผ ํ ์คํธ๋ก ์ ์ํ๋ค๋ ์ ์ ์ ์ํ์ญ์์ค. NODE_ENV๊ฐ ์ค์ ๋์ง ์์ ๊ฒฝ์ฐ ๊ธฐ๋ณธ์ ์ผ๋ก Babel๊ณผ ๊ฐ์ ๊ฐ๋ฐ ์น์ ์ ์ฌ์ฉํ์ง ์์ต๋๋ค.
์ด .babelrc๋ ๋๋ฅผ ์ํด ์ผํ ๊ฒ์ ๋๋ค.
{"env": {
"development": {
"plugins": ["transform-es2015-modules-commonjs"]
},
"test": {
"plugins": ["transform-es2015-modules-commonjs"]
}
}}
์ฐธ๊ณ : ์ด์ํด ๋ณด์ด์ง๋ง ๊ฐ๋ฐ ํค์ ํ ์คํธ ํค๊ฐ ๋ชจ๋ ์์ด์ผ ํฉ๋๋ค. ๊ทธ๋ ์ง ์์ผ๋ฉด ์๋ํ์ง ์์ต๋๋ค.
๊ทธ๋ผ ๋ ๊ทธ๋ฅ ์ ํ :
node_modules/.bin/jest
๋๋ package.json์ ์ฌ์ฉํ์ฌ:
{"scripts": {"test": "jest"}}
ํ ์คํธ๋ฅผ ์ํด NODE_ENV๋ฅผ ์ค์ ํ ํ์๊ฐ ์์ต๋๋ค.
@laszbalo ํฅ๋ฏธ๋กญ๊ฒ๋ ๋ ๋ฒ์งธ env
์ ์กด์ฌ๋ ํธ๋ฆญ์ ์ํํ๋ ๊ฒ ๊ฐ์ต๋๋ค. ๊ฐ๋ฐ ํ๊ฒฝ์ ์ถ๊ฐํ๊ณ ์คํฌ๋ฆฝํธ์์ ๋ช
์์ ์ธ NODE_ENV
์ ๋์ฌ๋ฅผ ์ ๊ฑฐํ๋ฉด ์๋ํฉ๋๋ค. ๋๋ ๋ฌธ์์ ๊ทธ ๋ถ๋ถ๋ ์ฝ์์ง๋ง ํจ๊ณผ๊ฐ ์์๊ธฐ ๋๋ฌธ์ ๊ทธ ์๋ฃจ์
์ผ๋ก ๋๋ฌ์ต๋๋ค.
@jlyman ๋ฌธ์์ ์๋ฃจ์ ์ ๋ฌด์์ ๋๊น?
@doudounannan ๋ช
ํํ์ง ์์ ๊ฒฝ์ฐ ์ฃ์กํฉ๋๋ค. ๋ด๊ฐ ๊ฐ์ก๋ ํด๊ฒฐ์ฑ
์ ๋ด๊ฐ ์ฌ๊ธฐ์ ๊ฒ์ํ ๊ฒ ์
๋๋ค. @laszbalo๊ฐ ๋งํ๋ฏ์ด ์ด๊ฒ์ Using Babel ์์ ๋ฌธ์๊ฐ ๋งํ๋ ๊ฒ๊ณผ ์ด๊ธ๋์ง ๋ง NODE_ENV test
์ ์ง์ ์ฃผ์
์ด ์๋ํ๋ ๊ฒ ๊ฐ์ต๋๋ค.
์ด์ํ๊ฒ๋ "transformIgnorePatterns": [ ]
ํ๋ฉด babel์ด ํ
์คํธํ ํ์ผ์ ๋ณํํ์ง๋ง transformIgnorePatterns
์ต์
์ ์ ์ธํ๋ฉด babel์ ์๋ฌด ๊ฒ๋ ํ์ง ์์ต๋๋ค... ํ
์คํธ์ฉ์ผ๋ก๋ง ๊ฐ๋จํ .babelrc๊ฐ ์์ต๋๋ค. ํ๋ก๋์
์๋ webpack์ ์ฌ์ฉํ์ง๋ง jest์ ํ
์คํธ์๋ ์ฌ์ฉํ์ง ์์):
{
"env": {
"test": {
"plugins": ["transform-es2015-modules-commonjs"]
}
}
}
https://github.com/facebook/jest/issues/2550#issuecomment -290812720์์ ์์ด๋์ด๋ฅผ ์ป์์ต๋๋ค. #770 ๋๋ ์ฌ๊ธฐ์ ์๋ ๋ค๋ฅธ ํญ๋ชฉ์ด ๋์์ด ๋์ง ์์๊ธฐ ๋๋ฌธ์ ๋๋ค...
๋ด๊ฐ ๋ฌด์์ ํ๋ ์ง ์๊ด์์ด ํญ์ ๊ฐ์ ์ค๋ฅ์ ๋๋ค.
/.../node_modules/@expo/react-native-action-sheet/ActionSheet.ios.js:3
import React from 'react';
^^^^^^
SyntaxError: Unexpected token import
at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/ScriptTransformer.js:289:17)
at Object.<anonymous> (node_modules/@expo/react-native-action-sheet/index.js:1:107)
at Object.<anonymous> (node_modules/react-native-gifted-chat/src/GiftedChat.js:10:29)
{
"env": {
"production": {
"plugins": ["transform-es2015-modules-commonjs"]
},
"development": {
"plugins": ["transform-es2015-modules-commonjs"]
},
"test": {
"plugins": ["transform-es2015-modules-commonjs"]
}
},
"presets": [
"react-native"
],
"plugins": [
["module-resolver", {
"cwd": "babelrc",
}]
]
}
"jest": {
"preset": "react-native",
"setupFiles": [
"./__tests__/setup.js"
],
"testRegex": "/__tests__/.*\\.spec\\.js$"
}
๊ฐ์ ๋ฌธ์ ๊ฐ ์์ต๋๋ค. ๋๋ ๋๋ด์ ๋ค์์ ์ถ๊ฐํ๋ค.
"jest": {
"preset": "react-native",
"transformIgnorePatterns": [
"node_modules/(?!react-native|react-navigation)/"
]
}
์ด์ Jest๋ฅผ ์คํํ ๋ ๋ค์ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
ReferenceError: 0์์ "/Users/caalcaz/Documents/Projects/Enterprise-app/node_modules/enzyme/.babelrc"์ ์ง์ ๋ ์ ์ ์๋ ํ๋ฌ๊ทธ์ธ "transform-replace-object-assign", "/Users/caalcaz/๋ฅผ ๊ธฐ์ค์ผ๋ก ํด๊ฒฐํ๋ ค๊ณ ์๋ํ์ต๋๋ค. Documents/Projects/Enterprise-app/node_modules/enzyme"
@calcazar node_modules/enzyme/.babelrc์ ๋ฌด์์ด ์์ต๋๊น?
babel-jest _and_ ts-jest๋ฅผ ์ฌ์ฉํ์ฌ ํด๊ฒฐํ์ต๋๋ค. package.json์์:
"jest": {
"modulePaths": [
"<rootDir>/src",
"<rootDir>/node_modules"
],
"globals": {
"NODE_ENV": "test"
},
"verbose": true,
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json"
],
"transform": {
"^.+\\.jsx?$": "babel-jest",
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"transformIgnorePatterns": ["/node_modules/(?!lodash-es)"], // <-- this allows babel to load only the node modules I need (which is lodash-es) and ignore the rest
"testRegex": "test/.*\\.spec\\.ts$",
"setupFiles": [
"<rootDir>/test/jest-pretest.ts"
],
"testEnvironment": "node",
"moduleNameMapper": {
"aurelia-(.*)": "<rootDir>/node_modules/$1"
},
// some coverage and results processing options
},
๊ทธ๋ฐ ๋ค์ .babelrc์์(ํ ์คํธ์ฉ์ผ๋ก๋ง ์ฌ์ฉ)
{
"env": {
"test": {
"plugins": ["transform-es2015-modules-commonjs"]
}
}
}
๊ทธ๊ฒ์ด ๋๊ตฐ๊ฐ๋ฅผ ๋๊ธฐ๋ฅผ ๋ฐ๋๋๋ค! (์ด๊ฒ์ aurelia ์ฑ์ฉ์ด๋ฏ๋ก YMMV)
@thinkOfaNumber
์ด๊ฒ์ด ๋ด๊ฐ ๋ณด๋ ๊ฒ์ ๋๋ค. ์ฐธ๊ณ ๋ก ์ ๋ Enzyme V3.0์ ์คํ ์ค์ ๋๋ค.
{
"presets": ["airbnb"],
"plugins": [
["transform-replace-object-assign", "object.assign"],
],
}
@thinkOfaNumber ์ ์์ ๊ฒ์
"jest": {
"modulePaths": [
"<rootDir>/src",
"<rootDir>/node_modules"
],
"transformIgnorePatterns": ["/node_modules/(?!lodash-es)"], // <-- this allows babel to load only the node modules I need (which is lodash-es) and ignore the rest
๊ผญ ํ์ํ ๋ถ๋ถ์ ๋๋ค. ์ ๋ง ๊ณ ๋ง์ต๋๋ค!
@rochapablo ๊ฐ์ ๊ณณ. ํ์ด์ด ์๋์?
Noop, ์ง๊ธ์ ์ค๋ฅ๋ฅผ ๋ฐ์์ํค๋ ํน์ ํ์ผ ํ ์คํธ๋ฅผ ๋ฌด์ํ๊ณ ์์ต๋๋ค.
์ํ์. ๋๋ ๊ฐ์ ๋ฌธ์ ๊ฐ ์์ผ๋ฉฐ ์ง๊ธ๊น์ง ์๋ฌด๊ฒ๋ ํด๊ฒฐํ์ง ๋ชปํ๋ ๊ฒ ๊ฐ์ต๋๋ค.
ํ๋ก์ฐ๋
@rochapablo ์ด ๋ฌธ์ ๋ฅผ
๋๋ ๋ํ ๋น์ ์ด ์ธ๊ธ ํ ๋ฌธ์ ๋๋ฌธ์ ์์์ผ๋ก GiftedChat
๊ตฌ์ฑ ์์๋ฅผ ํ
์คํธ ํ ์ ์์ต๋๋ค ...
@todorone , ๋ค์์ ์ถ๊ฐํ์ญ์์ค.
"transformIgnorePatterns": [
"node_modules/(?!react-native-gifted-chat)/"
]
@rochapablo ํํธ๋ฅผ ์ฃผ์
์ ๊ฐ์ฌํ์ง๋ง ๋ถํํ๋ ๋์์ด๋์ง ์์์ต๋๋ค. transformIgnorePatterns
์ ๊ท์์ผ๋ก ๋ ํ๋ ์ดํ๋ ค๊ณ ์๋ํ์ง๋ง ๋์์ด ๋์ง ์์์ต๋๋ค.
"transform": {
"^.+\\.js$": "babel-jest",
"^.+\\.jsx?$": "babel-jest"
},
๋๋ด์ ์์ ๋ด์ฉ์ ์ถ๊ฐํ์ญ์์ค
@rochapablo jest-expo
ํ๋ฆฌ์
ํด๋ณด ์
จ๋์? ํ์ด์ด ์๋์? transformIgnorePatterns
๋ ๋ค๋ฅธ ๊ตฌ๋ฌธ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ต๋๋ค.
์ฌ์ ํ ๋๋ฅผ ์ํด ์๋ํ์ง ์์ต๋๋ค. ๋น์ด๋จน์ ๊ฐ์ ธ์ค๊ธฐ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
@testacode ๋์ import
ํ์ํ babel ํ๋ฌ๊ทธ์ธ์ ์ถ๊ฐํ์ต๋๊น? https://facebook.github.io/jest/docs/en/webpack.html#using -with-webpack-2 ํ๋จ ์ฐธ์กฐ
@SimenB ๊ฐ์ฌํฉ๋๋ค, ๊ทธ๊ฒ์ด ๋ด๊ฐ ๋์น ๊ฒ์ ๋๋ค!
ํฅ๋ฏธ ๋กญ๊ตฐ. ์ผ๋ถ ์ต๊ทผ ์ ๋ฐ์ดํธ๋ babel-jest์ ์๋ ์ถ๊ฐ๋ฅผ ์ ๊ฑฐํ์ต๋๋ค. ๊ทธ๋์ ์คํ :
npm install babel-jest --save-dev
๊ตฌ์ฑ์ NOTHING ELSE๋ฅผ ์ถ๊ฐํ๋ฉด ๋ฌธ์ ๊ฐ ํด๊ฒฐ๋์์ต๋๋ค. ๋ ์ด์ ํฌํจ๋์ง ์๋๋ค๋ ์ค๋ฅ ๋ฉ์์ง๊ฐ ๋ ๋ช ํํด์ผํ๋ค๊ณ ์๊ฐํฉ๋๋ค ...
babel-jest๋ฅผ ์ค์นํ์ฌ ํด๊ฒฐํ์ต๋๋ค.
npm i babel-jest
๋ํ ๋ค์ ๋ด์ฉ์ผ๋ก .babelrc
ํ์ผ์ ๋ง๋๋ ๊ฒ์ ์์ง ๋ง์ญ์์ค.
{
"presets": ["env", "react"]
}
์ด๊ฒ์ ๋๋ฅผ ์ํด ์๋ํฉ๋๋ค.
.babelrc
:
{
"presets": ["env", "stage-3"],
"plugins": ["transform-class-properties"]
}
package.json
:
"scripts": {
"test": "jest --forceExit"
},
"devDependencies": {
"babel-core": "^6.11.4",
"babel-jest": "^21.2.0",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-preset-env": "^1.6.1",
"babel-preset-stage-3": "^6.24.1",
"jest": "^21.2.0"
}
@hqro ๊ทธ๊ฒ์ ๋๋ฅผ ์ํด ์ผํ์ต๋๋ค, ๊ฐ์ฌํฉ๋๋ค ๐
์ ์๊ฒ๋ .babelrc
ํ์ผ์ ๋ค์์ด ์์์ต๋๋ค.
{
"presets": ["react", ["env", { "modules": false }]],
"plugins": [ ... ]
}
๊ทธ๋ฆฌ๊ณ "ํ ์คํธ" ํ๊ฒฝ์ด ๋์ ๋ชจ๋์ ์ฌ์ฉํ๋์ง ํ์ธํ๊ธฐ๋ง ํ๋ฉด ๋ฉ๋๋ค.
{
"presets": ["react", ["env", { "modules": false }]],
"plugins": [ ... ],
"env": {
"test": {
"presets": ["react", "env"],
}
}
}
@nemoDreamer ํ๋์ ๊ตฌ์ฑ์์ ํ๋ฌ๊ทธ์ธ๊ณผ ์ฌ์ ์ค์ ์ ๋ฐ๋ณตํ์ง ์๋ ๊ฒ์ด ์ข์ต๋๋ค
{
"presets": ["react", ["env", { "modules": false }]],
"plugins": [ ... ],
"env": {
"test": {
"plugins": ["transform-es2015-modules-commonjs"],
}
}
}
๋ฌด์์ ์๋ํ๋ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค.
> cross-env NODE_ENV=test jest --no-cache
FAIL src\views\Pay\__tests__\Pay.test.js
โ Test suite failed to run
C:\Users\***\Documents\Project\***\node_modules\ne-rc\lib\index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import _Alert from './Alert';
^^^^^^
SyntaxError: Unexpected token import
11 | * <strong i="6">@param</strong> {Array<Object>} [data] ๆฏไธ็งๅๆ็่ฏฆ็ปไฟกๆฏ data=[{},{}...]
12 | * <strong i="7">@param</strong> {Number} [defaultSelected] ้ป่ฎค้ไธญ็ไธๆ
> 13 | * <strong i="8">@param</strong> {Funtion} [onClick] ็นๅป่งฆๅไบไปถ
14 | * <strong i="9">@param</strong> {Number} [columnNum] ไธ่กๆๅคๅฐๅ
15 | *
16 | * <strong i="10">@example</strong>
at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:316:17)
at Object.<anonymous> (src/components/Installment/Installment.jsx:13:13)
at Object.<anonymous> (src/components/Installment/index.js:8:20)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 3.693s
์ฌ๊ธฐ ๋ด ๊ตฌ์ฑ ํ์ผ์ด ์์ต๋๋ค.
* .babelrc *
{
"presets": [
[
"env",
{
"modules": false
}
],
// webpack understands the native import syntax, and uses it for tree shaking
"es2015",
"stage-2",
// Specifies what level of language features to doPay.
// Stage 2 is "draft", 4 is finished, 0 is strawman.
// See https://tc39.github.io/process-document/
"react"
// Transpile React components to JavaScript
],
"env": {
"test": {
"presets": ["env", "react", "stage-2"],
"plugins": [
"transform-es2015-modules-commonjs"
]
}
}
}
ํจํค์ง.json
{
"main": "index.js",
"scripts": {
"dev": "cross-env NODE_ENV=development node ./server/server.js",
"api": "cross-env NODE_ENV=api node ./server/server.js",
"clean": "rm -rf dist",
"dist": "npm run clean && cross-env NODE_ENV=production webpack --config ./webpack/index.js --progress --colors",
"online": "cross-env NODE_ENV=development node ./server/online.js",
"lint": "node ./node_modules/eslint/bin/eslint.js ./src --fix",
"test": "cross-env NODE_ENV=test jest --no-cache",
"test:watch": "npm test --watch",
"stats": "npm run clean && cross-env NODE_ENV=production webpack --config ./webpack/index.js --profile --json > stats.json",
"gatest": "node ./service/gatest.js"
},
"pre-commit": [
"lint"
],
"jest": {
"verbose": true,
"modulePaths": [
"<rootDir>/src",
"<rootDir>/node_modules"
],
"setupFiles": [
"./test/setup.js"
],
"moduleFileExtensions": [
"js",
"jsx",
"json",
"md"
],
"moduleDirectories": [
"node_modules"
],
"testMatch": [
"<rootDir>/src/**/__tests__/**/*.{js,jsx,mjs}",
"<rootDir>/src/**/?(*.)(spec|test).{js,jsx,mjs}"
],
"transform": {
"^.+\\.(js|jsx|mjs)$": "<rootDir>/node_modules/babel-jest",
"^.+\\.css$": "<rootDir>/config/jest/cssTransform.js",
"^(?!.*\\.(js|jsx|mjs|css|json)$)": "<rootDir>/config/jest/fileTransform.js"
},
"testEnvironment": "jsdom"
},
"devDependencies": {
"asset-inject-html-webpack-plugin": "^0.9.1",
"autoprefixer": "^7.1.3",
"babel": "^6.23.0",
"babel-core": "^6.26.0",
"babel-eslint": "^8.2.1",
"babel-jest": "^22.4.1",
"babel-loader": "^7.1.2",
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.0",
"babel-preset-env": "^1.6.1",
"babel-preset-es2015": "^6.24.1",
"babel-preset-jest": "^22.4.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",
"babel-register": "^6.26.0",
"better-npm-run": "0.1.0",
"body-parser": "^1.17.2",
"chalk": "^2.1.0",
"cross-env": "^5.0.5",
"css-loader": "^0.28.5",
"deepmerge": "^2.0.1",
"enzyme": "^3.3.0",
"eslint": "^4.5.0",
"eslint-config-standard": "^11.0.0-beta.0",
"eslint-config-standard-jsx": "^4.0.2",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-node": "^5.1.1",
"eslint-plugin-promise": "^3.5.0",
"eslint-plugin-react": "^7.3.0",
"eslint-plugin-standard": "^3.0.1",
"exports-loader": "^0.6.4",
"express": "^4.15.4",
"extract-text-webpack-plugin": "^3.0.0",
"file-loader": "^1.1.6",
"git-repo-info": "^1.4.1",
"glob": "^7.1.2",
"happypack": "^4.0.1",
"html-loader": "^0.5.1",
"html-webpack-plugin": "^2.30.1",
"idb": "^2.0.4",
"imports-loader": "^0.7.0",
"jest": "^22.4.2",
"lodash.range": "^3.2.0",
"mocha": "^5.0.0",
"node-sass": "^4.5.3",
"node-xlsx": "^0.11.2",
"postcss-loader": "^2.0.6",
"pre-commit": "^1.2.2",
"react-hot-loader": "^3.0.0-beta.6",
"react-test-renderer": "^16.2.0",
"redux-logger": "^3.0.6",
"regenerator-runtime": "^0.11.1",
"request": "^2.74.0",
"require-reload": "^0.2.2",
"rimraf": "^2.6.0",
"sass-loader": "^6.0.6",
"script-loader": "^0.7.0",
"sinon": "^4.2.0",
"style-loader": "^0.19.1",
"uglify-js": "^3.0.28",
"url-loader": "^0.6.2",
"webpack": "^3.5.5",
"webpack-bundle-analyzer": "^2.9.0",
"webpack-dev-middleware": "^2.0.4",
"webpack-dev-server": "^2.7.1",
"webpack-hot-middleware": "^2.18.2",
"webpack-merge": "^4.1.0",
"worker-loader": "^1.1.0"
}
}
.babelrc
์์ ์ฌ์ ์ค์ ์ ์ค์ ํ ๋ ๋ค์๊ณผ ๊ฐ์ด "env"
๊ฐ ๋๊ดํธ ["env"]
์ฌ์ด์ ์๋์ง ํ์ธํ์ญ์์ค.
"env": {
"test": {
"presets": [["env"], "stage-2"],
"plugins": ["transform-es2015-modules-commonjs", "dynamic-import-node"]
}
}
ํนํ ๊ตฌ์ฑ์ "modules: false"
์๋ ๊ฒฝ์ฐ.
๋ํ ๊ธฐ๋ณธ์ ์ผ๋ก jest๋ node_modules
ํ์ผ์ ๋ณํํ์ง ์์ต๋๋ค . cf https://facebook.github.io/jest/docs/en/configuration.html#transformignorepatterns -array-string
์ด๊ฒ์ด import ...
๊ฐ node_modules
ํ์ผ์์ ์๋ํ์ง ์๋ ์ด์ ์
๋๋ค. ์ด๊ฒ์ ๋นํ์ฑํํ๋ ค๋ฉด ๋ค์์ ์ถ๊ฐํ ์ ์์ต๋๋ค.
transformIgnorePatterns: []
๋น์ ์ ๋๋ด ๊ตฌ์ฑ์.
๋์์ด ๋์๊ธฐ๋ฅผ ๋ฐ๋๋๋ค!
@florentchauveau ๊ฐ์ฌํฉ๋๋ค. ์ด ์์ ์ด ์ ์๊ฒ ํจ๊ณผ์ ์ ๋๋ค.
@todorone @rochapablo ์ด๊ฒ์ ๋ํ ํด๊ฒฐ์ฑ ์ ์ฐพ์ผ์ จ์ต๋๊น? ๋๋ ๊ฐ์ ๋ฌธ์ ์ ์ง๋ฉดํด ์๋ค-
import * as React from 'react';
^^^^^^
SyntaxError: Unexpected token import
at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:316:17)
at Object.<anonymous> (node_modules/@expo/react-native-action-sheet/index.js:1:107)
๋ค์์ ์ฌ์ฉํ๋ ๊ตฌ์ฑ ์์ ํ
์คํธ์์๋ง ์ด ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
GiftedChat
- https://github.com/FaridSafi/react-native-gifted-chat.
@expo/react-native-action-sheet
๊ฐ ES6 ๋ชจ๋๋ก ๋ฐฐ์ก๋๊ณ ์๊ณ GiftedChat
๊ฐ ๋ด๋ถ์ ์ผ๋ก ์ฌ์ฉํ๊ณ ์๊ธฐ ๋๋ฌธ์ผ๊น์? ๊ทธ๋ฌ๋ jest
/ ts-jest
(ํ์คํ์ง ์์) ์ฌ์ ์ค์ ์ด ์ถ๊ฐ๋๋ฉด ์ฒ๋ฆฌํด์ผ ํฉ๋๋ค. ์ฌ๊ธฐ ๋ด Jest ๊ตฌ์ฑ์ด ์์ต๋๋ค -
"jest": {
"preset": "react-native",
"moduleFileExtensions": [
"ts",
"tsx",
"js"
],
"transform": {
"^.+\\.(js)$": "<rootDir>/node_modules/babel-jest",
"\\.(ts|tsx)$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"transformIgnorePatterns": [
"node_modules/(?!rn-placeholder|react-native|react-navigation|native-base|react-native-datepicker|native-base-shoutem-theme|@shoutem/animation|@shoutem/ui|tcomb-form-native)"
],
"setupTestFrameworkScriptFile": "<rootDir>/src/__mocks__/Adapter.js",
"testRegex": "(\\.(test|spec))\\.(ts|tsx|js)$",
"testPathIgnorePatterns": [
"\\.snap$",
"<rootDir>/node_modules/",
"<rootDir>/lib/"
],
"moduleNameMapper": {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/src/__mocks__/fileMock.js"
},
"cacheDirectory": ".jest/cache"
},
๊ทธ๋ฆฌ๊ณ .babelrc
-
{
"presets": ["react-native"]
}
์์์ ์ธ๊ธํ ๊ฑฐ์ ๋ชจ๋ ์๋ฃจ์ ์ ์๋ํ์ต๋๋ค. ๊ทธ๋ค ์ค ๋๊ตฌ๋ ๋ด ์๋๋ฆฌ์ค์์ ์๋ํ์ง ์์์ต๋๋ค. ๋์์ด ํ์ํ.
@omkarjoshi-cc ๋ด๊ฐ ์์ ํ๊ณ ์๋ ์๊ฐ์ ๋๋ฅผ ์ํด ์ผํ ์ ์ผํ ๊ฒ์ ์ค๋ฅ๋ฅผ ๋ฐ์์ํค๋ ํจํค์ง๋ฅผ ์ถ๊ฐํ๋ ๊ฒ์ด์์ต๋๋ค. ์ ๊ฒฝ์ฐ์๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
"transformIgnorePatterns": [
"node_modules/(?!react-native-gifted-chat)/"
]
@rochapablo ๋ค. ๋๋ ๊ทธ๊ฒ์ ์๋ํ๋ค. ์๋ํ์ง ์์์ต๋๋ค. ๋์น ์ ์๋ ๋ค๋ฅธ ๊ฒ์ด ์์ต๋๊น? ์ด๊ฑฐ ์ ๋ง ๋ต๋ตํฉ๋๋ค ๐
๋๋ babel 7์ ์ฌ์ฉํ๊ณ ์์ผ๋ฉฐ ๋๋ ์ฌ์ ํ "transformIgnorePatterns"์ ์ด์ด ์์์ต๋๋ค - ๋ง์ ๋ค๋ฅธ ์ ๊ท ํํ์์ ์๋ํ์ต๋๋ค :/
๋ด๊ฐ ๊ฐ์ง ๋ฌธ์ ๋ ํ๋์ ์ข ์์ฑ์ ๊ธฐ๋ณธ es6 ํด๋์ค๊ฐ ์๊ณ ๋ด ์์ค์ ์ด๋ฅผ ํ์ฅํ๋ ํด๋์ค๊ฐ ์๋ค๋ ๊ฒ์ ๋๋ค. Babel์ ์ํด ํธ๋์คํ์ผ๋ "es5 ํด๋์ค"๋ ๋ค์ดํฐ๋ธ ํด๋์ค๋ฅผ ํ์ฅํ ์ ์์ผ๋ฏ๋ก ๋๋ด์ผ๋ก ๋ฐํ์ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
์ด๋ป๊ฒ๋ "babel/preset-env"๊ฐ ์ฝ๋๊ฐ jest(๋์ ๋ ธ๋)๋ก ๋ณํ๋๋ ๋ฐฉ์์ ๋ฐฉํดํ๊ณ ์๋ค๊ณ ์๊ฐํ๊ธฐ ์์ํ์ต๋๋ค. ์ด๋ ๋ธ๋ผ์ฐ์ ๋์์ ๋ฐ๋ฅด์ง ์์ ์ ์์ง๋ง ์ฌ์ ํ ํ์คํ์ง ์์ต๋๋ค.
.babelrc์์ 'present-env'๋ฅผ ์ฌ์ฉํ ๋ ๋๋ด์ ์ํด ๋ชจ๋ ๊ฒ์ es5๋ก ๊ฐ์ ํ๋ ๋ฐฉ๋ฒ์ ์๋ ์ฌ๋์ด ์์ต๋๊น?
๋ด๊ฐ ์๊ฐํ ์ ์๋ ์ ์ผํ ํด๊ฒฐ ๋ฐฉ๋ฒ์ babel-jest๋ฅผ ์ฌ์ฉํ์ง ์๊ณ webpack(babel-loader๋ฅผ ์ฌ์ฉํ๊ณ ๋ณํํ ํ์ผ์ด ์ ์๊ฒ ์ ํฉํจ)์ ์คํํ ๋ค์ jest๋ฅผ ์คํํ๋ ๊ฒ์
๋๋ค. -> webpack -w
์ฌ์ฉ์ ๋ํด ์๊ฐํ๊ณ ์์ต๋๋ค.
์ฒจ๊ฐ
"transformIgnorePatterns": [
"node_modules/(?!react-native-gifted-chat|@expo/react-native-action-sheet)"
]
๋๋ฅผ ์ํด ์ผํ๋ค. @rochapablo ๋๋ถ์ ๐
์ด ๋ฌธ์ ๋ก ์ด๋ ค์์ ๊ฒช๊ณ ์์ต๋๋ค. ํ
์คํธ ์ค์ธ ๋ด ์ถ๋ ฅ ํด๋๊ฐ import
๋ฑ์ ES6 ๊ตฌ๋ฌธ์ด์ด์ผ ํฉ๋๊น ์๋๋ฉด ํ
์คํธํ ์ ์์ต๋๊น? ๋ด ์ถ๋ ฅ ํ์ผ/ํ์ผ์ commonjs๊ฐ ํ์๋์ง ์์ต๋๋ค(react-native์ ํจ๊ป ์ฌ์ฉ).
๋๋ ๊ฐ์ ๋ฌธ์ ์ ์ง๋ฉดํ๊ณ ์์ผ๋ฉฐ์ด ํ์ด์ง๋ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํฉ๋๋ค.
https://babeljs.io/docs/plugins/transform-es2015-modules-commonjs/
์ด๋ค ์ด์ ๋ก babel-jest
๋์ ๋ค์์ ์ฌ์ฉํ๋ฉด ์ ์๊ฒ ํจ๊ณผ์ ์ด์์ต๋๋ค ( transformIgnorePatterns
๋ถ๋ช
ํ ์กด์ค)
"transform": {
"^.+\\.(js|jsx|ts|tsx)$": "ts-jest-babel-7"
}
btw ์ด ๋ฌธ์ ๋ ์ฌ๊ธฐ์์ ๋ค์ ์ด์ด์ผ ํฉ๋๊น? ์ด๋ ค์์ ๊ฒช๋ ๋ง์ ์ฌ๋๋ค
์ ๊ฒฝ์ฐ์๋ ์ด ๋ฌธ์ ๊ฐ ๋ด babel ๊ตฌ์ฑ์์ ํ
์คํธ ํ์ผ์ ๋ฌด์ํ์ฌ ๋ฐ์ํ ๊ฒ์ผ๋ก ๋ํ๋ฌ์ต๋๋ค. ๊ด๋ จ ์๋ ์ด์ ๋ก ๋ด .babelrc์ "ignore": ["**/*.test.js"]
๊ฐ ์์์ต๋๋ค.
์๋ฃจ์ ์ ํ๋ก๋์ ํ๊ฒฝ์ ๋ํด์๋ง ๋ฌด์ ์ต์ ์ ์ค์ ํ๋ ๊ฒ์ด์์ต๋๋ค. ์:
"env": {
"production": {
"ignore": [
"**/*.test.js"
]
}
}
๋๋ ํ ์คํธ ํ๊ฒฝ์ ๋ํด ํน๋ณํ ๋ฌด์ ํจํด์ ์ง์ ํ ์ ์์ต๋๋ค.
๋ง์ด ์๋ํ ํ ๋ด ๊ฒฝ์ฐ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
์ฌ๊ธฐ ๋ด ๊ตฌ์ฑ์ด ์์ต๋๋ค.
.babelrc
{
"plugins": [
"transform-es2015-modules-commonjs",
"transform-class-properties"
]}
ํจ์์ฉ(setupTests.js)
import { configure } from "enzyme";
import Adapter from "enzyme-adapter-react-15";
configure({ adapter: new Adapter() });
ํจํค์ง.json
"dependencies": {
"babel-cli": "^6.26.0",
"babel-plugin-transform-class-properties": "^6.24.1",
"eslint": "^4.19.1",
"react": "^15.4.2",
"react-dom": "^15.4.2",
"react-test-renderer": "^15.6.2",
"babel-jest": "^22.4.3",
"babel-preset-env": "^1.6.1",
"babel-preset-react": "^6.24.1",
"enzyme": "^3.3.0",
"enzyme-adapter-react-15": "^1.0.5",
"gh-pages": "^0.12.0",
"jest": "^22.4.3",
"react-addons-test-utils": "^15.4.2",
"regenerator-runtime": "^0.11.1"
},
"jest": {
"globals": {
"NODE_ENV": "test"
},
"verbose": true,
"moduleDirectories": ["node_modules", "src"],
"transform": {
"^.+\\.jsx?$": "babel-jest"
},
"moduleFileExtensions": ["js", "jsx"],
"setupTestFrameworkScriptFile": "<rootDir>/src/setupTests.js"
}
yarn add --dev babel-jest babel-core regenerator-runtime
์ด๊ฒ์ ์ถ๊ฐํ๋ฉด ์ ์๊ฒ ํจ๊ณผ์ ์ด์์ต๋๋ค.
์ ๊ฒฝ์ฐ์๋ Windows ๋ฐ ์ด๋ฆฌ์์ ๊ถํ์ ๋ฌธ์ ๊ฐ ์์๊ณ ์๋ง๋ env ๋ณ์๋ฅผ ์ฌ๋ฐ๋ฅด๊ฒ ์ค์ ํ์ง ์์์ ๊ฒ์
๋๋ค.
git bash์์ ์์น๋ cmd.exe
ํ๊ณ ๋ค์ ์์
์ ์์ํ์ต๋๋ค.
๋๋์ด jest
๋ก ์ ์ ๋ script
(์ด๋ฆ test:jest
๋ก test
) ๋ด์์, package.json
. ๊ทธ๋ฌ๋ npm run test
์คํํ ๋ SyntaxError: Unexpected token import
์ป์ต๋๋ค.
root<strong i="14">@browserify</strong>:/var/machine-learning/src# npm run test
> [email protected] test /var/machine-learning/src
> jest --config /var/machine-learning/test/jest/jest.config.js
FAIL ../test/jest/__tests__/layout/analysis.test.jsx
โ Test suite failed to run
/var/machine-learning/test/jest/__tests__/layout/analysis.test.jsx:6
import React from 'react';
^^^^^^
SyntaxError: Unexpected token import
at ScriptTransformer._transformAndBuildScript (../../src/node_modules/jest-runtime/build/script_transformer.js:316:17)
FAIL ../test/jest/__tests__/layout/page.test.jsx
โ Test suite failed to run
/var/machine-learning/test/jest/__tests__/layout/page.test.jsx:6
import React from 'react';
^^^^^^
SyntaxError: Unexpected token import
at ScriptTransformer._transformAndBuildScript (../../src/node_modules/jest-runtime/build/script_transformer.js:316:17)
FAIL ../test/jest/__tests__/content/register.test.jsx
โ Test suite failed to run
/var/machine-learning/test/jest/__tests__/content/register.test.jsx:6
import React from 'react';
^^^^^^
SyntaxError: Unexpected token import
at ScriptTransformer._transformAndBuildScript (../../src/node_modules/jest-runtime/build/script_transformer.js:316:17)
FAIL ../test/jest/__tests__/content/login.test.jsx
โ Test suite failed to run
/var/machine-learning/test/jest/__tests__/content/login.test.jsx:6
import React from 'react';
^^^^^^
SyntaxError: Unexpected token import
at ScriptTransformer._transformAndBuildScript (../../src/node_modules/jest-runtime/build/script_transformer.js:316:17)
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | 0 | 0 | 0 | 0 | |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 4 failed, 4 total
Tests: 0 total
Snapshots: 0 total
Time: 3.782s
Ran all test suites.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] test: `jest --config /var/machine-learning/test/jest/jest.config.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2018-05-08T01_50_42_754Z-debug.log
๋ํ ํด๋น jest.config.js
์ ์ํ์ต๋๋ค.
module.exports = {
'verbose': true,
'collectCoverage': true,
'coverageDirectory': '/var/machine-learning',
'moduleDirectories': ['/var/machine-learning/src/node_modules'],
'transformIgnorePatterns': [
'/var/machine-learning/src/node_modules'
]
}
react-native
๋ฐ transformIgnorePatterns
๋ํด ๋ง์ ๊ฒ์ ๋ณด๊ณ ์์ต๋๋ค. ์ด ์ข๊ฒ๋ ๋ถํํ๋ ๋๋ ๊ทธ๊ฒ์ ์ฌ์ฉํ์ง ์์ต๋๋ค. ์ด ์ผ๋ง๋ ์ดํดํ๊ธฐ ํ๋ ๋ฒ๊ทธ์
๋๋ค. ๋ด๊ฐ ์ ์ํด์ผํฉ๋๊น .babelrc
, ๋๋ ์ถ๊ฐ jest
๋ด์์ ์ง์์ด๋ฅผ package.json
. ์ด ๊ฒฝ์ฐ ๋ด ๋๋ด script
์ด babel์ ๋ํด ์ด๋ป๊ฒ ์ ์ ์์ต๋๊น? ๋๋ package.json
ํฌํจํ๋ ๋์ผํ ๋๋ ํ ๋ฆฌ์ .babelrc
๋ฅผ ์ถ๊ฐํ๋ ค๊ณ ์๋ํ์ง๋ง ์ด์ด ์์์ต๋๋ค.
{
"env": {
"test": {
"presets": ["env", "stage-2", "react"]
}
}
}
์ฐธ๊ณ : ์์ ๋ฌธ์ ์ ๊ด๋ จํ์ฌ ๋ด ์๋ ๋ฌธ์ ๋ฅผ ์ถ์ ํ๋ ค๋ ์ฌ๋์ด ์๋ ๊ฒฝ์ฐ.
๋ง์ ์์์ ๋์ ๋ค์๊ณผ ๊ฐ์ ๋ฌธ์ ๋ฅผ ๋ฐ๊ฒฌํ์ต๋๋ค.
.babelrc.js
ํ์ผ์ ์ฐพ์ง ์์ต๋๋ค. .babelrc
ํ์ผ์๋ง ํด๋น๋ฉ๋๋ค. JSON ๋์ JS ๊ตฌ์ฑ์ด ์๋ ๊ฒฝ์ฐ ๊ตฌ์ฑ์ ์ฝ์ง ์์ผ๋ฏ๋ก ํ๋ฌ๊ทธ์ธ์ ์ฌ์ฉํ์ง ์์ต๋๋ค. ๊ฒฐ๊ณผ์ ์ผ๋ก unexpected token: import
๋์์ต๋๋ค.babel-7-jest
๋ ์ด์ Babel 7 ์ฝ์ด ์์ง์์ ์คํ๋๋ฏ๋ก ์๋ก์ด ์ ์ ํ๋ฌ๊ทธ์ธ์ ๋ํ ์ง์์ด ์ค๋จ๋ฉ๋๋ค.npm i -D [email protected]
๋ฌธ์ ๋ฅผ ์์ ํ ์์ ํ ๊ฒ ๊ฐ์ต๋๋ค. ๋ด babel ๊ตฌ์ฑ์ env.test
๋ ์๊ณ jest ๊ตฌ์ฑ๋ ์์ต๋๋ค.Jest๊ฐ babel-core
(๋ฐ๋ผ์ @babel/core
๋ฌด์)๋ฅผ ์ฐพ๋ ๊ฒ์ฒ๋ผ ๋ณด์ด์ง๋ง ๊ทธ๋ ์ง ์์ผ๋ฉด Babel 6์ผ๋ก ํด๋ฐฑํ๊ณ ๊ตฌ์ฑ์ ๊ฑด๋๋๋๋ค. ๊ทธ๋ฌ๋ ์ต์ babel-core(๋ธ๋ฆฌ์ง ๋ฒ์ )๊ฐ ์ค์น๋๋ฉด ์ถ๊ฐ ๊ตฌ์ฑ ์์ด ES6 ๋ชจ๋์ ๊ตฌ๋ฌธ ๋ถ์ํฉ๋๋ค.
kasvtv ๊ฐ์ฌํฉ๋๋ค! ๋ด .babelrc.js
๋ฅผ .babelrc
ํ๊ณ npm i -D [email protected]
. ์ด์ ๋ด ํ
์คํธ๋ babel-jest
์คํ๋ฉ๋๋ค.
.js ๊ตฌ์ฑ ํ์ผ์ ์์ ํ๊ฒ ์ฌ์ฉํ ์ ์์ต๋๋ค. Babel์ ๋ฒ์ 7๋ถํฐ ์ด๋ฌํ ์ ํ์ ํ์ผ๋ง ์ฝ๊ณ Jest๊ฐ Babel ๋ฒ์ 7์ ์ฌ์ฉํ๋ ค๋ฉด [email protected] ์ด ํ์ํฉ๋๋ค.
๊ด์ฌ์๋ ์ฌ๋์ด ์์ผ๋ฉด ๋๋ด + ํจ์ ํ ์คํธ๊ฐ ์๋ํฉ๋๋ค. ํด๋น ๊ตฌ์ฑ ํ์ผ์ ์์ ๋กญ๊ฒ ์ฐ๋ฌ๋ณด์ธ์. ๋ํ ๋์ผํ ํ๋ก์ ํธ์ ํ๋ก ํธ์๋ ๊ฐ๋ฐ์ ๋๋ ๋ฐ ๊ด์ฌ์ด ์๋ ์ฌ๋์ด ์์ต๋๊น?
antd
๋ฐ rc-calendar
๋ฑ์ผ๋ก ์ธํด ์ ์ฌํ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ต๋๋ค. ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ๋ชจ๋์ ๊ฐ์ ธ์ค์ง ๋ชปํ๋ ๊ฒฝ์ฐ ํด๊ฒฐ ๋ฐฉ๋ฒ์ transformIgnorePatterns
transformIgnorePatterns: [
'node_modules/?!(antd|rc-.+)/'
],
moduleDirectories
๋์์ด ๋์์ ์ ์์ต๋๋ค.
moduleDirectories: ['node_modules'],
์ต์ ๋ฐ์ ๋ค์ดํฐ๋ธ ๋ฒ์ ์ผ๋ก ์ ๋ฐ์ดํธํ๊ณ babel 7๋ก ์ ๋ฐ์ดํธํด์ผ ํ๋๋ฐ ์ด์ ๋์ผํ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
์ด node_modules๋ฅผ transformIgnorePatterns
ํด๋ ๋ ์ด์ ์๋ฌด ๊ฒ๋ ํ์ง ์๋ ๊ฒ ๊ฐ์ต๋๋ค.
์ ๊ฒฝ์ฐ์๋ ๋ ์ด์ Babel์ ์ฌ์ฉํ์ฌ ๋ชจ๋์ ๋ณํํ์ง ์๊ณ , babelrc์ test
๋ถ๋ถ์ ๋ค์ ์ถ๊ฐํ๋ ๋ฐฉ๋ฒ์ ๋ํ ์์ ๊ฐ์ด๋์ ๋ํ ๋ฉ๋ชจ๊ฐ ์๊ธฐ ๋๋ฌธ์
๋๋ค.
@kasvtv @willb335 ๋๋ Babel 6์ด .babelrc.js๋ฅผ ์ธ์ํ๋๋ก ํ๋ ํธ๋ฆญ์ ์ฌ์ฉํฉ๋๋ค:
ํจํค์ง.json
"babel": {
"presets": [
"./.babelrc.js"
]
},
@material/base ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ฌ ๋์ผํ ๋ฌธ์ ๋ฅผ ๋ง๋ฌ์ต๋๋ค. ๋๊ตฌ๋ ์ง package.json
& .babelrc
ํ์ผ์ ์ ๊ณตํ์ฌ ํด๊ฒฐํ ์ ์์ต๋๊น? ์์ ํผ๋๋ฐฑ์ ์๋ํ์ง๋ง ํผ๋๋์์ต๋๋ค ...
๋๋ฅผ ์ํ ํด๊ฒฐ์ฑ
์ .babelrc ํ์ผ์ "env" ์ฌ์ ์ค์ ์ ์ถ๊ฐํ๋ ๊ฒ์ด์ด์ ๋ค์๊ณผ ๊ฐ์ด ๋ฐํ์ก์ต๋๋ค.
{
"์ฌ์ ์ค์ ": ["env", "es2016", "stage-0", "๋ฐ์"]
}
์ด ๋งํฌ ์ ๋ณด๋ฅผ ์ฌ์ฉํ์ฌ ๋ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ์ต๋๋ค.
https://jestjs.io/docs/en/webpack.html
@negarineh ๋งํฌ๊ฐ ์๋ํ์ง ์์ต๋๋ค
@haywirez ๊ฐ ์ ๊ณตํ๋ transformIgnorePatterns
์๋ฃจ์
์ ์ฌ์ฉํ๋ฉด
SyntaxError: Unexpected token import
์ค๋ฅ
TypeError: Cannot read property 'DocumentDir' of undefined
์ค๋ฅ. ์ฆ, ํจํค์ง(rn-fetch-blob)๊ฐ ์ด์ ์ ์๋์ง ์์์ต๋๋ค.
ํธ์งํ๋ค:
์ค์ ๋ก ๋จ์ง ์ถ๊ฐ transformIgnorePatterns: [],
๋ด์ jest.config.js
ํ์ ์ค๋ฅ์ CHAGE์ ๊ตฌ๋ฌธ ์๋ฌ๊ฐ ๋ฐ์ํฉ๋๋ค.
@schhumannd ์ฃ์กํฉ๋๋ค ์ ๊ฐ ์ค์๋ฅผ ์ ์ง๋ ์ต๋๋ค. ๋งํฌ๊ฐ ํ์ฌ ํ์ด์ง๋ก ๋ฆฌ๋๋ ์
์ค์
๋๋ค. ๋งํฌ์ ์ ๊ณต๋ ๊ตฌ์ฑ์ผ๋ก "jest.config.js"๋ฅผ ์ค์ ํ๋ฉด ์๋ํฉ๋๋ค.
์ค๋ฅ๊ฐ ๋งํฌ์ ์์ ์ ๊ฐ์ ๋ชจ์ ํ์ผ์ ์ถ๊ฐํ๊ฑฐ๋ ์ด ๊ตฌ์ฑ์ผ๋ก "identity-obj-proxy"๋ฅผ ์ค์นํ์ฌ .css ํ์ผ์ ๊ฐ์ ธ์ค๋ ๊ฒ๊ณผ ๊ด๋ จ๋ ๊ฒฝ์ฐ ์๋ํด์ผ ํฉ๋๋ค.
npm install --save-dev identity-obj-proxy
jest.config.js(CSS ๋ชจ๋์ฉ):
{
"jest": {
"moduleNameMapper": {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js",
"\\.(css|less)$": "identity-obj-proxy"
}
}
}
๋ํ ์ด๊ฒ์ ํ๋ก์ ํธ์ ๋ฃจํธ ๋๋ ํฐ๋ฆฌ์ ์๋ ๋ด .babelrc ํ์ผ์ ๋๋ค( test.config.js๋ ๋ฃจํธ์ ์์ด์ผ ํจ).
๋จผ์ babel์ ์ฌ์ฉํ๋ ค๋ฉด "babel-jest" ๋ฐ "regenerator-runtime"์ ์ค์นํ์ญ์์ค.
npm --dev babel-jest babel-core regenerator-runtime
๊ทธ๋ฐ ๋ค์ ์ด ๊ตฌ์ฑ์ .babelrc ํ์ผ์ ์ถ๊ฐํฉ๋๋ค.
{
"presets": ["env", "react"],
"plugins": ["transform-class-properties"],
"env": {
"production": {
"plugins": ["transform-es2015-modules-commonjs"]
},
"test": {
"presets": ["env", "react"],
"plugins": ["transform-export-extensions"],
"only": [
"./**/*.js",
"node_modules/jest-runtime"
]
}
}
}
๋์์ด ๋์๊ธฐ๋ฅผ ๋ฐ๋๋๋ค.
https://jestjs.io/docs/en/webpack.html
์ ๊ฒฝ์ฐ์๋ ๋ค์ .babelrc
์ถ๊ฐํ ํ ์๋ํฉ๋๋ค.
{
"presets": ["env", "react"],
"env": {
"test": {
"plugins": ["transform-class-properties"]
}
}
}
๊ทธ๋ฐ ๋ค์ NODE_ENV=test jest
์ฌ์ฉํ์ฌ ํ
์คํธ๋ฅผ ์คํํฉ๋๋ค.
์ด๊ฒ์ ์ผ๋ถ ์ฌ๋๋ค์๊ฒ๋ง ๋์์ด ๋ ๊ฒ์ ๋๋ค.
๋ด ๋ฌธ์ ๋ ๋๋ด์ด ๋ด JS ํ์ผ์ ๊ตฌ๋ฌธ ๋ถ์ํ ์ ์๊ธฐ ๋๋ฌธ์
๋๋ค. ๋์ require
์ฌ์ฉํ์ต๋๋ค.
์๋ค ์. babelrc ํ์ผ์ด ์๊ธฐ ๋๋ฌธ์ ์ด๋ฌํ ์๋ฃจ์ ์ค ์ด๋ ๊ฒ๋ ์๋ํ์ง ์์ต๋๋ค. babel.config.js๋ง ์์ต๋๋ค. babel.config.js๋ก ์ด๋ป๊ฒ ์์ ํฉ๋๊น? ๊ฐ์ฌ ํด์
babel
๊ตฌ์ฑ์ ๋ํ ์์ ํ ๋ก ๊ณผ ๋ค๋ฅธ ๊ฒ์์ ๋ช ๊ฐ์ง ์ถ๊ฐ ์ต์
(์: esm
์ต์
)์์ ๋ง์ ๊ฒ์ ์๋ํ์ง๋ง ์๋ฌด ๊ฒ๋ ์๋ํ์ง ์์์ต๋๋ค. import ํค์๋๊ฐ ๊ณ์ํด์ jest์์ ์ค๋ฅ๋ฅผ ์ ๊ณตํ์ต๋๋ค. es6 ๊ฐ์ ธ์ค๊ธฐ๋ก ๋ชจ๋์ ํ
์คํธํ๋ ๊ฒ์ด ๋๋ฌด ์ด๋ ต๋ค๋ ๊ฒ์ ์ฌํ ์ผ์
๋๋ค.
๋ง์ง๋ง์ผ๋ก ์ด๊ฒ์ด ๋๋ฅผ ์ํด ์ผํ ๊ฒ์ ๋๋ค : ts-jest
์ด์ ๋๋ ๋ด ์ฝ๋์์ ์ด๋ค typescript๋ ์ฌ์ฉํ์ง ์์ง๋ง ts-jest
๋ชจ๋ ๋์ผํ๊ฒ ์ฌ์ฉํ์ต๋๋ค(typescript๋ ๋ด๋ถ์ ์ผ๋ก ๊ฐ์ ธ์ค๊ธฐ ๋ฐ ๊ธฐํ ๋ชจ๋ ๊ฒ์ ์ง์ํ๊ธฐ ๋๋ฌธ์). ์ด๊ฒ์ ๋จ์ง ๋๋ด์ด ์๋ฐ์คํฌ๋ฆฝํธ ์ฝ๋๋ฅผ ์ฌ๋ฐ๋ฅด๊ฒ ์ดํดํ๋๋ก ํ๊ธฐ ์ํ ๊ฒ์
๋๋ค.
๋ค์ ๋จ๊ณ๋ฅผ ์ํํ์ต๋๋ค.
npm i -D jest typescript ts-jest @types/jest
jest.config.js
ํ์ผ์์:
transform: {
"\\.js$": "ts-jest",
transformIgnorePatterns: [],
},
Jest๋ js
ํ์ผ์ด ์ปดํ์ผ๋๋ ๊ฒ์ ํ์ฉํ์ง ์๋ typescript์ ๋ํด ๋ถํํ ์ ์์ต๋๋ค. ์๋ ์ต์
์ ์ฌ์ฉํ์ฌ tsconfig.json
ํ์ผ์ ์ถ๊ฐํฉ๋๋ค.
{
"compilerOptions": {
"allowJs": true,
"module": "commonjs",
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"sourceMap": true
}
}
๋ชจ๋ babel
๊ด๋ จ ๊ตฌ์ฑ ๋ฐ ํ์ผ์ ์ ๊ฑฐํ์ต๋๋ค. npm
๊ตฌ์ฑ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
"scripts": {
"test": "jest --runInBand --detectOpenHandles --no-cache --config ./jest.config.js"
}
์์
ํ๊ณผ ๊ด๋ จํ์ฌ ๋น์ทํ ๋ฌธ์ ์ ์ง๋ฉดํ ์ฌ๋์๊ฒ ๋์์ด ๋๊ธธ ๋ฐ๋๋๋ค.
๋๊ตฌ๋ ์ง JS ๊ฐ์ ธ ์ค๊ธฐ๋ฅผ ์ฌ์ฉํ์ฌ ์๋ํ๋ ์ค์ ์ด์๋ ๋ฒ ์ด ๋ณธ ๋ฆฌํฌ์งํ ๋ฆฌ๋ฅผ ๊ฐ์ง๊ณ ์์ต๋๊น?
๋๊ตฌ๋ ์ง JS ๊ฐ์ ธ ์ค๊ธฐ๋ฅผ ์ฌ์ฉํ์ฌ ์๋ํ๋ ์ค์ ์ด์๋ ๋ฒ ์ด ๋ณธ ๋ฆฌํฌ์งํ ๋ฆฌ๋ฅผ ๊ฐ์ง๊ณ ์์ต๋๊น?
์๋ ํ ๋ก ๊ณผ ๊ด๋ จ์ด ์์ ๊ฐ๋ฅ์ฑ์ด ์๋ ์ ์ฌํ ๋ฌธ์ ๋ฅผ ๊ฒ์ํ๋ ๋์ ์ด ํ์ด์ง๋ฅผ ์ฐพ์ ์ฌ๋์ผ๋ก์. ๋ด ๋ฌธ์ ๋ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Dynamic_Imports์ ์ค๋ช ๋ ๊ธฐ๋ฅ๊ณผ ๊ฐ์ ๋์ ๊ฐ์ ธ์ค๊ธฐ๋ฅผ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ ๋ฐ์ํ์ต๋๋ค https://github.com/airbnb/babel-plugin-dynamic-import-node ์์ ์ฌ์ฉํ ์ ์๋ ์ด ๊ธฐ๋ฅ์ ๋ํ babel ํ๋ฌ๊ทธ์ธ์ ์ถ๊ฐํ๊ณ ๊ทธ์ ๋ฐ๋ผ .babelrc๋ฅผ ์์ ํ์ฌ ํด๊ฒฐ๋์์ต๋๋ค.
{
...
"env": {
"test": {
"plugins": ["dynamic-import-node"]
}
...
}
๋ ๊ทธ๋ฅ ์ต์ ์ ์ ๊ฑฐ :{ module:false }, ๊ทธ๋ฆฌ๊ณ ๊ทธ๊ฒ์ ์๋ํฉ๋๋ค
"presets": [
["@babel/env", {module: false} ],
"@babel/preset-react",
"@babel/preset-typescript"
]
jest config์ transformIgnorePatterns: []
๋ฅผ ์ถ๊ฐํ๊ณ babelrc์ transform-es2015-modules-commonjs
๋ฅผ ์ถ๊ฐํ๋ฉด ๋ฌธ์ ๊ฐ ํด๊ฒฐ๋์์ต๋๋ค.
๋๋ด: ^23.1.0,
๋ฐ๋ฒจ: 6.26.3
@ngmgit ๊ทธ๋ฌ๋ ์ด๊ฒ์ ๋ชจ๋ node_modules๋ฅผ ํธ๋์คํ์ผํ๋ค๋ ๊ฒ์ ์๋ฏธํฉ๋๋ค. ์ด๋ ๋งค์ฐ ์ต์ ์ด ์๋๋๋ค...
๋์ ๊ฒฝ์ฐ์๋,
.babelrc
์๋ํ์ง ์์ต๋๋คํ์ง๋ง ๋์ ๋์ผํ ์ค์ ์ ์ด๋ํ์ฌ ๋ฌธ์ ๋ฅผ ํด๊ฒฐ .babelrc
์ babel.config.js
.
๋์์ด ๋์๊ธฐ๋ฅผ ๋ฐ๋๋๋ค!
์ฌ๊ธฐ์ ์๋ ๋ชจ๋ ๊ฒ์ ๋์๊ฒ ํจ๊ณผ๊ฐ ์์ต๋๋ค. ๋๋ React ์ ํ๋ฆฌ์ผ์ด์ ์ ํ ์คํธํ๊ธฐ ์ํ ํ์ด์ค๋ถ์ ๊ณต์ ํ ์คํธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ๊ทธ ์ ๋๋ก ๊ตฌ์ฑํ๊ธฐ ์ด๋ ต๋ค๋ ๊ฒ์ ๋ฏฟ์ ์ ์์ต๋๋ค. ์ ๋ 8๋ ์ด์์ ๊ฒฝํ์ ๊ฐ์ง ๊ฐ๋ฐ์์ด๊ณ ๋ง์ ๋ฌธ์ ์ ์ง๋ฌธ์ ํตํด ์ฑ๊ณตํ์ง ๋ชปํ ์ฑ ํ๋ฃจ ์ค 5์๊ฐ์ ๋ณด๋์ต๋๋ค. ์คํ ์ค๋ฒํ๋ก์.
jest + react-testing-library์ ํจ๊ป ์๋ํ๋๋ก ๋ฐ์ ์์ฉ ํ๋ก๊ทธ๋จ์ ๊ตฌ์ฑํ ์ ์์ต๋๋ค.
์ด๊ฒ์ ๋จ์ง jest๊ฐ ์ฒ๋ฆฌํ ์ ์๋ ์๋ก์ด ๊ตฌ๋ฌธ ์ค๋ฅ๋ฅผ ๋์๊ฒ ๋งค๋ฒ ๊ณ ํจ์ ์ง๋ฅด๊ธฐ ๋๋ฌธ์ ๋๋ค. ๋ด๊ฐ ์ฌ์ฉํ๋ ๋ชจ๋ ํ๋ฌ๊ทธ์ธ์ ํ๋์ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ณ ๋ค๋ฅธ ํ๋๋ฅผ ์ ๊ณตํฉ๋๋ค. babel์ด ๋ชจ๋ ๊ฒ์ ์ฒ๋ฆฌํ๋๋ก ํ๋ ๋ฐฉ๋ฒ์ด ์์ต๋๊น?
๋๋ ์ ๋ง๋ก ํฌ๊ธฐํ ๊ฒ์ ์ง์ฌ์ผ๋ก ๋๋ด๊ณ ๋ ์ด์ ์๊ฐ์ ๋ณด๋ด์ง ์์ ๊ฒ์ ๋๋ค. ์๋ง๋ ํ๋ก์ ํธ์ ์ด ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ถ๊ฐํ๋ ๊ฒ์ ์ทจ์ํ ๊ฒ์ ๋๋ค.
์ง์งํ๊ฒ, jest + react-testing-library๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐ์ ์์ฉ ํ๋ก๊ทธ๋จ์ ํ ์คํธํ๋ ค๋ ์ฌ๋์ ์ํ ๊ธฐ๋ณธ ๊ตฌ์ฑ์ด ์๋ค๋ ๊ฒ์ ๋ณด๋ ๊ฒ์ ๋งค์ฐ ์ด์ํฉ๋๋ค.
๋ง์ SO ๋ต๋ณ ๋ฐ github ๋ฌธ์ ๋ก ๋ช ์๊ฐ ๋์ ์ํ ์ฐฉ์ค๋ฅผ ๊ฒช์ ํ @cpojer ์ ๋ต๋ณ์ด ์ ์๊ฒ
๋๋ฅผ ๋์ด ๋ช ๊ฐ์ง ์ถ๊ฐ ์ปจํ ์คํธ๋ก ์ต์ ๋ฐ ๊ฐ์ฅ ํฐ ์ ๋ฐ์ดํธ:
babel.config.js
module.exports = {
"env": {
"production": {
- "plugins": ["transform-es2015-modules-commonjs"]
# new kid on the block
+ "plugins": ["@babel/plugin-transform-modules-commonjs"]
}
}
}
jest.config.js
module.exports = {
// if you're also using typescript
preset: "ts-jest",
testEnvironment: "node",
verbose: true,
// registers babel.config.js with jest
transform: {
"^.+\\.js$": "babel-jest",
},
// explicitly include any node libs using ESM modules
transformIgnorePatterns: ["node_modules/?!(<ESM module here>|<another here>|<etc...>)"],
}
babel-jest
@babel/plugin-transform-modules-commonjs
ts-jest
(ํ์ดํ์คํฌ๋ฆฝํธ์ฉ)์ด ๋ฌธ์ ๋ ํ๋ก์ ํธ์ ๋ฃจํธ ๋๋ ํฐ๋ฆฌ์์ jest๋ฅผ ์คํํ์ง ์์ ๋๋ ๋ฐ์ํ ์ ์์ต๋๋ค...
react-native
๋๋ jest-expo
์ฌ์ ์ค์ ์ ์ฌ์ฉ ์ค์ด๊ณ ํ๋ก์ ํธ์ .js
ํ์ผ์ด ์๋ ๊ฒฝ์ฐ jest ๊ตฌ์ฑ์์ ์ผ์นํ๋๋ก transform
๋ฅผ ์ค์ ํด์ผ ํฉ๋๋ค.
https://jestjs.io/docs/en/configuration.html#transform -objectstring-pathtotransformer--pathtotransformer-object์ ํํ์์ ์ฌ์ฉํ๋๋ฐ ์๋ํ์ต๋๋ค.
transform: { '^.+\\.[jt]sx?$': 'babel-jest' }
pnpm ์ฌ์ฉ์๋ฅผ ์ํด ์ฌ๊ธฐ์ ๋ฉ๋ชจ๋ฅผ ๋จ๊ธฐ๊ณ ์ถ์ต๋๋ค....
๋ค์ ์์ ๋ pnpm ๊ฒฝ๋ก๊ฐ ๋ค๋ฅด๊ธฐ ๋๋ฌธ์ pnpm ์ฌ์ฉ์์๊ฒ๋ ์๋ํ์ง ์์ต๋๋ค. ์์ ์ ๊ท์์ ๋ณํฉ๋ node_modules
๋๋ ํ ๋ฆฌ์์๋ง ์๋ํฉ๋๋ค.
/xxx/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/antd/es/input/String
์ฐธ์กฐ: https://regexr.com/5cs2h
"transformIgnorePatterns": [
"node_modules/(?!(react-native|my-project|react-native-button)/)"
]
https://jestjs.io/docs/en/tutorial-react-native#transformignorepatterns - ๋ง์ถคํ
.*
๋ฅผ ์ถ๊ฐ ํ์ฌ ๋ง์ง๋ง ๋ฐ์์ ๋ํด ๋ถ์ ์ ์ธ ์์ธก์ ์ฌ์ฉํฉ๋๋ค.
node_modules/(?!(.*antd/es)/)
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
Node์์ ES ๋ชจ๋์ด ์ง์๋์ง ์๊ณ ๋ธ๋ผ์ฐ์ ๋ก ๋ฐฐ์กํ ๋ ๋น๋ ํ์ดํ๋ผ์ธ(์นํฉ, ๋กค์ )์ผ๋ก ์ปดํ์ผํ๋ ๊ฒ์ ์๋์ ์ผ๋ก ์ค๋จํ๊ธฐ ๋๋ฌธ์ ์ด ๋ฌธ์ ๊ฐ ๋ฐ์ํ ๊ฐ๋ฅ์ฑ์ด ๋์ต๋๋ค.
babelrc์ "ํ ์คํธ" ํ๊ฒฝ์ ์ํด ES ๋ชจ๋์์ CommonJS๋ก ์ปดํ์ผ์ ํ์ฑํํด์ผ ํฉ๋๋ค( https://babeljs.io/docs/usage/babelrc/#env -option ๋ฐ https://babeljs.io/docs/ ์ฐธ์กฐ). ํ๋ฌ๊ทธ์ธ/transform-es2015-modules-commonjs/