0%

integrate jest to angular app

Integrate Jest to Angular App

  1. Create your angular app
  2. Install jest
    npm install jest jest-preset-angular @types/jest
  3. In your project root, create a setup-jest.ts file with the following contents:
    import 'jest-preset-angular/setup-jest';
  4. Create the jest.config.js file in your project root directory with the following contents:
    1
    2
    3
    4
    5
    module.exports = {
    preset: 'jest-preset-angular',
    setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'],
    globalSetup: 'jest-preset-angular/global-setup',
    };
  5. Adjust your tsconfig.spec.json in your project root
    1
    2
    3
    4
    5
    6
    7
    8
    9
    {
    "extends": "./tsconfig.json",
    "compilerOptions": {
    "outDir": "./out-tsc/spec",
    "module": "CommonJs",
    "types": ["jest"]
    },
    "include": ["src/**/*.spec.ts", "src/**/*.d.ts"]
    };
  6. update package.json file in your project root as below.
    1
    2
    "test": "jest --verbose",
    "test:watch": "jest --watch"
  7. Open your terminal and run npm run test, enjoy!
  8. If you want to remove karma and Jasmine and switch to Jest completely, run the following command.
    1
    npm uninstall karma karma-chrome-launcher karma-coverage-istanbul-reporter karma-jasmine karma-jasmine-html-reporter jasmine-core jasmine-spec-reporter

references:

xfive.co/blog/testing-angular-faster-jest/