I am new to angular unit testing with jest. And config the project with jest as stated below.
After every –clearCache, running a simple unit test takes 500 to 1500 sec. But after the 1st run unit test complete in 30sec. This mainly cause issue in running inside pipeline.
I have looked around found people where facing issue with old version of jest. And also tried tweaking but it didn’t help.
enter image description here
enter image description here
unit test
describe('LoginPage', () => {
let component: any;
let harness: RouterTestingHarness;
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [SigninComponent, ToastComponent, InputComponent],
imports: [CommonModule,BrowserAnimationsModule, RouterTestingModule,MyCommonLibModule FormsModule],
providers: [
provideRouter([{
path: 'auth/signin',
component: SigninComponent
}]), Router,
{ provide: AuthenticationService, useValue: { CognitoUserpoolLogin: jest.fn() } },
{ provide: AuthIntegrationService, useValue: { triggerEventOnLogin: jest.fn() } },
{ provide: ActivatedRoute, useValue: { params: of({ page: '' }), } }
],
}).compileComponents();
harness = await RouterTestingHarness.create();
component = await harness.navigateByUrl('auth/signin');
});
it('should create the app', async() => {
expect(component).toBeInstanceOf(SigninComponent);
});
})
package
"@angular/core": "^16.2.0",
"jest": "^29.7.0",
"jest-preset-angular": "^14.0.0",
"@types/jest": "^29.5.11",
"ts-node": "^10.9.2",
"tslib": "^2.3.0",
tsconfig.spec.ts
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./out-tsc/spec",
"types": [
"jest" // 1
],
"esModuleInterop": true, // 2
"emitDecoratorMetadata": true // 3
},
"files": [
"src/polyfills.ts"
],
"include": [
"src/**/*.spec.ts",
"src/**/*.d.ts"
]
}
Jest Config
const esModules = ['ngx-otp-input','@angular', '@ngrx', "@mylib/my-common-lib", "@aws-amplify", "@aws-sdk", "ngx-cookie-service", "ngx-spinner", "primeng"];
const config: Config = {
modulePaths: ["<rootDir>"],
clearMocks: true,
collectCoverage: true,
coverageDirectory: "coverage",
coverageProvider: "v8",
moduleDirectories: [
"node_modules"
],
preset: 'jest-preset-angular',
setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'],
testEnvironment: "jsdom",
transformIgnorePatterns: [
`<rootDir>/node_modules/(?!${esModules.join('|')})`
],
}
Rajesh Patra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.