Skip to content

How to debug jest tests? #27117

Open
Enhancement
@nicojs

Description

@nicojs

Command

test

Description

How does one debug jest tests?

Debugging a normal jest test run is documented here: https://jestjs.io/docs/troubleshooting#debugging-in-vs-code. It describes using jest with --runInBand. However, there is no way to use --runInBand with ng test:

ng test --help
ng test [project]

Runs unit tests in a project.

Arguments:
  project  The name of the project to build. Can be an application or a library.                                                                                                                                                                                                                                                                                                                                [string] [choices: "datepicker"]

Options:
      --help           Shows a help message for this command in the console.                                                                                                                                                                                                                                                                                                                                                           [boolean]
  -c, --configuration  One or more named builder configurations as a comma-separated list as specified in the "configurations" section in angular.json.
                       The builder uses the named configurations to run the given target.
                       For more information, see https://angular.io/guide/workspace-config#alternate-build-configurations.                                                                                                                                                                                                                                                                                   [string] [choices: "jest", "karma"]
      --exclude        Globs of files to exclude, relative to the project root.                                                                                                                                                                                                                                                                                                                                                          [array]
      --include        Globs of files to include, relative to project root.                                                                                                                                                                                                                                                                                                                                                              [array]
      --polyfills      A list of polyfills to include in the build. Can be a full path for a file, relative to the current workspace or module specifier. Example: 'zone.js'.                                                                                                                                                                                                                                                            [array]
      --ts-config      The name of the TypeScript configuration file.

See also related issue: #25434.

Describe the solution you'd like

In my comment I suggest a way to run ng build --test (or ng test --build). It can run the build step before jest runs. This allows us to run jest --runInBand ourselves.

Another approach would be to add a --runInBand ng test flag for jest.

Describe alternatives you've considered

I've currently invented a workaround. Unfortunately, without a proper ng test --build command, I had to revert to ng test || exit 0. This will build and run the tests once before debugging can start. The necessary sourceMapPathOverrides are hideous as well 🙈

launch.json
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "ng test (jest)",
      "request": "launch",
      "preLaunchTask": "npm: test:build",
      "runtimeArgs": [
        "--experimental-vm-modules",
        "./node_modules/jest/bin/jest.js",
        "--runInBand",
        "--rootDir",
        "${workspaceFolder}/dist/test-out/browser",
        "--testMatch",
        "**/?(*.)+(spec|test).mjs"
      ],
      "skipFiles": ["<node_internals>/**"],
      "type": "node",
      "sourceMapPathOverrides": {
        "src/app/*": "${workspaceFolder}/src/app/*"
      },
      "outputCapture": "std",
      "outFiles": ["${workspaceFolder}/dist/**/*.mjs"]
    }
  ]
}
package.json
{
  "scripts": {
    "test": "ng test",
    "test:build": "npm test || exit 0",
  }
}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions