尝试调试 TestCafe 测试时 VS 代码出错

Error on VS code when trying to debug TestCafe test

我的 test.js 文件包含此行以便从页面-model.js 文件导入 'Page' class: 从“./page-model”导入页面;

当我使用命令 运行 test.js 通过 Shell 时:"npm test",它 运行 很好。

我尝试在 Visual studio 代码上调试我的 Testcafe 测试时遇到 "Unexpected identifier" 错误。 这是完整的信息: “

(function (exports, require, module, __filename, __dirname) { import Page from './page-model';
                                                                 ^^^^

SyntaxError: Unexpected identifier
    at new Script (vm.js:79:7)
    at createScript (vm.js:251:10)
    at Object.runInThisContext (vm.js:303:10)
    at Module._compile (internal/modules/cjs/loader.js:656:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:741:12)
    at startup (internal/bootstrap/node.js:285:19)
Waiting for the debugger to disconnect...

"

This are my dependencies on 'package.json' file:
  "devDependencies": {
    "babel-preset-es2015": "^6.24.1",
    "babel-register": "^6.26.0",
    "testcafe": "^0.23.1-alpha.4"
  }

谢谢!

您的 launch.json 配置在 program 属性:

中指定了您的测试脚本
"program": "${workspaceRoot}/testcafe-example/test.js" 

使用此配置,Visual Studio 代码尝试将您的脚本作为常规 Node.js 脚本执行,但失败了,因为 Node.js 不支持 import 语句。

在此 属性 中指定主 TestCafe 脚本,如 Debug in Visual Studio Code 配方中所述:

"program": "${workspaceRoot}/node_modules/testcafe/bin/testcafe.js"

如果要运行特定的测试脚本,请将"arguments" 属性中的"{relativePath}"变量替换为测试脚本的路径:

"program": "${workspaceRoot}/node_modules/testcafe/bin/testcafe.js"
"args": [
    "firefox",
    "${workspaceRoot}/testcafe-example/test.js"
],