使用 VSCode 调试器进行 Jest 测试会抛出错误“SyntaxError:无法在模块外部使用 import 语句”

问题描述 投票:0回答:2

我已经尝试了很多事情,在这个阶段我可能会让事情变得更糟。有很多相关的问题,但没有解决我的问题。我显然做错了什么。

我想在 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"
        }
    ]
}

错误信息 error message

文件结构 file structure

在尝试一百万件事时,我最终得到了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
2个回答
0
投票

最后,我无法完全“解决”我的问题(即使在知识渊博的人的帮助下),我不得不从一个干净的香草仓库重新开始,然后将我的文件粘贴进去。

但是,我认为我的问题很大一部分是我如何打开 vscode。我打开了主“编码”文件夹(因此所有项目立即可见),并在所有这些项目之外有一个 launch.json 文件。由于 launch.json 文件指向特定目录,我不确定这个“全局”启动文件是否可以工作。

也许有一种方法仍然可以打开包含所有项目的文件夹,其中每个项目都有自己的 launch.json 文件,但我还没有让它工作。

相反,当我导航到项目的根目录并在项目根目录下使用其自己的 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", } }



0
投票

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.