I am migrating my component unit tests with only jest to UI tests using Testbed and jest (Before I had just unit test with component = new MyCompontent()
).
I am using Angular 11.
There is one thing I do not understand about compileComponents
. In the docs it says "You can ignore this section if you only run tests with the CLI ng test command because the CLI compiles the application before running the tests."
I am not calling ng test
but jest --watch
but my tests work both with and without compileComponents
( by work I mean they assert correctly my view).
My component is using both templateUrl
and styleUrls
@Component({
selector: 'app-my-example',
templateUrl: './my-example.component.html',
styleUrls: ['./my-example.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class MyExampleComponent {}
So what I am missing? I want to be sure that my test will not break if I eventually set a CI or similar.
or should I always call compileComponents
? I am under the impression it will be not so efficent to do this if not needed.
So, when exactly does compileComponents
is needed ?
Edit:
This is my jest config
module.exports = {
preset: 'jest-preset-angular',
setupFilesAfterEnv: ['<rootDir>/src/setupJest.ts'],
transformIgnorePatterns: ['<rootDir>/node_modules/(?!@ionic|@saninn|@ngrx|@capacitor-community|@capacitor)'],
watchPathIgnorePatterns: ['/node_modules/', ...ignoredFiles],
coverageReporters: process.env.CI ? ['text'] : ['lcov'],
// coverageReporters: ['lcov'],
// coverageReporters: ['text'],
testPathIgnorePatterns: ['/node_modules/', ...ignoredFiles],
modulePathIgnorePatterns: [...ignoredFiles],
collectCoverage: false,
notify: true,
// notifyMode: 'failure',
coverageDirectory: './coverage',
collectCoverageFrom: ['<rootDir>/src/**/*.ts', '!**/*.module.ts', '!**/*.enum.ts'],
coveragePathIgnorePatterns: ['/node_modules/', 'package.json', ...ignoredFiles],
globals: {
'ts-jest': {
tsconfig: '<rootDir>/src/tsconfig.spec.json',
diagnostics: {
ignoreCodes: [
6138, // declared but never read
6133, // declared but never used,
2322 // object should just have known keys
]
}
}
}
};
jest-preset-angular
that includes resource transformer github.com/thymikee/jest-preset-angular/blob/master/src/… It does the same what Angular CLI does when compiles application – Ringworm