Integrate Jest to Angular App
- Create your angular app
- Install jest
npm install jest jest-preset-angular @types/jest
- In your project root, create a setup-jest.ts file with the following contents:
import 'jest-preset-angular/setup-jest';
- 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', };
|
- Adjust your
tsconfig.spec.json
in your project root1 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"] };
|
- update
package.json
file in your project root as below.1 2
| "test": "jest --verbose", "test:watch": "jest --watch"
|
- Open your terminal and run
npm run test
, enjoy!
- 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/