ãžã§ã¹ãã奜ãïŒ ç§ãã¡ã®éåäœããµããŒãããããšãæ€èšããŠãã ããïŒðhttpsïŒ//opencollective.com/jest/donate
Reactã¢ããªã§Jestã䜿çšããŠãã¹ããå®è¡ããããšãããšã Jest encountered an unexpected token
ãšã©ãŒãçºçããŸãã Link
ã³ã³ããŒãã³ããã³ããŒããŠãããããçŽæ¥ãã¹ãããŸã
FAIL src/components/Link.test.js
â Test suite failed to run
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
Here's what you can do:
⢠To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
⢠If you need a custom transformation specify a "transform" option in your config.
⢠If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html
Details:
SyntaxError: C:\workspace\react\testapp\src\components\Link.test.js: Unexpected token (8:4)
6 | test('Link changes the class when hovered', () => {
7 | const component = renderer.create(
> 8 | <Link page="http://www.facebook.com">Facebook</Link>,
| ^
9 | );
10 | let tree = component.toJSON();
11 | expect(tree).toMatchSnapshot();
at Parser.raise (node_modules/@babel/parser/lib/index.js:3938:15)
at Parser.unexpected (node_modules/@babel/parser/lib/index.js:5247:16)
at Parser.parseExprAtom (node_modules/@babel/parser/lib/index.js:6327:20)
at Parser.parseExprSubscripts (node_modules/@babel/parser/lib/index.js:5923:21)
at Parser.parseMaybeUnary (node_modules/@babel/parser/lib/index.js:5902:21)
at Parser.parseExprOps (node_modules/@babel/parser/lib/index.js:5811:21)
at Parser.parseMaybeConditional (node_modules/@babel/parser/lib/index.js:5783:21)
at Parser.parseMaybeAssign (node_modules/@babel/parser/lib/index.js:5730:21)
at Parser.parseExprListItem (node_modules/@babel/parser/lib/index.js:6994:18)
at Parser.parseCallExpressionArguments (node_modules/@babel/parser/lib/index.js:6123:22)
ãããç§ã®package.json
{
"name": "testapp",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"start": "webpack-dev-server --config ./config/webpack.config.development.js",
"build": "webpack -p --config ./config/webpack.config.production.js",
"test": "jest"
},
"dependencies": {
"axios": "^0.18.0",
"moment": "^2.22.2",
"polished": "^2.0.3",
"prop-types": "^15.6.2",
"react": "^16.4.2",
"react-delay-render": "^0.1.2",
"react-dom": "^16.4.2",
"react-imported-component": "^4.6.2",
"react-router-dom": "^4.3.1",
"simple-grid": "^1.0.1",
"styled-components": "^3.4.5",
"uuid": "^3.3.2",
"validator": "^10.7.0"
},
"devDependencies": {
"@babel/core": "^7.0.0",
"@babel/plugin-proposal-class-properties": "^7.0.0",
"@babel/plugin-syntax-dynamic-import": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"@babel/preset-react": "^7.0.0",
"babel-core": "7.0.0-bridge.0",
"babel-jest": "^23.4.2",
"babel-loader": "^8.0.0",
"babel-plugin-styled-components": "^1.6.0",
"css-loader": "^1.0.0",
"html-webpack-plugin": "^3.2.0",
"jest": "^23.5.0",
"mini-css-extract-plugin": "^0.4.2",
"react-test-renderer": "^16.4.2",
"regenerator-runtime": "^0.12.1",
"style-loader": "^0.23.0",
"webpack": "^4.17.1",
"webpack-cli": "^3.1.0",
"webpack-dev-server": "^3.1.7"
}
}
ãããç§ã®.babelrc
ãã¡ã€ã«ïŒã«ãŒããã£ã¬ã¯ããªã«ãããŸãïŒã§ãïŒ
{
"plugins": [
"@babel/plugin-proposal-class-properties",
"babel-plugin-styled-components",
"@babel/plugin-syntax-dynamic-import"
],
"presets": ["@babel/preset-env", "@babel/preset-react"],
"env": {
"development": {
"plugins": [
"@babel/plugin-proposal-class-properties",
["babel-plugin-styled-components", { "displayName": true }],
"@babel/plugin-syntax-dynamic-import"
],
"presets": ["@babel/preset-env", "@babel/preset-react"]
},
"test": {
"plugins": [
"@babel/plugin-proposal-class-properties",
["babel-plugin-styled-components", { "displayName": true }],
"@babel/plugin-syntax-dynamic-import"
],
"presets": ["@babel/preset-env", "@babel/preset-react"]
},
"production": {
"plugins": [
"@babel/plugin-proposal-class-properties",
"babel-plugin-styled-components",
"@babel/plugin-syntax-dynamic-import"
],
"presets": ["@babel/preset-env", "@babel/preset-react"]
}
}
}
åäœãåçŸããæé ïŒ
yarn add --dev babel-jest babel-core@^7.0.0-0 @babel/core
Jestãã€ã³ã¹ããŒã«ããŸãyarn test
å®è¡ããŸãnpx envinfo --preset jest
å®è¡ããŸãããã«çµæã貌ãä»ããŸãã
OS: Windows 10
CPU: x64 Intel(R) Xeon(R) CPU E3-1505M v6 @ 3.00GHz
Binaries:
Yarn: 1.5.1 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
npm: 5.6.0 - C:\Program Files\nodejs\npm.CMD
ç§ãåãåé¡ãæ±ããŠããŸãã
ããã§ãåãåé¡ããããŸãã
ããã§ãåãåé¡ããããŸããã¢ãã¬ãã§äœæ¥ããŠããã®ã§ãå°ãè€éã§ãã
@fabioSalimbeni @hoaiduyit @mikedloss Reactããã³Jest
ããã³enzyme
ïŒãBabel7ã䜿çšããŠLernaã¢ãã¬ãã«åæ ŒãããŸããã
ã¢ãã¬ãã§äœæ¥ããŠããå Žåã¯ã babel.config.js
æ°ããBabelæ§æã䜿çšããå¿
èŠããããŸãïŒ https ïŒ//babeljs.io/docs/en/v7-migration
ããã¯ãmonorepoã®ã«ãŒãã«å¯Ÿããç§ã®babel.config.js
ã§ãã
module.exports = {
overrides: [
{
test: 'platforms/webApp1',
extends: 'platforms/webApp1/babel.config.js'
},
{
test: 'platforms/webApp2',
extends: 'platforms/webApp2/babel.config.js'
}
]
};
ããã¯ãmonorepoã¢ããªã®1ã€ã§ããwebApp1
babel.config.js
ãã©ã®ããã«èŠãããã瀺ããŠããŸãã
module.exports = {
env: {
test: {
presets: [
[
'@babel/preset-env',
{
modules: 'commonjs',
debug: false
}
],
'@babel/preset-flow',
'@babel/preset-react'
],
plugins: [
'@babel/plugin-syntax-dynamic-import',
'@babel/plugin-proposal-class-properties'
]
},
production: {
presets: [
['@babel/preset-env', { modules: false }],
'@babel/preset-flow',
'@babel/preset-react'
],
plugins: [
'@babel/plugin-syntax-dynamic-import',
'@babel/plugin-proposal-class-properties'
]
},
development: {
presets: [
['@babel/preset-env', { modules: false }],
'@babel/preset-flow',
'@babel/preset-react'
],
plugins: [
'@babel/plugin-syntax-dynamic-import',
'@babel/plugin-proposal-class-properties'
]
}
}
};
ãããŠãããã¯Jestã®æ§æã§ãã
"jest": {
"verbose": true,
"clearMocks": true,
"collectCoverage": true,
"setupTestFrameworkScriptFile": "<rootDir>/config/setupTest.js",
"transform": {
"^.+\\.js$": "babel-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|scss)$": "<rootDir>/__mocks__/styleMock.js"
}
}
setupTest.js
ã¯æ¬¡ã®ããã«ãªããŸãã
const Enzyme = require('enzyme');
const Adapter = require('enzyme-adapter-react-16');
Enzyme.configure({ adapter: new Adapter() });
fileMocks.js
ã¯æ¬¡ã®ããã«ãªããŸãïŒ
module.exports = 'i-am-a-stubbed-file';
styleMocks.js
ã¯æ¬¡ã®ããã«ãªããŸãïŒ
module.exports = {};
ãŸããåã¢ããªã®package.json
ã«ããã€ãã®Babelãã©ã°ã€ã³ãšbabel-core
ããŒãžã§ã³ãdevDependencies
ãšããŠè¿œå ããå¿
èŠããããŸãã
...
"@babel/cli": "^7.0.0",
"@babel/core": "^7.0.0",
"@babel/plugin-proposal-class-properties": "^7.0.0",
"@babel/plugin-syntax-dynamic-import": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"@babel/preset-flow": "^7.0.0",
"@babel/preset-react": "^7.0.0",
"babel-core": "7.0.0-bridge.0",
"babel-eslint": "^9.0.0",
"babel-jest": "^23.4.2",
"babel-loader": "^8.0.2",
...
"jest": "^23.5.0",
"jest-cli": "^23.5.0",
...
3æ¥åŸãç§ã¯ãã®åé¡ã®è§£æ±ºçãèŠã€ããŸããã
ããã«ã¯2ã€ã®è§£æ±ºçããããŸãã
ãã¡ã€ã«åã.babelrc
ããbabel.config.js
ã«å€æŽã§ããããã¯æ©èœããŸãã
##解決ç2ïŒ
ç§ã¯ãã®ããã«äœ¿çšããŠããŸãã
次ã®ãããªå€æãã¡ã€ã«ãäœæããŸãïŒç§ã®ãã®ã¯jest-transforme.js
ïŒïŒ
const config = {
babelrc: false,
presets: [
[
"@babel/env",
{
modules: false
}
],
"@babel/react"
],
plugins: [
["@babel/plugin-proposal-decorators", { legacy: true }],
["@babel/plugin-proposal-class-properties", { loose: true }],
"transform-es2015-modules-commonjs"
]
};
module.exports = require("babel-jest").createTransformer(config);
ãããç§ã®jest.config.js
ã§ãïŒ
module.exports = {
collectCoverageFrom: ["src/**/*.{js,jsx,mjs}"],
testMatch: ["<rootDir>/src/**/__tests__/**/*.{js,jsx,mjs}", "<rootDir>/src/**/?(*.)(spec|test).{js,jsx,mjs}"],
transform: {
"^.+\\.(js|jsx|mjs)$": "<rootDir>/config/jest/jest-transformer.js"
},
transformIgnorePatterns: ["[/\\\\]node_modules[/\\\\].+\\.(js|jsx|mjs)$"]
};
ãããŠãç§ã¯ãŸã ç§ã®.babelrc
æã£ãŠããŸã
`` `
const enviroments = requireïŒ "./ env-config.js"ïŒ;
module.exports = {
ããªã»ããïŒ[
[
ãnext / babelãã
{{
"preset-env"ïŒ{
useBuiltInsïŒ "ãšã³ããª"
}
}
]
]ã
ãã©ã°ã€ã³ïŒ[
["styled-components"ã{ssrïŒtrueãdisplayNameïŒtrueãpreprocessïŒfalse}]ã
[
ãã¢ãžã¥ãŒã«ãªãŸã«ããã
{{
æ ¹ïŒ ["ã/"]
}
]
]ã
envïŒ{
devïŒ{
ãã©ã°ã€ã³ïŒ[
["transform-define"ãç°å¢]ã
["module-resolver"ã{rootïŒ["./"]}]ã
ãtransform-decorators-legacyãã
ãtransform-class-propertiesã
]
}ã
建ãŠãïŒ {
ãã©ã°ã€ã³ïŒ[
["transform-define"ãç°å¢]ã
["module-resolver"ã{rootïŒ["./"]}]ã
ãtransform-decorators-legacyãã
ãtransform-class-propertiesã
]
}ã
補é ïŒ {
ããªã»ããïŒ[
[
"minify"ã//ããã«minifyãè¿œå ããçç±ãã³ãŒããã³ãã«å
šäœã«å¯ŸããŠuglifyãå®è¡ãã
{{
ãã³ã°ã«ïŒfalseã
è©äŸ¡ïŒfalse
}
]
]ã
ãã©ã°ã€ã³ïŒ[
["transform-define"ãç°å¢]ã
["@ babel / plugin-proposal-decorators"ã{ã¬ã¬ã·ãŒïŒtrue}]ã
["@ babel / plugin-proposal-class-properties"ã{looseïŒtrue}]ã
["module-resolver"ã{rootïŒ["./"]}]
]ã
ã³ã¡ã³ãïŒfalseã
ã³ã³ãã¯ãïŒæ¬åœã
çž®å°ïŒtrue
}
}
};
ãã
ããã§çããããã®åé¡ã«åæ Œã§ããããšãé¡ã£ãŠããŸãã 也æ¯ã
@GeorgianSorinMaximåé¡ã¯ãç§ã®jestã¢ããªã.babelrc
ãã¡ã€ã«ãç¡èŠãã babel.config.js
ã«å€æŽãããšåé¡ã解決ããããšã§ããããšã«ãã解決ããŸãããç§ã®çãã¯ãã®ã³ã¡ã³ãã®äžã«ãããŸãã
@GeorgianSorinMaxim @hoaiduyitã«æè¬ããŸããæçµçã«ã¯ãbabel-jestçšã®ã«ã¹ã¿ã ãã©ã³ã¹ãã©ãŒããŒãäœæããã ãã§è§£æ±ºããŸããã å¿ èŠã«å¿ããŠä»ã®å€æŽã¯ãããŸããã
Babel 7ãæ£ããåäœããã«ã¯ã babel.config.js
ãå¿
èŠãªããã§ããbabelrcã®ä»£ããã«ããã䜿çšããŠãã ãã
babel-jest
ãŸã Babel 6ã®babel-core
äŸåããŠããŸããBabel7ã䜿çšããå Žåã¯ã次ã䜿çšã§ããŸãã
ãã®ãã©ã³ã¹ãã©ãŒããŒbabel7-jest
babel-jest
ã¯babel 7ã§åé¡ãªãåäœããã«ã¹ã¿ã ãã©ã³ã¹ãã©ãŒããŒã¯å¿
èŠãããŸãã
@SimenB .babelrc
ååãbabel.config.js
ããäºæããªãããŒã¯ã³ã§ãã®ãšã©ãŒã衚瀺ãããŸãã ãã ããbabel7-pre.42ã§åäœããŸãã ä»ã«ããèŠèœãšãããŠãããã®ã¯ãããŸããïŒ ïŒïŒ ããããšãã
@alexindigo babel.config.js
æžãæ¹ã«ã€ããŠã¯ãã¡ããã芧ãã ãã
ããã yarn upgrade --latest
å®è¡ãããšåé¡ãä¿®æ£ãããŸãã:)
@alexindigo @hoaiduyit babel.config.js
ã«ã€ããŠããã£ããã©ããã£ãŠãããç解ããã®ã§ããïŒ åãåé¡ãšæŠããããªãã®è§£æ±ºçãèŠã€ããŠãç§ã¯èªåèªèº«ã«èããŠããŸãã... _ "ããŒãããããããŸãããæ¹æ³ã¯ãããŸãããããšã«ãããã£ãŠã¿ãŸãã" _ãããã確ãã«ããã¯lulzãããŸããã ããããšã
ãžã§ã¹ããšé µçŽ ã®æ§æïŒpackage.jsonã«æ¬¡ã®ã³ãŒããè¿œå ããŸã
"jest": {
"testEnvironment": "jsdom",
"moduleDirectories": [
"src",
"node_modules"
],
"moduleNameMapper": {
"\\.(css|scss)$": "<rootDir>/__mocks__/styleMock.js",
"\\.(jpg|gif|ttf|eot|svg)$": "<rootDir>/__mocks__/fileMock.js"
},
"transform": {
"^.+\\.(js|jsx)$": "babel-jest",
".+\\.(css|styl|less|sass|scss)$": "<rootDir>/node_modules/jest-css-modules-transform"
},
"setupTestFrameworkScriptFile": "<rootDir>/setupTests.js",
"setupFiles": [
"<rootDir>setup.js"
],
"moduleFileExtensions": [
"css",
"scss",
"js",
"json",
"jsx"
],
"testRegex": "\/test\/spec\/.*\\.js$",
"transformIgnorePatterns": [
"/node_modules/(?!test-component).+\\.js$"
]
}
é µçŽ ã®ã»ããã¢ããçš=> setup.js
import { configure } from 'enzyme'
import Adapter from 'enzyme-adapter-react-16'
configure({ adapter: new Adapter() })
setupTestFrameworkScriptFileã®å ŽåïŒsetupTests.js global.fetch = requireïŒ 'jest-fetch-mock'ïŒ
const { JSDOM } = require('jsdom')
const jsdom = new JSDOM('<!doctype html><html><body></body></html>')
const { window } = jsdom
const exposedProperties = ['window', 'navigator', 'document']
const { document } = new JSDOM('').window
global.document = document
global.window = document.defaultView
global.HTMLElement = window.HTMLElement
global.HTMLAnchorElement = window.HTMLAnchorElement
Object.keys(document.defaultView).forEach(property => {
if (typeof global[property] === 'undefined') {
exposedProperties.push(property)
global[property] = document.defaultView[property]
}
})
global.navigator = {
userAgent: 'node.js',
}
.tsxãã¡ã€ã«ã ããåãåé¡ã«çŽé¢ããŠããŸããïŒ
ãã¥ãŒã¹ãåºããã ãã§ãJest v24ã¯å
éšã§Babel 7ã䜿çšããããã«æŽæ°ããã babel bridge
ãŸãã¯babel7-jest
ã䜿çšããå¿
èŠããªããªããŸããã
https://twitter.com/i/web/status/1088904207653105664
.babelrc
ååãbabel.config.js
ã«å€æŽããBabel7ã«ã¢ããã°ã¬ãŒãããŠãã以åãšåããšã©ãŒãçºçããŸãã ç§ã®babel.config.js
ãã¡ã€ã«ïŒ
module.exports = function (api) {
api.cache(true);
const presets = ["@babel/preset-env"];
return {
presets
};
}
@babel/preset-react
ãã€ã³ã¹ããŒã«ããŠã babel.config.js
ãã¡ã€ã«ã«è¿œå ããŸããã ãšã©ãŒãä¿®æ£ããŸããã
__test__
ãã£ã¬ã¯ããªå
ã«çœ®ããããã babel.config.js
ãã¡ã€ã«ãbabel-jest
ã«ãã£ãŠæ€åºãããŸãã__test__
ã package.json
ã眮ãããŠããããããã£ã¬ã¯ããªã«ç§»åãããã¹ãŠãæ©èœããŸããã
ç§ã¯æ¬¡ã®ããšãè¡ãããšã§åæ§ã®åé¡ã解決ããŸããïŒ
1-é µçŽ ã»ããã¢ãããã¡ã€ã«ãè¿œå ãã以äžãèšè¿°ããŸãã
import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
Enzyme.configure({ adapter: new Adapter() });
2-次ã®ããã«jestæ§æãpackage.jsonã«è¿œå ããŸãã
"jest": {
"setupFilesAfterEnv": [
"./path to your setup file/setupEnzyme.js"
]
}
3-ã«ãŒããã¹ã«.babelrcãã¡ã€ã«ãè¿œå ãã次ã®ããã«èšè¿°ããŸãã
{
"env": {
"test": {
"presets": [
"@babel/preset-env",
"@babel/preset-react"
]
}
}
}
4-ãã¹ãã§ãexpectãããŒã¯ãŒãã§ãšã©ãŒãçºçããå Žåã¯ã npm install -D chai
ãå®è¡ãã import { expect } from 'chai';
ããã«ãã¹ãã³ãŒãã«expecté¢æ°ãã€ã³ããŒãããŸãã
ããã§ããšã©ãŒãçºçããå Žåã¯ããã®ãããªbabeläŸåé¢ä¿ãã€ã³ã¹ããŒã«ããŠã¿ãŠãã ããnpm install -D @babel/core @babel/preset-env @babel/preset-react
ã圹ã«ç«ãŠãã°ã
3æ¥åŸãç§ã¯ãã®åé¡ã®è§£æ±ºçãèŠã€ããŸããã
ããã«ã¯2ã€ã®è§£æ±ºçããããŸãã解決ç1ïŒè§£æ±ºç1ïŒ
ãã¡ã€ã«åã
.babelrc
ããbabel.config.js
ã«å€æŽã§ããããã¯æ©èœããŸãã
ç§ã¯.babelrcãã¡ã€ã«ãæã£ãŠããªãã£ãã®ã§ãéã®æäœãæ©èœããã¯ãã ãšèãã代ããã«babel.config.jsã®ããªã»ããæ
å ±ã䜿çšããŠ.babelrcãã¡ã€ã«ãäœæããŸããã ïŒè¿œå ã®ãã¡ã€ã«ãäœæããŸããããå
ã®ãã¡ã€ã«ã®ååã¯å€æŽããŸããã§ããïŒ
ãã¹ããå床å®è¡ãããšãæ°ãããšã©ãŒRequires Babel "^7.0.0-0", but was loaded with "6.26.3"
ã
ç§ã¯ãã®ãšã©ãŒãã°ãŒã°ã«ã§æ€çŽ¢ããŸããããããŠããã¯ç§ãããã«å°ããŸãnpm install [email protected]
ïŒã«åŸãããã¹ããå®è¡ãããåæ ŒããŸããã
ãã¥ãŒã¹ãåºããã ãã§ãJest v24ã¯å éšã§Babel 7ã䜿çšããããã«æŽæ°ããã
babel bridge
ãŸãã¯babel7-jest
ã䜿çšããå¿ èŠããªããªããŸããã
https://twitter.com/i/web/status/1088904207653105664
Jest v24ã«ã¢ããã°ã¬ãŒããããšããã®åé¡ãä¿®æ£ãããŸãã:)
unexpected token
ãšã©ãŒãçºçãã
> 10 | const browser;
ïŒæåŸã®ã»ãã³ãã³ïŒ
ç§ã®package.json
"dependencies": {
"@babel/cli": "^7.2.3",
"@babel/core": "^7.3.4",
"@babel/register": "^7.0.0",
"assert": "^1.4.1",
"chai": "^4.2.0",
"jest-puppeteer": "^4.0.0",
"screen-info": "^1.0.1",
"screenres": "^2.0.1"
},
"devDependencies": {
"@babel/plugin-transform-modules-commonjs": "^7.2.0",
"@babel/preset-env": "^7.3.4",
"babel-jest": "^24.5.0",
"babel-preset-env": "^1.7.0",
"babel-preset-jest": "^24.3.0",
"jest": "^24.5.0",
"jest-cli": "^24.5.0",
"puppeteer": "^1.13.0"
},
"jest": {
"transformIgnorePatterns": [
"/node_modules/(?!@babel).+\\.js$"
],
"transform": {
"^.+\\.js?$": "babel-jest"
}
},
babel.config.js
module.exports = {
presets: [
[
'@babel/preset-env',
{
targets: {
node: 'current'
},
},
'jest'
]
],
env: {
test: {
plugins: ['@babel/plugin-transform-modules-commonjs']
}
}
}
ãã¹ãæžã¿ã®hoaiduyitãœãªã¥ãŒã·ã§ã³ã ããã¯åäœããŸãã ç§ã¯ãã babel.config.jsãè¿œå ããjest.config.jsã«babel-jest
ãã©ã³ã¹ãã©ãŒã /ãã©ã³ã¹ãã€ã«jestãã€ã³ã¹ããŒã«ããå¿
èŠããããŸã
3æ¥åŸãç§ã¯ãã®åé¡ã®è§£æ±ºçãèŠã€ããŸããã
ããã«ã¯2ã€ã®è§£æ±ºçããããŸãã解決ç1ïŒè§£æ±ºç1ïŒ
ãã¡ã€ã«åã
.babelrc
ããbabel.config.js
ã«å€æŽã§ããããã¯æ©èœããŸãã解決ç2ïŒè§£æ±ºç2ïŒ
ç§ã¯ãã®ããã«äœ¿çšããŠããŸãã
次ã®ãããªå€æãã¡ã€ã«ãäœæããŸãïŒç§ã®ãã®ã¯jest-transforme.js
ïŒïŒconst config = { babelrc: false, presets: [ [ "@babel/env", { modules: false } ], "@babel/react" ], plugins: [ ["@babel/plugin-proposal-decorators", { legacy: true }], ["@babel/plugin-proposal-class-properties", { loose: true }], "transform-es2015-modules-commonjs" ] }; module.exports = require("babel-jest").createTransformer(config);
ãããç§ã®
jest.config.js
ã§ãïŒmodule.exports = { collectCoverageFrom: ["src/**/*.{js,jsx,mjs}"], testMatch: ["<rootDir>/src/**/__tests__/**/*.{js,jsx,mjs}", "<rootDir>/src/**/?(*.)(spec|test).{js,jsx,mjs}"], transform: { "^.+\\.(js|jsx|mjs)$": "<rootDir>/config/jest/jest-transformer.js" }, transformIgnorePatterns: ["[/\\\\]node_modules[/\\\\].+\\.(js|jsx|mjs)$"] };
ãããŠãç§ã¯ãŸã ç§ã®
.babelrc
æã£ãŠããŸãconst enviroments = require("./env-config.js"); module.exports = { presets: [ [ "next/babel", { "preset-env": { useBuiltIns: "entry" } } ] ], plugins: [ ["styled-components", { ssr: true, displayName: true, preprocess: false }], [ "module-resolver", { root: ["./"] } ] ], env: { dev: { plugins: [ ["transform-define", enviroments], ["module-resolver", { root: ["./"] }], "transform-decorators-legacy", "transform-class-properties" ] }, build: { plugins: [ ["transform-define", enviroments], ["module-resolver", { root: ["./"] }], "transform-decorators-legacy", "transform-class-properties" ] }, production: { presets: [ [ "minify", // why add minify here, we run uglify over the whole code bundles { mangle: false, evaluate: false } ] ], plugins: [ ["transform-define", enviroments], ["@babel/plugin-proposal-decorators", { legacy: true }], ["@babel/plugin-proposal-class-properties", { loose: true }], ["module-resolver", { root: ["./"] }] ], comments: false, compact: true, minified: true } } };
ããã§çããããã®åé¡ã«åæ Œã§ããããšãé¡ã£ãŠããŸãã 也æ¯ã
ããªãã®æ¹æ³ã®ããã®tksããã®æ¹æ³ã¯ç§ã®åé¡ãæ£ããä¿®æ£ããããšãã§ããŸãïŒ
ããã«ã¡ã¯-ç§ã¯ãã®ã¹ã¬ããã§ããã€ãã®ããšãè©ŠããŸããããäœãæ©èœããŠããªãããã§ãã äž»ãªãšã©ãŒã¯å¥åŠãªb / cã§ããããœãŒã¹ã³ãŒããšå®è¡ããããšããŠããå®éã®ã³ãŒããæŽåããŠããªãããã«ãæå³ããªããªãè¡çªå·ãæããŠããããã§ãã
ãã®ãšã©ãŒãJestã¯äºæããªãããŒã¯ã³ãæ€åºããŸããããæ®ããŸãã ç§ã¯Jest> 24ã䜿çšããŠãããã¿ã€ãã¹ã¯ãªããã¯ãããŸãããããæè¿ïŒ6ãæåãïŒã«react-create-appãæåºãããŸããã ãã£ãã·ã¥ãã¯ãªã¢ããŠnode_modulesãåé€ããŠãå¹æã¯ãããŸããã§ããã ãããã誰ãããjestãé©åã«å€æããŠå®è¡ããããã«å¿ èŠãªæ§æãšãç§ã®æ£ç¢ºãªãšã©ãŒãäžãããã³ããæ£ç¢ºã«èª¬æããããšãã§ããŸã...
FAIL __e2e__/chat/sms.spec.js
â Test suite failed to run
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
Here's what you can do:
⢠To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
⢠If you need a custom transformation specify a "transform" option in your config.
⢠If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html
Details:
/Users/ben/dev/app/webui/node_modules/babel-preset-react-app/node_modules/@babel/runtime/helpers/esm/asyncToGenerator.js:17
export default function _asyncToGenerator(fn) {
^^^^^^
SyntaxError: Unexpected token export
5 | afterEach(async () => {
6 | await logout();
> 7 |
| ^
8 | await page.evaluate(() => {
9 | localStorage.clear();
10 | });
at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:471:17)
at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:513:25)
at Object.<anonymous> (__e2e__/chat/sms.spec.js:7:49)
çãããããããšãããããŸããã
Jenkinsã¯ãã®åé¡ãåå ã§å€±æããŸãããããã€ãã®å®çšçãªè§£æ±ºçã¯ãããŸããïŒ
Create-React-Appã䜿çšããŠããå ŽåãCRAã䜿çšãããšpackage.jsonã§å€æŽã§ããã®ã¯ç¹å®ã®jestæ§æã®ã¿ã§ãããããpackage.jsonã«transformignorepatterns
ãè¿œå ããŠãåé¡ã¯è§£æ±ºããŸããã
Jestãå éšã©ã€ãã©ãªãååŸããéã«åé¡ãçºçããŸããããã®ã©ã€ãã©ãªããã€ã³ããŒãããå ŽåãJestã¯ãäºæããªãããŒã¯ã³ããšã©ãŒã衚瀺ããŠããŸããã
ããã解決ããã«ã¯ããã¹ãã¹ã¯ãªããã次ã®ããã«å€æŽããŸãã
"test": "react-scripts test --transformIgnorePatterns 'node_modules/(?!(<your-package-goes-here>)/)'",
ã¿ãªãããããã«ã¡ã¯ãç§ã¯JetSetãšåŒã°ããReactNativeã§èšè¿°ãããå°ããªãããžã§ã¯ãã§åäœãã¹ããè¡ãã®ã«èŠåŽããŠããŸããã ç§ã¯ãã®åé¡ã解決ããããã«2æ¥éæ€çŽ¢ããbabel.config.jsã®ãããªãã®ãç·šéãããã®ãã¡ã€ã«ãåé€ããŠãã.babelãã¡ã€ã«ãäœæããããšããŸããïŒãããŠããã€ãã®ããããšãè¿œå ããŸããïŒã
_ãã°ã¬ããŒãïŒ_
DïŒ\ jetset \ JetSetApp> jest SaveData.test.js
FAIL __test __ / SaveData.test.js
âãã¹ãã¹ã€ãŒãã®å®è¡ã«å€±æããŸãã
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
Here's what you can do:
⢠To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
⢠If you need a custom transformation specify a "transform" option in your config.
⢠If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html
Details:
D:\jetset\JetSetApp\node_modules\expo-file-system\build\index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import * as FileSystem from './FileSystem';
^
SyntaxError: Unexpected token *
1 | import React, { Component } from 'react'
2 | import { View, Text, TouchableOpacity, TextInput, StyleSheet } from 'react-native'
> 3 | import { documentDirectory, getInfoAsync, readDirectoryAsync, readAsStringAsync, writeAsStringAsync, deleteAsync } from 'expo-file-system';
| ^
4 |
5 | /**
6 | * This class primarily serves to handle files within the phone system so that results can be stored
at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:537:17)
at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:579:25)
at Object.<anonymous> (app/resources/SaveData/SaveData.js:3:1)
ãã¹ãã¹ã€ãŒãïŒ1ã€å€±æãåèš1ã€
ãã¹ãïŒåèš0
ã¹ãããã·ã§ããïŒåèš0
æéïŒ3.509ç§
/SaveData.test.js/iã«äžèŽãããã¹ãŠã®ãã¹ãã¹ã€ãŒããå®è¡ããŸããã
_ babel.config.js _
module.exports = functionïŒapiïŒ{
api.cacheïŒtrueïŒ;
æ»ã {
ããªã»ããïŒ['babel-preset-expo']ã
};
};
_ package.json _
{{
"main"ïŒ "node_modules / expo / AppEntry.js"ã
ãã¹ã¯ãªãããïŒ{
ãã¹ã¿ãŒããïŒããšãã¹ãã¹ã¿ãŒããã
"android"ïŒ "expo start --android"ã
"ios"ïŒ "expo start --ios"ã
"web"ïŒ "expo start --web"ã
ãã€ãžã§ã¯ããïŒããšã¯ã¹ãã€ãžã§ã¯ããã
ããã¹ããïŒãåè«ã
}ã
ãåè«ãïŒ{
ãããªã»ãããïŒãreact-nativeã
}ã
ãäŸåé¢ä¿ãïŒ{
"expo"ïŒ "^ 34.0.1"ã
"expo-file-system"ïŒ "^ 6.0.2"ã
"expo-mail-composer"ïŒ "^ 6.0.0"ã
"ã¢ãŒã¡ã³ã"ïŒ "^ 2.24.0"ã
"react"ïŒ "16.8.3"ã
"react-dom"ïŒ "^ 16.8.6"ã
"react-native"ïŒ " https://github.com/expo/react-native/archive/sdk-34.0.0.tar.gz "ã
"react-native-gesture-handler"ïŒ "^ 1.3.0"ã
"react-native-selection-group"ïŒ "^ 1.1.2"ã
"react-native-web"ïŒ "^ 0.11.4"
}ã
"devDependencies"ïŒ{
"@ babel / core"ïŒ "^ 7.6.2"ã
"@ babel / preset-env"ïŒ "^ 7.6.2"ã
"babel-jest"ïŒ "^ 24.9.0"ã
"babel-preset-expo"ïŒ "^ 6.0.0"ã
"jest"ïŒ "^ 24.9.0"ã
"prop-types"ïŒ "^ 15.7.2"ã
"react-native-dismiss-keyboard"ïŒ "^ 1.0.0"ã
"react-native-modal-datetime-picker"ïŒ "^ 7.5.0"ã
"react-navigation"ïŒ "^ 3.12.1"
}ã
ããã©ã€ããŒããïŒtrue
}
åèãŸã§ã«ãNODE_ENV = "test"ãèšå®ãããŠããªããšãã«ãã·ã¹ãã å šäœãããŸãåäœããªãã£ãããã«ãããã«å¯Ÿããæ¬åœã«ã°ããã解決çãèŠã€ãããšæããŸãã
React Jestã®æ°ããããŒãžã§ã³ã§ã¯ããããã£ãŠpackage.jsonã«çµã¿èŸŒãŸããŠããããããã¹ãã¹ã¯ãªããã¯æ¬¡ã®ããã«ãªããŸãã
"test": "react-scripts test"
ãã以å€ã®"test": "jest"
React Jestã®æ°ããããŒãžã§ã³ã§ã¯ããããã£ãŠpackage.jsonã«çµã¿èŸŒãŸããŠããããããã¹ãã¹ã¯ãªããã¯æ¬¡ã®ããã«ãªããŸãã
"test": "react-scripts test"
ãã以å€ã®"test": "jest"
ã©ããããããšããããã¯ç§ã«ãšã£ãŠåé¡ã解決ããŸããïŒ
LOTãè©ŠããåŸãç§ã«ãšã£ãŠã®åé¡ïŒ
.babelrcãã¡ã€ã«ã«"transform-es2015-modules-commonjs",
ããããŸãã
{
"presets": ["next/babel"],
"plugins": [
["styled-components", { "ssr": true, "displayName": true, "preprocess": false } ],
"transform-flow-strip-types",
"jsx-control-statements",
"import-glob",
"transform-es2015-modules-commonjs",
[
"import",
{
"libraryName": "antd",
"style": "css"
}
]
]
}
"jest"ïŒ "^ 24.9.0"ã
"babel-jest"ïŒ "23.6.0"ã
ããªãã®jestãšbabel-jestãæŽæ°ããŠã¿ãŠãã ãã
.babelrc
ååãbabel.config.js
ã«å€æŽããBabel7ã«ã¢ããã°ã¬ãŒãããŠãã以åãšåããšã©ãŒãçºçããŸãã ç§ã®babel.config.js
ãã¡ã€ã«ïŒmodule.exports = function (api) { api.cache(true); const presets = ["@babel/preset-env"]; return { presets }; }
@babel/preset-react
ãã€ã³ã¹ããŒã«ããŠãbabel.config.js
ãã¡ã€ã«ã«è¿œå ããŸããã ãšã©ãŒãä¿®æ£ããŸããã
ããã§åé¡ãä¿®æ£ãããŸããã@ babel / prefix-envãã€ã³ã¹ããŒã«ããŠbabel.config.jsã«è¿œå ãããšãä¿®æ£ãããŸããã
ããããšã@jmayergit
babel.rcãã¡ã€ã«ãbabel.config.jsãã¡ã€ã«ã«å€æŽããŸããããæ©èœããŸããã§ããã ããããç§ã¯ãã®çããèŠãŸããïŒ https ïŒ
ã€ã³ããŒããã©ã°ã€ã³ãããå Žåã¯ããã¹ããéçºãæ¬çªç°å¢ã®ãã©ã°ã€ã³ãåé¢ããŸãã
ç§ã®babel.config.jsãã¡ã€ã«ã¯æ¬¡ã®ããã«ãªããŸãã
module.exports = {
presets: [
[
'next/babel',
],
],
env: {
test: {
},
dev: {
plugins: [
['import', { libraryName: 'antd', style: 'css' }],
],
},
production: {
plugins: [
['import', { libraryName: 'antd', style: 'css' }],
],
},
},
};
ç§ã5æ¥ãããã®ãšã©ãŒã«èŠããã§ããŸã
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
Here's what you can do:
⢠To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
⢠If you need a custom transformation specify a "transform" option in your config.
⢠If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html
Details:
SyntaxError: C:\Users\SK-PC\Desktop\React-Course\expansify\src\test\components\MyNavbar.test.js: Unexpected token (5:28)
3 | import { MyNavbar } from '../../components/MyNavbar'
4 | test("Should return MyNavbar component", () => {
> 5 | const wrapper = shallow(<MyNavbar />)
| ^
6 | expect(wrapper.find('.navbar-brand').find('kbd').text()).toBe("Expensify")
7 | // const renderer = new ReactShallowRenderer()
8 | // renderer.render(<MyNavbar />)
at Parser._raise (node_modules/@babel/parser/src/parser/location.js:241:45)
at Parser.raiseWithData (node_modules/@babel/parser/src/parser/location.js:236:17)
at Parser.raise (node_modules/@babel/parser/src/parser/location.js:220:17)
at Parser.unexpected (node_modules/@babel/parser/src/parser/util.js:149:16)
at Parser.parseExprAtom (node_modules/@babel/parser/src/parser/expression.js:1144:20)
at Parser.parseExprSubscripts (node_modules/@babel/parser/src/parser/expression.js:539:23)
at Parser.parseMaybeUnary (node_modules/@babel/parser/src/parser/expression.js:519:21)
at Parser.parseExprOps (node_modules/@babel/parser/src/parser/expression.js:311:23)
at Parser.parseMaybeConditional (node_modules/@babel/parser/src/parser/expression.js:263:23)
at Parser.parseMaybeAssign (node_modules/@babel/parser/src/parser/expression.js:211:21)
PASS src/test/reducers/filters.test.js
Test Suites: 1 failed, 5 passed, 6 total
Tests: 29 passed, 29 total
Snapshots: 0 total
Time: 12.144s
Ran all test suites.
@fullstacksk
React Jestã®æ°ããããŒãžã§ã³ã§ã¯ããããã£ãŠpackage.jsonã«çµã¿èŸŒãŸããŠããããããã¹ãã¹ã¯ãªããã¯æ¬¡ã®ããã«ãªããŸãã
"test": "react-scripts test"
ãã以å€ã®"test": "jest"
次ã®å Žæã§ããã¥ã¡ã³ããèªãããšãã§ããŸãã
https://jestjs.io/docs/en/tutorial-react
https://create-react-app.dev/docs/running-tests/#docsNav
@ catmans1
create-react-appãreact-scriptsã®ãããªäŸåé¢ä¿ãã€ã³ã¹ããŒã«ããŠããŸããã
ããã¯æ©èœããŸããïŒ
"test": "react-scripts test"
ãã以å€ã®"test": "jest"
envãšreactã§ãªãã·ã§ã³ããã¹ãããããã«babel-preset-jestã䜿çšãããšããããã¯æ©èœããŠããŸãã babel-jeastãåé€ããŸããã
ç§ã®ããã«åããïŒ babel.config.js
ã@babel / preset-reactããå«ãã
https://medium.com/@audreyhal/how -to-solve-jest-error-with-create-react-app-part-1-80f33aa1661a
æããªééããç¯ããæåã®ãã¹ããã¡ã€ã«ã.tsx
æ¡åŒµåã§ã¯ãªã.ts
æžã蟌ãã åŸããã®ãšã©ãŒãçºçããŸããã å
ã®åé¡ããã¯ã»ã©é ãã§ããã誰ãã«åœ¹ç«ã€ãããããŸãããð
æãåèã«ãªãã³ã¡ã³ã
3æ¥åŸãç§ã¯ãã®åé¡ã®è§£æ±ºçãèŠã€ããŸããã
ããã«ã¯2ã€ã®è§£æ±ºçããããŸãã
解決ç1ïŒè§£æ±ºç1ïŒ
ãã¡ã€ã«åã
.babelrc
ããbabel.config.js
ã«å€æŽã§ããããã¯æ©èœããŸãã##解決ç2ïŒ
ç§ã¯ãã®ããã«äœ¿çšããŠããŸãã
次ã®ãããªå€æãã¡ã€ã«ãäœæããŸãïŒç§ã®ãã®ã¯
jest-transforme.js
ïŒïŒãããç§ã®
jest.config.js
ã§ãïŒãããŠãç§ã¯ãŸã ç§ã®
.babelrc
æã£ãŠããŸã`` `
const enviroments = requireïŒ "./ env-config.js"ïŒ;
module.exports = {
ããªã»ããïŒ[
[
ãnext / babelãã
{{
"preset-env"ïŒ{
useBuiltInsïŒ "ãšã³ããª"
}
}
]
]ã
ãã©ã°ã€ã³ïŒ[
["styled-components"ã{ssrïŒtrueãdisplayNameïŒtrueãpreprocessïŒfalse}]ã
[
ãã¢ãžã¥ãŒã«ãªãŸã«ããã
{{
æ ¹ïŒ ["ã/"]
}
]
]ã
envïŒ{
devïŒ{
ãã©ã°ã€ã³ïŒ[
["transform-define"ãç°å¢]ã
["module-resolver"ã{rootïŒ["./"]}]ã
ãtransform-decorators-legacyãã
ãtransform-class-propertiesã
]
}ã
建ãŠãïŒ {
ãã©ã°ã€ã³ïŒ[
["transform-define"ãç°å¢]ã
["module-resolver"ã{rootïŒ["./"]}]ã
ãtransform-decorators-legacyãã
ãtransform-class-propertiesã
]
}ã
補é ïŒ {
ããªã»ããïŒ[
[
"minify"ã//ããã«minifyãè¿œå ããçç±ãã³ãŒããã³ãã«å šäœã«å¯ŸããŠuglifyãå®è¡ãã
{{
ãã³ã°ã«ïŒfalseã
è©äŸ¡ïŒfalse
}
]
]ã
ãã©ã°ã€ã³ïŒ[
["transform-define"ãç°å¢]ã
["@ babel / plugin-proposal-decorators"ã{ã¬ã¬ã·ãŒïŒtrue}]ã
["@ babel / plugin-proposal-class-properties"ã{looseïŒtrue}]ã
["module-resolver"ã{rootïŒ["./"]}]
]ã
ã³ã¡ã³ãïŒfalseã
ã³ã³ãã¯ãïŒæ¬åœã
çž®å°ïŒtrue
}
}
};
ãã
ããã§çããããã®åé¡ã«åæ Œã§ããããšãé¡ã£ãŠããŸãã 也æ¯ã