node.jstypescriptvisual-studio-codetesting

How can I create a node test launch configuration?


I have some "node 18 test runner" tests written. I can run them with this command:

node --loader tsx --test tests/**/*.ts

I want to be able to debug the tests in vscode, so I figured I should have a launch.json configuration entry for that. But how do I do that?

This is what I tried:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "test",
            "type": "node",
            "request": "launch",
            "args": [
                "--loader",
                "tsx",
                "--test",
                "--test-reporter=spec",
                "tests/**/*.ts"
            ],
            "console": "integratedTerminal",
            "sourceMaps": true,
            "internalConsoleOptions": "neverOpen",
        },
    ]
}

When I run this configuration, this is the executed command:

❯ node --loader tsx --test tests/**/*.ts cd /home/birger/someproject ; /usr/bin/env 'NODE_OPTIONS=--require /usr/share/code/resources/app/extensions/ms-vscode.js-debug/src/bootloader.js --ins
pect-publish-uid=http' 'VSCODE_INSPECTOR_OPTIONS={"inspectorIpc":"/tmp/node-cdp.165926-f285dca5-96.sock","deferredMode":false,"waitForDebugger":"","execPath":"/home/birger/.nvm/versions/node/v18.15.0/bin/node","o
nlyEntrypoint":false,"autoAttachMode":"always","fileCallback":"/tmp/node-debug-callback-4ea344155215e4e5"}' /home/birger/.nvm/versions/node/v18.15.0/bin/node --loader tsx --test --test-reporter=spec tests/\*\*/\*
.ts

This is the output:

Could not find '/home/birger/someproject/cd'
Debugger attached.
Could not find '/home/birger/someproject/tests/**/*.ts'
Waiting for the debugger to disconnect...

The command ls /home/birger/someproject/tests/ yields a list of 4 .ts files.

How can I write a vscode launch.json configuration to debug the node 18 test runner tests?


Solution

  • Node version: v19.9.0

    Project folder structure:

    ☁  node-test-runner  tree -L 4 -I 'node_modules'         
    .
    ├── package-lock.json
    ├── package.json
    ├── tests
    │   └── modules
    │       └── a.test.ts
    └── tsconfig.json
    

    tests/modules/a.test.ts:

    import test from 'node:test';
    import assert from 'assert';
    
    test('synchronous passing test', () => {
      assert.strictEqual(1, 1);
      const a = '123';
      debugger
    });
    

    package.json:

    {
      "name": "test-runner",
      "version": "1.0.0",
      "type": "module",
      "directories": {
        "test": "tests"
      },
      "scripts": {
        "test:vscode": "globstar -- node --loader ts-node/esm --test tests/**/*.test.ts"
      },
      "devDependencies": {
        "globstar": "^1.0.0",
        "ts-node": "^10.9.1",
        "typescript": "^5.0.4"
      }
    }
    

    .vscode/launch.json:

    {
      // Use IntelliSense to learn about possible attributes.
      // Hover to view descriptions of existing attributes.
      // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
      "version": "0.2.0",
      "configurations": [
        {
          "type": "node",
          "request": "launch",
          "name": "test",
          "skipFiles": [
            "<node_internals>/**"
          ],
          "runtimeExecutable": "npm",
          "runtimeArgs": [
            "run-script",
            "test:vscode"
          ],
          "console": "integratedTerminal",
          "cwd": "${workspaceFolder}"
        }
      ]
    }
    

    Click the "RUN AND DEBUG" arrow button, result:

    enter image description here