【问题标题】:Using VSCode Debugger for Jest Tests throws error "SyntaxError: Cannot use import statement outside a module"使用 VSCode 调试器进行 Jest 测试会引发错误“SyntaxError:无法在模块外使用导入语句”
【发布时间】: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


    【解决方案1】:

    最后,我无法完全“解决”我的问题(即使在知识渊博的人的帮助下),我不得不从一个干净的原版 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,
      };
    };
    

    【讨论】:

    • 我在尝试在具有多个子包的 monorepo 中调试 Typescript Jest 测试时遇到了同样的问题。我的错误是在根文件夹中启动 VSCode。如果我只打开我想要工作的子包文件夹,那么问题就会消失,我可以调试。
    猜你喜欢
    • 1970-01-01
    • 2021-02-22
    • 1970-01-01
    • 1970-01-01
    • 2021-07-02
    • 2021-01-05
    • 2020-03-15
    • 2021-07-08
    • 1970-01-01
    相关资源
    最近更新 更多