Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to debug jest tests? #27117

Open
nicojs opened this issue Feb 16, 2024 · 4 comments
Open

How to debug jest tests? #27117

nicojs opened this issue Feb 16, 2024 · 4 comments
Labels
area: @angular-devkit/build-angular devkit/build-angular:jest feature: insufficient votes Label to add when the not a sufficient number of votes or comments from unique authors feature Issue that requests a new feature

Comments

@nicojs
Copy link
Contributor

nicojs commented Feb 16, 2024

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",
  }
}
@dgp1130 dgp1130 added feature Issue that requests a new feature devkit/build-angular:jest labels Feb 20, 2024
@angular-robot angular-robot bot added the feature: votes required Feature request which is currently still in the voting phase label Feb 21, 2024
Copy link
Contributor

angular-robot bot commented Feb 21, 2024

This feature request is now candidate for our backlog! In the next phase, the community has 60 days to upvote. If the request receives more than 20 upvotes, we'll move it to our consideration list.

You can find more details about the feature request process in our documentation.

@johncrim
Copy link

johncrim commented Feb 23, 2024

I think a command to just build the jest tests (ideally with a --watch option), so that jest can be run as a separate command with a custom config and the full set of supported parameters, is going to be key for angular CLI jest support to work out well, at least in the short term. It's not as clean as a black box where everything is hidden from view, but given all the jest config options and things devs have put in their jest.config.ts I don't see a way around it.

Copy link
Contributor

angular-robot bot commented Apr 1, 2024

Just a heads up that we kicked off a community voting process for your feature request. There are 20 days until the voting process ends.

Find more details about Angular's feature request process in our documentation.

Copy link
Contributor

angular-robot bot commented Apr 20, 2024

Thank you for submitting your feature request! Looks like during the polling process it didn't collect a sufficient number of votes to move to the next stage.

We want to keep Angular rich and ergonomic and at the same time be mindful about its scope and learning journey. If you think your request could live outside Angular's scope, we'd encourage you to collaborate with the community on publishing it as an open source package.

You can find more details about the feature request process in our documentation.

@angular-robot angular-robot bot added feature: insufficient votes Label to add when the not a sufficient number of votes or comments from unique authors and removed feature: votes required Feature request which is currently still in the voting phase labels Apr 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: @angular-devkit/build-angular devkit/build-angular:jest feature: insufficient votes Label to add when the not a sufficient number of votes or comments from unique authors feature Issue that requests a new feature
Projects
None yet
Development

No branches or pull requests

4 participants