angular / angular-cli

CLI tool for Angular
https://cli.angular.io
MIT License
26.77k stars 11.97k forks source link

How to debug jest tests? #27117

Open nicojs opened 8 months ago

nicojs commented 8 months ago

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 ```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": ["/**"], "type": "node", "sourceMapPathOverrides": { "src/app/*": "${workspaceFolder}/src/app/*" }, "outputCapture": "std", "outFiles": ["${workspaceFolder}/dist/**/*.mjs"] } ] } ```
package.json ```json { "scripts": { "test": "ng test", "test:build": "npm test || exit 0", } } ```
angular-robot[bot] commented 8 months ago

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 commented 8 months ago

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.

angular-robot[bot] commented 7 months ago

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.

angular-robot[bot] commented 6 months ago

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.