当我运行npm test
(jest --watch
)时,我得到:
C:\sites\YDR\branches\trunk\development\app\static\js\_modules\marketplace\postbacks\Components\Form.js:44
import('Form').then(function (m) {
^^^^^^
SyntaxError: Unexpected token import
at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/ScriptTransformer.js:289:17)
at Object.<anonymous> (__tests__/postbacks-form.js:1:115)
Jest的一部分是package.json:
"jest": {
"rootDir": "./../../",
"modulePaths": [
"<rootDir>tools/webpack/node_modules/",
"<rootDir>development/app/static/css/_modules/",
"<rootDir>development/app/static/js/_modules/"
],
"transform": {
"^.+\\.js$": "<rootDir>/tools/webpack/jest.transform.js"
}
}
jest.transform.js:
// Custom Jest transform implementation that wraps babel-jest and injects our
// babel presets, so we don't have to use .babelrc.
module.exports = require('babel-jest').createTransformer({
presets: [
require("babel-preset-react"),
require("babel-preset-es2015")
],
plugins: [
require("babel-plugin-syntax-dynamic-import"),
]
});
我使用以下版本:
我也尝试过babel-plugin-dynamic-import-node@1.0.2,但后来我有不同的错误:
Plugin 0 specified in "base" provided an invalid property of "default"
答案 0 :(得分:0)
解决。它是babel-plugin-dynamic-import-node中的bug。
可以通过将.default
添加到require:
module.exports = require('babel-jest').createTransformer({
presets: [
require("babel-preset-react"),
require("babel-preset-es2015")
],
plugins: [
require("babel-plugin-transform-es2015-modules-commonjs"),
require("babel-plugin-dynamic-import-node").default,
]
});
此外,为了能够解析import()
,需要babel-plugin-transform-es2015-modules-commonjs。