Tips: How to debugger Unit-Test (Jest, Mocha)

Tips: How to debugger Unit-Test (Jest, Mocha)

·

1 min read

Enable the debugger

In VSCode, open the command palette, and search for "JavaScript Debug Terminal"

Set a breakpoint in your code:

The red dot indicates a breakpoint

Then, run your code normally with npm start or npm run test.

Skip Node-modules

.vscode/settings.json

{
  "debug.javascript.terminalOptions": {
    "skipFiles": [
      "${workspaceFolder}/node_modules/**/*",
      "<node_internals>/**"
    ]
  }
}

Tests will time out

    "test:mocha:debug": "nyc mocha --timeout 300000",
    "test:jest:debug": "jest --testTimeout 300000"

Parallelism can bite you

    "test:mocha:debug": "nyc mocha --timeout 300000 --jobs 1",
    "test:jest:debug": "jest --testTimeout 300000 --maxWorkers 1"