【问题标题】:VSCode not stopping at breakpoints when test launched using npm and package.json scripts使用 npm 和 package.json 脚本启动测试时,VSCode 不会在断点处停止
【发布时间】:2019-11-15 09:30:27
【问题描述】:

VSCode 我想启动当前文件(例如 my-function.spec.ts)并进行交互式调试设置断点。

为了运行测试,我需要设置一些环境变量,例如MONGO=mongodb://localhost:27017/。为此,我通过 npm 脚本 启动测试,并使用 launch.json 中定义的配置的"envFile" 属性传递环境变量。

launch.json

   "configurations": [
        {
            "name": "Current TS Tests File",
            "type": "node",
            "request": "launch",
            "runtimeExecutable": "npm",
            "args": ["${relativeFile}"],
            "cwd": "${workspaceRoot}",
            "protocol": "inspector",
            "envFile": "${workspaceFolder}/.env",
            "runtimeArgs": ["run-script", "test-one"]
        },
    ]
}

package.json 脚本是

"scripts": {
   "test-one": "npm mocha -r ts-node/register",
}

使用此配置,我可以启动测试。测试按预期执行,但代码不会在我设置的断点处停止执行。关于如何使断点起作用的任何建议?

【问题讨论】:

    标签: typescript visual-studio-code vscode-debugger


    【解决方案1】:

    以下配置适用于我的机器。源代码是in this GitHub repository. 从调试器运行Launch via NPM 配置会在assert 处遇到断点。

    有更多关于使用 node-ts 和 VS Code in this node-ts issue 进行调试的详细信息。如果您需要更多帮助来将此设置映射到您的要求,请告诉我。

    package.json

    {
      "scripts": {
        "test": "mocha -r ts-node/register --inspect --debug-brk index.test.ts"
      },
      "devDependencies": {
        "@types/mocha": "^5.2.7",
        "@types/node": "^12.0.12",
        "mocha": "^6.1.4",
        "ts-node": "^8.3.0",
        "typescript": "^3.5.2"
      }
    }
    

    launch.json

    {
      "configurations": [
        {
          "type": "node",
          "request": "launch",
          "name": "Launch via NPM",
          "runtimeExecutable": "npm",
          "runtimeArgs": ["run-script", "test"],
          "port": 9229
        }
      ]
    }
    

    tsconfig.json

    {
      "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "strict": true,
        "esModuleInterop": true
      }
    }
    

    index.test.ts

    import assert from 'assert';
    
    describe('index', function () {
        it('should pass', function () {
            assert.equal(true, true);
        });
    });
    

    【讨论】:

    • 太棒了。 ` --inspect --debug-brk ` 对我有用。谢谢
    猜你喜欢
    • 1970-01-01
    • 2021-04-16
    • 2021-12-24
    • 2019-12-12
    • 2018-01-11
    • 2020-02-08
    • 1970-01-01
    • 2021-06-29
    • 2021-07-21
    相关资源
    最近更新 更多