【发布时间】:2020-11-19 22:51:12
【问题描述】:
我已经尝试了很多事情,在这个阶段我可能只会让事情变得更糟。有很多相关的问题,但没有解决我的问题。我显然做错了什么。
我想在我的 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"
}
}
Launch.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
}
}
]
]
}
如果有什么我可以发布的额外帮助,请告诉我!
谢谢!
【问题讨论】:
标签: javascript ecmascript-6 jestjs es6-modules babel-jest