Reputation: 17762
From within VSCode I would like to launch the current file (e.g. my-function.spec.ts) and do interactive debugging setting breakpoints.
For the test to run I need to set some environment variables, e.g. MONGO=mongodb://localhost:27017/
. For this reason I launch the test via an npm script and pass the environment variables using the "envFile"
property of the configurations defined in launch.json.
launch.json is
"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"]
},
]
}
the package.json script is
"scripts": {
"test-one": "npm mocha -r ts-node/register",
}
With this configuration I am able to launch the test. The test executes as expected, but the code does not stop execution at the breakpoints I set. Any suggestion on how to make breakpoints work?
Upvotes: 1
Views: 3458
Reputation: 141682
The following configuration works on my machine. The source code is in this GitHub repository. Running the Launch via NPM configuration from the debugger hits the breakpoint on assert
.
There are more details about debugging with node-ts
and VS Code in this node-ts
issue. Let me know if you need more help mapping this setup to your requirements.
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);
});
});
Upvotes: 2