I’m trying to configure Jest in my Angular 15 application.
Jest config:
const jestPreset = require('jest-preset-angular/jest-preset');
const { globals } = jestPreset;
const tsjest = globals['ts-jest'];
const tsjestOverrides = {
...tsjest,
tsconfig: '<rootDir>/tsconfig.spec.json'
};
const globalOverrides = {
...globals,
'ts-jest': { ...tsjestOverrides }
};
module.exports = {
...jestPreset,
globals: { ...globalOverrides },
preset: 'jest-preset-angular',
setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'],
globalSetup: 'jest-preset-angular/global-setup',
reporters: [
"default",
["./node_modules/jest-html-reporter", {
pageTitle: "Test Report"
}]
]
};
tsconfig.spec.json
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./out-tsc/spec",
"types": [
"jest",
"node",
"googlemaps",
"offscreencanvas"
],
"esModuleInterop": true,
"emitDecoratorMetadata": true,
"module": "CommonJs",
},
// "files": ["./setup.jest.ts"],
"include": [
"projects/design/src/app/**/*.spec.ts",
"projects/design/src/app/**/*.d.ts"
]
}
tsconfig.json
{
"compileOnSave": false,
"compilerOptions": {
"paths": {
"api-management": [
"dist/api-management/api-management",
"dist/api-management"
],
"lib-ui-component": [
"dist/lib-ui-component/lib-ui-component",
"dist/lib-ui-component"
],
"@api/*": ["./projects/api-management/src/*"],
"@design/*": ["./projects/design/src/app/design/*"],
"@lib-ui/*": ["./projects/lib-ui-component/src/*"]
},
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"strict": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"sourceMap": true,
"declaration": false,
"downlevelIteration": true,
"experimentalDecorators": true,
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"importHelpers": true,
"target": "ES2022",
"module": "es2020",
"lib": ["es2018", "es2019", "dom"],
"useDefineForClassFields": false
},
"angularCompilerOptions": {
"extendedDiagnostics": {
"checks": {
"optionalChainNotNullable": "suppress"
}
},
"enableI18nLegacyMessageIdFormat": false,
"strictInjectionParameters": true,
"strictInputAccessModifiers": true,
"strictTemplates": true,
"suppressImplicitAnyIndexErrors": true,
"strictPropertyInitialization": false,
}
}
setup-jest.ts
import 'jest-preset-angular/setup-jest';
Test file
describe('BatteryConfigurationComponent', () => {
let storeMock: Store<DesignState>;
let matDialogMock: MatDialog;
let openNotificationServiceMock: openNotificationService;
let SettingsServiceMock: SettingsService;
let ViewContainerRefMock: ViewContainerRef;
let ComponentFactoryResolverMock: ComponentFactoryResolver;
let TranslateServiceMock: TranslateService;
let component: BatteryConfigurationComponent;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [component],
providers: [storeMock, matDialogMock, openNotificationServiceMock, SettingsServiceMock, ViewContainerRefMock, ComponentFactoryResolverMock, TranslateServiceMock],
});
})
afterEach(() => {
jest.clearAllMocks();
});
});
Error I am facing is:
I think my config is not able to understand Typescript.