Generate complete jest config for angular libraries
See original GitHub issueExpected Behavior
When generating a new angular library, a complete jest.config.js
file should be generated, including everything needed for jest-preset-angular.
This would allow using the jest CLI directly which has a few benefits:
- easier integration of vscode-jest
- ability to run a single unit test file without specifying the library first
- ability to run all unit tests in one go
Current Behavior
The jest.config.js
does not include all necessary config properties required to run the tests. Instead, those are patched by the nx jest builder, preventing configuration by the user.
Steps to Reproduce
- Create nx workspace
- Generate a new angular library
- Try to run unit tests using the Jest CLI (
cd <path_to_library_root> && jest
) - See failing tests
Solution
I would solve this by renaming skipSerializers
to something like useAngularPreset
, controlling all required properties for jest-preset-angular
.
I’m happy to do a PR for this if that’s the way to go.
Related
Issue Analytics
- State:
- Created 3 years ago
- Reactions:14
- Comments:15 (6 by maintainers)
Top Results From Across the Web
Migrate your Angular library to Jest | by Kevin Kreuzer - Medium
To configure Jest in a multi-projects file setup, we will create different Jest configurations. A base configuration in our root directory; A ...
Read more >Using jest to test an Angular Library? - Stack Overflow
Trying to test an Angular 9 library with Jest, and I've installed the dependencies for Jest and Typescript within the local library ......
Read more >Integrate Jest into an Angular application and library
The first thing to do is to create an application. ... The jest-preset-angular library will configure Jest and it will also configure the ......
Read more >From Jasmine to Jest and Testing library in Angular Projects
In your project root, create the setup-jest.ts file and import the angular preset. Copy. import 'jest ...
Read more >Angular Testing Series: How to add Jest to Angular project ...
Let's create jest.config.js file in your project's directory ... In first (1) line, we import the appropriate 'jest-preset-angular' library.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
For us it’s been resolved with the new
jest.preset.js
- thanks @Nx-team! 😄@wrslatz @jeserkin I just wanted to add the solution we are using to run tests using VSCode-Jest. This issue is somewhat of a requirement for it though (at least without touching generated libs), as all libs must have a working jest config.
I’ve uploaded the script on gist: https://gist.github.com/FERNman/14e3fe4086d6040cbb1f8e6174c9f0c6
What it does is pretty much a simplified version of
nx affected:test
combined with the Jest CLI. For some jest parameters (mainly arrays), the methodparseJestArgs
must probably be adapted.Also, it may require maintenance from time to time if the nx workspace interface changes, but that should not happen too often.