@ud-angular-builders/jest-7
TypeScript icon, indicating that this package has built-in type declarations

7.6.2 • Public • Published

Jest builder for Angular build facade

@ud-angular-builders/jest-7

Fork from angular-builders. Only @ud-angular-builders/custom-webpack has been enhanced for now.

This branch is compatible with Angular 7 and not not Angular 8. Please, refer to @ud-angular-builders/jest for Angular 8.

Allows running ng test with Jest instead of Karma & Jasmine.
The builder comes to provide zero configuration setup for Jest while keeping the workspace clear of boilerplate code.

Prerequisits

Installation

  1. Remove Karma related libraries and files:
      npm remove karma karma-chrome-launcher karma-coverage-istanbul-reporter karma-jasmine karma-jasmine-html-reporter
      rm src/karma.conf.js
      rm src/test.ts
  2. Install the builder (and jest if you still haven't): npm i -D jest @angular-builders/jest

Updating Typescript configurations

Although you run your unit tests with Jest, Protractor (e2e tests) still has to use Jasmine. Due to this fact it's possible that you favorite IDE will get confused with the typings and will propose you Jasmine types in unit tests or Jest types in e2e test. In order to avoid these problems you have to specify the types explicitly:

  1. In tsconfig.spec.json (src directory, used by Jest):

    "compilerOptions": {
    	...
    	"module": "commonjs",
    	"types": ["jest"]
    } 

    Make sure the module is commonjs, otherwise tests will fail at imports.

  2. In tsconfig.json (root directory, used by IDE):

    "compilerOptions": {
    	...
    	"types": ["jest"]
    } 

Running with Angular CLI

  • In your angular.json:
    "projects": {
        ...
        "[your-project]": {
             ...
             "architect": {
                    ...
                    "test": {
                              "builder": "@angular-builders/jest:run"
                              "options": {
                                    ... //see below
                              }
  • Run the tests: ng test

Multi-projects workspace support

The builder supports multi-project workspaces out of the box, the only thing required is editing tsconfig.spec.json in the relevant project directory as described above.

Builder options

  • configPath - path to jest config file, relative to project root (or src/ directory in case of non-project app), defaults to jest.config.js. The configuration is merged on top of the default configuration, so there is no need to specify the whole jest configuration in this file. Just specify the changes you'd like to make to the default configuration. The way the configurations are merged is as following:

    1. Take the default configuration from the library
    2. Add on top of it default project specific config (that is dynamic due to different root directories). Used to scope single project test runs.
    3. Add on top of it package.json jest config if exists (for all projects) or jest.config.js from workspace root directory if exists
    4. Add on top of it project specific config if it is specified inside angular.json or jest.config.js from project directory (or src/ directory in case of non-project app) if exists.

    Thus, if you don't provide configPath in options, and you'd like to customize the configuration of a single project in your workspace, you only have to add jest.config.js in this project's root directory and specify the configuration delta in this file.
    Or, if you'd like the same custom configuration to be applied to all the projects in the workspace, you just specify it in package.json. Another option in such a case is creating a single config file in the workspace root and specifying it in angular.json for each project.

  • [jest-cli-option] - any option from Jest CLI options. For example, to run unit tests without caching and with junit-reporter use:

    "options": {
     	"no-cache": true,
     	"reporters": "jest-junit"
    }   

    These options can also be provided directly to ng test command. For example, to run a single test from this suite:

     describe("My cool suite", () => {
        it("Should do one thing", () => {
     		...
        })
        
        it("Should do another thing", () => {
     		...
        })
     })

    Use the following command: ng test --testNamePattern="My cool suite Should do one thing"

Migrating existing tests to Jest

Use this for automatic migration of your Jasmine tests to Jest framework.

Troubleshooting

Please find below a selection of potential issues you might face when using this builder. Refer to jest-preset-angular Troubleshooting for jest-preset-angular specific issues.

Unexpected token [import|export|other]

This means that the library you're using doesn't use commonjs module format (which jest expects to see). You will need to implement the recommendations mentioned in jest-preset-angular Troubleshooting Guide.

One of the recommendations might require you to transpile js files through babel-jest.
In this case make sure you add allowSyntheticDefaultImports to the ts-jest configuration (see here for an explanation of this setting).

"ts-jest": {
   ...
  "allowSyntheticDefaultImports": true
}

Your final jest.config.js file should look something like this:

const esModules = ['[thir-party-lib]'].join('|');

module.exports = {
  globals: {
    "ts-jest": {
      "allowSyntheticDefaultImports": true
    }
  },
  transformIgnorePatterns: [`<rootDir>/node_modules/(?!${esModules})`],

  "transform": {
    "^.+\\.js$": "babel-jest"
  }
};

Package Sidebar

Install

npm i @ud-angular-builders/jest-7

Weekly Downloads

25

Version

7.6.2

License

MIT

Unpacked Size

647 kB

Total Files

112

Last publish

Collaborators

  • milottit