我在运行jest测试时遇到问题.我得到了一个意外的标识符
我已经try 过清理npm缓存和installing npm babel jest、polyfill、preset-es2015.我还try 了一些不同的配置.
这是我的babel.config.js
美元
module.exports = {
presets: [
'@vue/app'
],
env: {
test: {
plugins: [
"transform-strict-mode",
"transform-es2015-modules-commonjs",
"transform-es2015-spread",
"transform-es2015-destructuring",
"transform-es2015-parameters"
]
}
}
}
还有我的jest config
元Package.json
"jest": {
"moduleFileExtensions": [
"js",
"json",
"vue"
],
"transform": {
"^.+\\.js$": "babel-jest",
".*\\.(vue)$": "vue-jest"
},
"moduleNameMapper": {
"^@/(.*)$": "<rootDir>/src/$1"
}
}
这里是错误.
Test suite failed to run
PCROUTE\Test.test.js:3
import _interopRequireDefault from "PCROUTE\\node_modules\\@babel\\runtime-corejs2/helpers/esm/interopRequireDefault";
^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Unexpected identifier
我希望通过考试,但我收到了错误信息.我认为问题在于《巴别塔》中的env部分.配置.js
Edit: I'm also using windows 7,我认为这可能会导致前后混合的斜杠.
Edit2: I just tested it in Linux and it doesn't work.我都是前斜杠
PCROUTE/src/tests/Test.test.js:3
import _interopRequireDefault from "PCROUTE/node_modules/@babel/runtime-corejs2/helpers/esm/interopRequireDefault";
^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Unexpected identifier
at ScriptTransformer._transformAndBuildScript
Edit 3: I saw a lot of people fixed it with [these lines](https://github.com/vuejs/vue-cli/issues/1879#issuecomment-409872897).
transformIgnorePatterns: [
"node_modules/(?!(babel-jest|jest-vue-preprocessor)/)"
]
在jest config file
人中.我添加了它们并运行了./node_modules/jest/bin/jest.js --clearCache
,但它不起作用.
Edit 4:我刚刚在我的Jest 文件中添加了一些东西.删除了我babel文件中的一对
新Jest 文件
module.exports = {
moduleFileExtensions: [
'js',
'jsx',
'json',
'vue'
],
transform: {
'^.+\\.vue$': 'vue-jest',
'.+\\.(css|styl|less|sass|scss|svg|png|jpg|ttf|woff|woff2)$': 'jest-transform-stub',
'^.+\\.jsx?$': 'babel-jest'
},
transformIgnorePatterns: [
'/node_modules/'
],
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1'
},
snapshotSerializers: [
'jest-serializer-vue'
],
testMatch: [
'**/tests/**/*.test.(js|jsx|ts|tsx)|**/__tests__/*.(js|jsx|ts|tsx)'
],
testURL: 'http://localhost/',
watchPlugins: [
'jest-watch-typeahead/filename',
'jest-watch-typeahead/testname'
]
}
除presets: @vue/app
部分外,我还有deleted everything from my babel file部分.
这是我得到的新错误.
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import "core-js/modules/es6.array.find";
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Unexpected string