我尝试了很多事情,以至于在这个阶段我可能只会变得更糟。有很多相关的问题,但是没有什么可以解决我的问题。我显然做错了。
我想在我的Jest测试中使用VSCode调试器。我在使用require&module.export ..时进行了此工作。通过导入和导出切换到ES6模块时,出现错误“ SyntaxError:无法在模块外部使用import语句”
我尝试使它与babel配置一起使用,然后立即阅读,因为我们支持不需要babel的ES6模块。我真的不介意我如何工作。
典型的出口就像
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
}
}
]
]
}
让我知道我还有什么可以提供的帮助!
谢谢!
答案 0 :(得分:0)
最后,我无法完全“解决”我的问题(即使是在一个知识渊博的人的帮助下),我也不得不从一个干净的原始仓库重新开始,然后将我的文件粘贴进去。
但是,我认为我的问题很大一部分是如何打开vscode的。我打开了我的主“ coding”文件夹(所有项目一次可见),并且在所有这些项目之外都有一个launch.json文件。由于launch.json文件指向特定目录,因此我不确定此“全局”启动文件是否可以工作。
也许还有一种方法可以打开所有项目的文件夹,其中每个项目都有自己的launch.json文件,但我还没有开始使用它。
相反,当我导航到项目的根目录和A B
PLACE POINT
1 10
2 5
3 2
4 1
并在项目根目录使用其自己的launch.json文件时,它起作用。
code .
package.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
}
]
}
babel.config.js
{
"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",
}
}