์ด ๋๋ ํ ๋ฆฌ์์ ์์ ๋ฅผ ์คํํ๋ ค๊ณ ํ ๋ ๋ค์ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
https://github.com/facebook/jest/tree/master/examples/async
FAIL __tests__/user-test.js
โ Test suite failed to run
ReferenceError: regeneratorRuntime is not defined
at Object.<anonymous> (__tests__/user-test.js:16:48)
at process._tickCallback (internal/process/next_tick.js:103:7)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 1.495s
Ran all test suites.
๋ ธ๋ 6.10 ๋ฐ ๋ ธ๋ 7.7.4๋ฅผ ์ฌ์ฉํ์ฌ ๋์ผํ ๋ฌธ์ ๊ฐ ์ฌํ๋ ์ ์์ต๋๋ค.
git clone https://github.com/facebook/jest jest
cd jest/examples/async
npm install
npm run test
์ด๊ฒ์ CI ๋๋ ๋ก์ปฌ์์ ๋ก์ปฌ๋ก ์ฌํ๋์ง ์์ต๋๋ค. npm ๋ฒ์ ์ ๋ฌธ์ ๊ฐ ์์ต๋๊น?
@cpojer
๊ณ ๋ง์ ํฌ๋ฆฌ์ค.
์ด ๋ฌธ์ ๋ ๋ค์ ๋ฆฌํฌ์งํ ๋ฆฌ์์ node:latest ๋ฅผ ์คํํ๋ ๋์ปค ์ปจํ
์ด๋์์ ์ฌํํ ์ ์์ต๋๋ค.
https://hub.docker.com/_/node/
์ค๋ฅ๊ฐ ๋ฐ์ํ๋ ๋จ๊ณ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
# from local machine (also tested in vagrant 1.9.2 running ubuntu/trusty64)
docker pull node:latest
docker run --rm -it node bash
# from inside the container (docker version 17.03.0-ce, node version 7.7.2)
git clone https://github.com/facebook/jest /jest
cd /jest
# as per instructions from https://github.com/facebook/jest/tree/master/examples
npm install
cd examples/async
npm install
npm test
์ด ์์ ๋ฅผ ์คํํ๋ ๋ฐฉ์์ด ๋ง์ต๋๊น?
์ด ์๋๋ฆฌ์ค๋ฅผ ํต๊ณผํ ์ ์๋ ๋ช ๊ฐ์ง ๊ธ๋ก๋ฒ ์๊ตฌ ์ฌํญ์ด ๋๋ฝ๋์์ ์ ์์ต๋๊น?
์ถ๊ฐ ์ ๋ณด๋ฅผ ์ ๊ณตํ ์ ์์ต๋๊น?
ํธ์งํ๋ค:
devDependencies์์ ์ฌ์ ๋ฐํ์์ babel-polyfill๋ก ๋ฐ๊พธ๋ฉด ์์ ๊ฐ ์๋ํ๋ค๋ ๊ฒ์ ์์์ต๋๋ค.
jest in master๊ฐ ์ค์ ๋ก ์ด์ ๋ชจ๋๊ณผ ํธํ๋๋ค๊ณ ๊ฐ์ ํ์ง๋ง ์์ ์์๋ ๋ง์คํฐ ์ฝ๋๋ฅผ ์ฌ์ฉํ์ง ์๊ณ ๋์ babel-polyfill์ด ํ์ํ ์ ์๋ ์ต์ ๋ฆด๋ฆฌ์ค๋ฅผ ๊ฐ์ ธ์ต๋๋ค.
Travis CI์์ ๋์ผํ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๊ณ ์์ต๋๋ค. https://travis-ci.org/okonet/lint-staged/jobs/212179781
๋ก์ปฌ์์๋ ์ ์์ ์ผ๋ก ์คํ๋์ง๋ง CI์์๋ ์คํ๋์ง ์๋ ํ ์คํธ
๋ฐฉ๊ธ์ด ๋ฌธ์ ์ ๋ถ๋ช์ณค์ต๋๋ค!
@mpontus @okonet jest@next
๊ณ ์ ๋ ๊ณณ์ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๊ทธ๋ ์ง ์์ผ๋ฉด babel-polyfill
์ค์นํด์ผ ํฉ๋๋ค.
Jest๋ babel-polyfill
๋ฅผ ์๋ ํฌํจํ๋ ๋ฐ ์ฌ์ฉํ์ง๋ง(19.0.2์์๋ ์ฌ์ ํ ์ํ), ๋ฉ๋ชจ๋ฆฌ ๋์๊ฐ ๋ฐ์ํ๊ธฐ ๋๋ฌธ์ regenerator-runtime
๋ง ์ฌ์ฉํ๋๋ก ์ฎ๊ฒผ์ต๋๋ค. v20 npm >=3 ๋๋ yarn์ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ async/await ์ง์์ ์ํด ์๋ฌด๊ฒ๋ ์ค์นํ ํ์๊ฐ ์์ต๋๋ค.
๋ฌธ์ ๋ ๋ฌธ์์์ babel-polyfill
๋ฅผ ์ญ์ ํ์ง๋ง ์ด diff https://github.com/facebook/jest/pull/2755 ๋ฅผ 19.0.2
์ ํฌํจํ๋ ๊ฒ์ ์์๊ณ ๋กค์์ํ์ง ์์๋ค๋ ๊ฒ์
๋๋ค. ๊ทธ ์ดํ๋ก ๊ด์ฐฎ์ ๋ฆด๋ฆฌ์ค( jest@next
ํ๊ทธ๋ง).
๋ถํธ์ ๋๋ ค ์ฃ์กํฉ๋๋ค!
@thymikee ๋ณํ๊ณผ ํจ๊ป ๋ฌธ์์ ์ค๋ช
๋ ๋๋ก regenerator-runtime
๋ฅผ ์ด๋ป๊ฒ ์ฌ์ฉํ ์ ์์ต๋๊น?
๋ด ํ๋ก์ ํธ์์ .babelrc
ํ์ผ์ ์ฌ์ฉํ์ง ์๊ณ ์๋์ ๊ฐ์ด webpack ๋ฐ jest ๊ตฌ์ฑ ๋ชจ๋์ ๋ํด ๊ณต์ ๋๋ babel ๊ตฌ์ฑ์ ๋ก๋ํฉ๋๋ค.
jest.config.js
...
'transform': {
'^.+\\.js$': '<rootDir>/config/jest.transform.js'
}
...
jest.transform.js
var babelConfig = require('./babel.config')
module.exports = require('babel-jest').createTransformer(babelConfig)
์ค๋๋ ๋ฌธ์ ๋ฅผ ๊นจ์์ ์ฃ์กํฉ๋๋ค. Jest 20.0.4์์ ๋์ผํ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๊ณ ์์ผ๋ฉฐ ์ด๋ฌํ ์๊ฒฌ์ ๋ํ ๊ฒฐ๋ก ์ ๋ด๋ฆด ์ ์์ต๋๋ค. ๋ด๊ฐ ๋ณผ ์์๋ ๊ฒ์์ :
babel-preset-env
์ฌ์ฉ์ ๊ถ์ฅํ์ง๋ง async/await ์์ ์์๋ ๋์ okonet
์ ๋ง์ฐฌ๊ฐ์ง๋ก regeneratorRuntime์ CircleCI์์ ๋ํ๋์ง๋ง ๋ก์ปฌ์์๋ ๋ํ๋์ง ์์ต๋๋ค. --runInBand
ํ์ฌ ๋ก์ปฌ์์ CI ํ
์คํธ ์คํฌ๋ฆฝํธ๋ฅผ ์คํํด๋ ์ด๊ฒ์ด ๋ฌธ์ ์์ ๋ํ๋ด์ง๋ ์์ต๋๋ค.regenerator-runtime
๋ฅผ ์ง์ ์ค์นํด์ผ ํ๊ธฐ ๋๋ฌธ์ธ ๊ฒ ๊ฐ์ต๋๋ค.๋๋ ๊ฐ์ ๋ฌธ์ ์ ์ง๋ฉดํ๊ณ babel-polyfill
๋ฅผ jest.init.js
(jest ์ค์ ํ์ผ)๋ก ์ง์ ๊ฐ์ ธ์ค๋ฉด ๋ฌธ์ ๊ฐ ํด๊ฒฐ๋์์ต๋๋ค.
...
"setupFiles": [
"<rootDir>/jest.init.js"
],
...
/// jest.init.js
import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import 'babel-polyfill';
๋ฒํ ์ฃ์กํฉ๋๋ค! ํ์ง๋ง ๐ ๊ณต์ ํ๊ณ ์ถ์์ต๋๋ค. ๋ด ์ผ๋ฐ ํ๋ฌ๊ทธ์ธ์ ์ด๊ฒ์ ์ถ๊ฐํ๋ฉด ์ ์๊ฒ ํจ๊ณผ์ ์ ๋๋ค.
[
"transform-runtime",
{
"helpers": false,
"polyfill": false,
"regenerator": true
}
]
ํ์ง๋ง ์ด๊ฒ์ด ์ต์ ์ ์ฒ๋ฆฌ ๋ฐฉ๋ฒ์ธ์ง๋ ๋ชจ๋ฅด๊ฒ ์ต๋๋ค.
์ด๊ฒ์ Jest์์ "ReferenceError: regeneratorRuntime์ด ์ ์๋์ง ์์์ต๋๋ค"๋ฅผ ์์ ํ๋ ๋ฐ ๋์์ด ๋์์ต๋๋ค.
npm install --save-dev @babel/plugin-transform-runtime
๊ทธ๋ฐ ๋ค์ .babelrc์์(๋ค๋ฅธ ์ต์ ์ ์ธ):
{
"env": {
"test": {
"plugins": ["@babel/plugin-transform-runtime"]
}
}
}
๋๋ ๊ฐ์ ๋ฌธ์ ์ ์ง๋ฉดํ๊ณ
babel-polyfill
๋ฅผjest.init.js
(jest ์ค์ ํ์ผ)๋ก ์ง์ ๊ฐ์ ธ์ค๋ฉด ๋ฌธ์ ๊ฐ ํด๊ฒฐ๋์์ต๋๋ค.... "setupFiles": [ "<rootDir>/jest.init.js" ], ...
/// jest.init.js import Enzyme from 'enzyme'; import Adapter from 'enzyme-adapter-react-16'; import 'babel-polyfill';
jest.init.js
์์ ํด๋ฆฌํ์ ๊ฐ์ ธ์ค๋ ค๊ณ ํ์ง๋ง ์ด์ํ๊ฒ ํ
์คํธ๊ฐ ์ค๋จ๋์์ต๋๋ค. ์ ์๋์ง ์์ ์์ฑ๊ธฐ ์ค๋ฅ๋ ์์์ง๋ง ๊ตฌ์ฑ ์์ ๊ฐ์ ธ์ค๊ธฐ ๋๋ฝ ๋๋ ์ํ ๋๋ฝ๊ณผ ๊ฐ์ด ์ด์ ์ ํ
์คํธ์์ ์์๋ ์ค๋ฅ๊ฐ ๋ง์ด ๋ฐ์ํ์ต๋๋ค.
๋ด jest.conf.js
์๋ ๋ค์ ์ค์ด ํฌํจ๋์ด ์์ต๋๋ค.
setupFiles: ['<rootDir>/test/unit/setup']
๋ฐ๋ผ์ jest.init.js
(์กด์ฌํ์ง ์์) ๋์ setup.js
์ ํด๋ฆฌํ์ ๊ฐ์ ธ์์ผ ํ์ต๋๋ค.
import Vue from 'vue'
import '@babel/polyfill';
Vue.config.productionTip = false
Vue.config.silent = true
_ํด๋ฆฌํ ์ํฌํธ ๋ผ์ธ์ ์ ์ธํ๊ณ ํ์ผ์ ์ด์์ ๋ ์ด๋ฏธ ๋ค๋ฅธ 3๊ฐ์ ๋ผ์ธ์ด ํ์ผ์ ์์์ต๋๋ค._
setup.js
์ ํด๋ฆฌํ ๊ฐ์ ธ์ค๊ธฐ๋ ์๋ํ์ง๋ง jest.init.js
์ ํด๋ฆฌํ ๊ฐ์ ธ์ค๊ธฐ๋ ์๋ํ์ง ์์์ต๋๋ค(์ ๋ setupFiles ๊ฒฝ๋ก๋ฅผ ๋ณ๊ฒฝํ์ต๋๋ค).
๋์ ๋ ธ๋๋ฅผ ํ์ฌ ์ถ๊ฐํ๋ฉด Jest์์ "ReferenceError: regeneratorRuntime์ด ์ ์๋์ง ์์"์ ์์ ํ๋ ๋ฐ ๋์์ด ๋์์ต๋๋ค.
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "current"
}
}
]
]
}
๋์ ๋ ธ๋๋ฅผ ํ์ฌ ์ถ๊ฐํ๋ฉด Jest์์ "ReferenceError: regeneratorRuntime์ด ์ ์๋์ง ์์"์ ์์ ํ๋ ๋ฐ ๋์์ด ๋์์ต๋๋ค.
{ "presets": [ [ "@babel/preset-env", { "targets": { "node": "current" } } ] ] }
์ด๊ฒ์ ์ค์ ๋ก babelyfing์ ๋์์ด ๋์ง ์์ต๋๋ค. ๋ด ์ฝ๋๋ ํ์ฌ ๋์ ๋ ธ๋๋ก ์ปดํ์ผ๋์ง ์์ต๋๋ค. ์์ ํ ๋์ผํ๊ฒ ๋ณด์ผ ๊ฒ์ ๋๋ค(ES6). ํ ์คํธ๊ฐ ์คํ๋๊ณ ์์ง๋ง ์ปดํ์ผ๋ ์ฝ๋๋ ์์ต๋๋ค.:P
์ฌ๊ธฐ ๋ด package.json์ ์ผ๋ถ๊ฐ ์์ต๋๋ค. ์ด๊ฒ์ ์ง๊ธ ์ ์๊ฒ ํจ๊ณผ์ ์ ๋๋ค.
"babel": {
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "current"
}
}
],
"@babel/preset-react"
],
"plugins": [
"component-identification",
"@babel/plugin-proposal-class-properties",
[
"module-resolver",
{
"root": [
"./src"
]
}
]
]
},
์ ์๊ฒ๋ ๊ณตํต ๊ตฌ์ฑ์ ์ง์ ๋ ์ฌ์ ์ค์ ํ๊ฒฝ ๋์์ ๊ฐ๋ ๊ฒ๋ง์ผ๋ก๋ ์ถฉ๋ถํ์ง ์์์ต๋๋ค.
๋ด๊ฐ ๋ช
์ ์ ์ผ๋ก ๋ค์ ๋์์ ์ ์ํ๋ค node
์์ env.test
.
{
'presets': [
[
'@babel/preset-env',
{
'targets': {
'chrome': 61,
'node': 8,
'electron': '2.0.9'
},
}
],
'@babel/typescript',
'@babel/preset-react'
],
'env': {
'test': {
'presets': [
[
'@babel/preset-env',
{
'targets': {
'node': 8, // <- ADDED
},
}
],
'@babel/typescript',
]
}
}
}
๋ด๊ฐํด์ผ ํ ์ ์ผํ ์ผ์ @AoDev๊ฐ ์ธ๊ธ ํ ๊ฒ์ฒ๋ผ babel์์ ๋์์ ์ ์ํ๋ ๊ฒ์ ๋๋ค. ๋ค๋ฅธ ๊ณณ์์๋ ์ถ๊ฐ ๋์์ด ์์ต๋๋ค.
regenerator-runtime์ ์ ์ญ์ ์ผ๋ก ๊ฐ์ ธ์ค๊ณ ์ ์ํ๋ ค๋ฉด ๋ค์์ ์ํํฉ๋๋ค.
require('regenerator-runtime/runtime');
ํ์ํ ๊ฒ์ ์ถ๊ฐํ๊ณ ๋๋ด์ ์ํด ๋ชจ๋ babel ํด๋ฆฌํ์ ์ถ๊ฐํ๋ ๋ฐ ํผ๋์ ์ฃผ์ง ์์ผ๋ ค๋ ์ฌ๋๋ค์ ์ํด.
์ด๊ฒ์ Jest 24๋ฅผ ์๊ฐํ๋ ๋ธ๋ก๊ทธ ๊ฒ์๋ฌผ์์ ์ธ๊ธ๋์์ต๋๋ค. ๋ง์ฝ ์ฌ๋๋ค์ด ๊ทธ๊ฒ์ ๋์ณค๋ค๋ฉด: https://jestjs.io/blog/2019/01/25/jest-24-refreshing-polished-typescript-friendly#breaking -changes. babel์ ์ ์ ํ๊ฒ ๊ตฌ์ฑํด์ผ ํ๋ฉฐ ์ฝ๋๋ฅผ ๋ณ๊ฒฝํ ํ์๊ฐ ์์ต๋๋ค. PR์์ ์ด ๋๊ธ์ ์ฐธ์กฐ ํ์ธ์ .
@ghengeveld ์ด๊ฒ์ ๋๋ฅผ ์ํ ์ผ์ ๋๋ค, ๊ฐ์ฌํฉ๋๋ค
๋๋ฅผ ์ํด ๊ทธ๊ฒ์ ํด๊ฒฐ ํ ๊ฒ์ ์ฌ์๊ธฐ๋ฟ๋ง ์๋๋ผ ๋ฐํ์๋ ๋ด ๊ตฌ์ฑ์ ์ถ๊ฐํ๋ ๊ฒ์ด ์์ต๋๋ค.
'globals': {
'vue-jest': {
'babelConfig': {
'plugins': [
'@babel/plugin-transform-regenerator',
'@babel/plugin-transform-runtime',
],
'presets': [['@babel/preset-env', { 'modules': false }]],
'env': {
'test': {
'presets': [
['@babel/preset-env', { 'targets': { 'node': 'current' } }],
],
},
},
},
},
},
@ ์ฌ๊ธฐ์ ์๋ ์๋ ์ค ์ด๋ ๊ฒ๋ ๋๋ฅผ ์ํด ์๋ํ์ง ์์์ต๋๋ค. ์ฌ์ ํ ํ ์คํธ ์ค์ํธ๊ฐ ์คํ๋์ง ์๋ ๊ฒ ๊ฐ์ต๋๋ค.
ReferenceError: regeneratorRuntime is not defined
์ด๊ฒ์ ๋ด package.json
"devDependencies": {
"@babel/core": "^7.6.2",
"@babel/plugin-transform-regenerator": "^7.8.7",
"@babel/runtime": "^7.9.6",
"@react-native-community/eslint-config": "^0.0.5",
"babel-eslint": "^10.0.1",
"๋ฐ๋ฒจ ๋๋ด": "^24.9.0",
"babel-plugin-module-resolver": "^3.1.3",
"babel-plugin-transform-runtime": "^6.23.0",
"ํด๋
": "^16.5.1",
.babelrc
์ด ํ์ผ์ ๋ฌด์ธ๊ฐ๋ฅผ ์ถ๊ฐํ ๋๋ง๋ค ๋ด ์ฑ ํ์ผ์ด ์คํ๋์ง ์์ต๋๋ค. ๋ฐ์ํ ๊ธฐ๋ณธ ์ฑ์ ์คํํ๋ ค๋ฉด ์ด ํ์ผ์ ๋ค์ ์ญ์ ํด์ผ ํฉ๋๋ค.
๋์ผํ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ต๋๋ค. ์ ์ ํ jest
(26.6.3), ์ผ๋ฐ ๋น๋๊ธฐ ํ
์คํธ ๋ฐ ์ด..
์ค์ค๋ก๋ฅผ "์ฆ๊ฑฐ์"์ผ๋ก ์ ์ํ๋ ํ๋ ์์ํฌ์ ๊ฒฝ์ฐ ์ด๊ฒ์ ์ค์ ๋ก๋ ๊ทธ๋ ์ง ์์ต๋๋ค.
๋๋์ฒด ์ babel ๊ตฌ์ฑ, ํ๋ฌ๊ทธ์ธ ๋ฑ์ ์ ๊ฒฝ์จ์ผ ํฉ๋๊น?
๊ธฐ๋ณธ์ ์ผ๋ก ์๋ํ์ง ์๋ ์ด์ ๋ ๋ฌด์์
๋๊น?
์
๋ฐ์ดํธ ์ ๊ฒฝ์ฐ์๋ babel.config.js๊ฐ ์ด๋ฏธ ์์์ง๋ง ๋๋ฝ๋์์ต๋๋ค.
targets: {
node: 'current'
}
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
์ด๊ฒ์ Jest์์ "ReferenceError: regeneratorRuntime์ด ์ ์๋์ง ์์์ต๋๋ค"๋ฅผ ์์ ํ๋ ๋ฐ ๋์์ด ๋์์ต๋๋ค.
๊ทธ๋ฐ ๋ค์ .babelrc์์(๋ค๋ฅธ ์ต์ ์ ์ธ):