Angular Project Setup - NextensArelB/SwaggerGenerationTool GitHub Wiki
Create angular application
npx ng new jest-example --directory ./ --routing --style=scss
Remove redundant packages
npm rm karma karma-chrome-launcher karma-coverage karma-jasmine karma-jasmine-html-reporter jasmine-core @types/jasmine
Install jest
npm i -D jest @types/jest jest-preset-angular ts-node
Initialize jest
npx jest --init
โ Would you like to use Jest when running "test" script in "package.json"? ... yes โ Would you like to use Typescript for the configuration file? ... yes โ Choose the test environment that will be used for testing ยป jsdom (browser-like) โ Do you want Jest to add coverage reports? ... yes โ Which provider should be used to instrument code for coverage? ยป babel โ Automatically clear mock calls, instances and results before every test? ... no
Edit rootDir/jest.config.ts
export default {
preset: 'jest-preset-angular'
setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'],
globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
},
},
}
Edit rootDir/setup-jest.ts
import 'jest-preset-angular/setup-jest';
Edit rootDir/tsconfig.spec.json
{
"compilerOptions": {
"esModuleInterop": true,
"types": ["jest"]
},
"files": ["src/polyfills.ts"],
}
Delete files
rootDir/src/test.ts
rootDir/karma.config.js
Run tests
npm run test