![](/img/trans.png)
[英]SyntaxError: Cannot use import statement outside a module: when running Jest-expo tests
[英]Using VSCode Debugger for Jest Tests throws error “SyntaxError: Cannot use import statement outside a module”
我已经尝试了很多事情,在这个阶段我可能只会让它变得更糟。 有很多相关的问题,但没有解决我的问题。 我显然做错了什么。
我想在我的 Jest 测试中使用 VSCode 调试器。 当我使用 require 和 module.export 时,我有这个工作。使用 import 和 export 切换到 ES6 模块给了我错误“SyntaxError:无法在模块外使用 import 语句”
我试过让它与 babel 配置一起工作,然后读到现在支持 ES6 模块,我们不需要 babel。 我真的不介意我如何让它工作。
典型的出口就像
export default function buildIdentifiables(disposals, acquisitions) {
典型导入(尝试使用和不使用.js)
import buildIdentifiables from './buildIdentifiables.js'
我的 package.json
"name": "cgc-node",
"type": "module",
"version": "1.0.0",
"description": "",
"main": "main.js",
"scripts": {
"test": "jest"
},
"_moduleAliases": {
"@": "src"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.10.5",
"@babel/preset-env": "^7.10.4",
"babel-jest": "^26.1.0",
"jest": "^26.1.0",
"jest-environment-node": "^26.1.0"
},
"jest": {
"testEnvironment": "node",
"verbose": true,
"transform": {
"^.+\\.[t|j]sx?$": "babel-jest"
}
},
"babel": {
"presets": [
[
"@babel/env",
{
"targets": {
"node": true
}
}
]
]
},
"dependencies": {
"body-parser": "^1.19.0",
"currency.js": "^2.0.0",
"express": "^4.17.1",
"lodash": "^4.17.19",
"module-alias": "^2.2.2",
"moment": "^2.27.0",
"mysql": "github:mysqljs/mysql",
"mysql2": "^2.1.0",
"uuid": "^8.2.0"
}
}
发射.json
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/CGTC/cgc-node/main.js"
},
{
"name": "Jest",
"type": "node",
"request": "launch",
"env": { "NODE_ENV": "test" },
"cwd": "${workspaceRoot}",
"program": "${workspaceRoot}/CGTC/cgc-node/node_modules/.bin/jest",
"stopOnEntry": false,
"args": ["--config=${workspaceRoot}/CGTC/cgc-node/package.json"],
"runtimeArgs": ["--nolazy"],
"console": "internalConsole",
"sourceMaps": false,
"internalConsoleOptions": "openOnSessionStart",
// "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/babel-node"
}
]
}
在尝试一百万件事时,我最终得到了jest.config.js
module.exports = {
verbose: true,
testEnvironment: "node",
verbose: true,
transform: {
"^.+\\.[t|j]sx?$": "babel-jest"
}
};
和 babel.cofig.js
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": true
}
}
]
]
}
让我知道我是否可以发布任何额外的帮助!
谢谢!
最后,我无法完全“解决”我的问题(即使在知识渊博的人的帮助下),我不得不从一个干净的原版 repo 重新开始,然后将我的文件粘贴进去。
但是,我认为我的问题的很大一部分是我如何打开 vscode。 我打开了我的主要“编码”文件夹(因此所有项目一次可见)并且在所有这些项目之外有一个 launch.json 文件。 由于 launch.json 文件指向特定目录,我不确定这个“全局”启动文件是否可以工作。
也许有一种方法仍然可以打开包含所有项目的文件夹,每个项目都有自己的 launch.json 文件,但我还没有让它工作。
相反,当我导航到项目和code.
在项目根目录有自己的 launch.json 文件。
"version": "0.2.0",
"configurations": [
{
"name": "Debug Jest Tests",
"type": "node",
"request": "launch",
"runtimeArgs": [
"--inspect-brk",
"${workspaceRoot}/node_modules/.bin/jest",
"--runInBand"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"port": 9229
}
]
}
package.json
{
"name": "node-jest-example",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"test": "yarn jest"
},
"devDependencies": {
"@babel/preset-env": "^7.11.0"
},
"jest": {
"transform": {
"^.+\\.[t|j]sx?$": "babel-jest"
}
},
"dependencies": {
"jest": "^26.2.2",
}
}
babel.config.js
module.exports = (api) => {
api.cache(false);
const presets = [
[
"@babel/preset-env",
{
targets: {
esmodules: true,
node: "current",
},
},
],
];
return {
presets,
};
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.