gpt4 book ai didi

javascript - 导入意外标识符 + SyntaxError : Unexpected string

转载 作者:可可西里 更新时间:2023-11-01 02:47:01 24 4
gpt4 key购买 nike

我在运行 Jest 测试时遇到了问题。我收到一个导入的意外标识符

我已经尝试清理 npm 缓存和 installing npm babel jest,polyfill,preset-es2015。我也read this在这里和那里尝试一些不同的配置。

这是我的 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"
]
}
}
}

和我在 package.json 中的 jest 配置

"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

我希望通过测试,但我收到了错误消息。我认为问题出在 babel.config.js 中的 env 部分

编辑:我也在使用 windows 7,这可能会导致前后混合斜杠。

Edit2:我刚在 Linux 上测试过它,但它不起作用。我所有的都是正斜杠

 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

编辑 3:我看到很多人用[这些行]修复了它( https://github.com/vuejs/vue-cli/issues/1879#issuecomment-409872897 )。

transformIgnorePatterns: [
"node_modules/(?!(babel-jest|jest-vue-preprocessor)/)"
]

jest 配置文件 中。我添加了它们并运行了这个 ./node_modules/jest/bin/jest.js --clearCache 但它不起作用。

编辑 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'
]
}

删除了我的 babel 文件中的所有内容,除了 presets: @vue/app 部分。

这是我遇到的新错误。

({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import "core-js/modules/es6.array.find";
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

SyntaxError: Unexpected string

最佳答案

经过几天的努力。最后我回答了我的问题。在这里,我保留了所有配置文件。

jest.config.js

process.env.VUE_CLI_BABEL_TARGET_NODE = true;
process.env.VUE_CLI_BABEL_TRANSPILE_MODULES = true;

module.exports = {
moduleFileExtensions: [
'js',
'jsx',
'json',
'vue'
],
transform: {
'^.+\\.vue$': '<rootDir>/node_modules/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'
]
}

babel.config.js

module.exports = {
presets: [
'@vue/app'
]
}

注意事项

  • 我需要添加 babel core bridge能够在 vue-jest 中使用 @babel/core
  • 建议修改文件后清除jest的缓存

关于javascript - 导入意外标识符 + SyntaxError : Unexpected string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56217958/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com