Issue
I recently started to convert my Angular apps and libs into an @nrwl/nx workspace. The apps are running fine, however when it comes to my Jest tests I get some strange errors.
The workspace is a standard NX workspace created with
npx create-nx-workspace
No changes to the generated jest config files.
When starting my tests for one of my libs with
nx test my-lib-name
simple tests run fine. However when a test imports some external modules like e.g. @ngx-translate/core/TranslateModule or different ngx-bootstrap modules I get the following error:
Unexpected value 'TranslateModule' imported by the module 'DynamicTestModule'. Please add an @NgModule annotation.
My test looks like this:
import { ComponentFixture, waitForAsync, TestBed } from '@angular/core/testing';
import { TranslateModule } from '@ngx-translate/core';
import { BsModalRef, ModalModule } from 'ngx-bootstrap/modal';
import { ErrorDialogComponent } from './error-dialog.component';
describe('ErrorDialogComponent', () => {
let comp: ErrorDialogComponent;
let fixture: ComponentFixture<ErrorDialogComponent>;
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
declarations: [
ErrorDialogComponent,
],
imports: [
TranslateModule.forRoot(),
ModalModule.forRoot()
],
providers: [
BsModalRef
]
}).compileComponents()
}));
beforeEach(() => {
fixture = TestBed.createComponent(ErrorDialogComponent);
comp = fixture.componentInstance;
fixture.detectChanges();
});
it('should create component', () => {
expect.assertions(1);
expect(comp).toBeTruthy();
});
});
And my jest.config.js inside the lib like this:
module.exports = {
displayName: 'my-lib-name',
preset: '../../jest.preset.js',
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],
globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
stringifyContentPathRegex: '\\.(html|svg)$',
},
},
coverageDirectory: '../../coverage/libs/my-lib-name',
transform: {
'^.+\\.(ts|mjs|js|html)$': 'jest-preset-angular',
},
snapshotSerializers: [
'jest-preset-angular/build/serializers/no-ng-attributes',
'jest-preset-angular/build/serializers/ng-snapshot',
'jest-preset-angular/build/serializers/html-comment',
]
};
Global jest.presets.js and jest.config.js (both unchanged since generation):
// jest.presets.js
const nxPreset = require('@nrwl/jest/preset');
module.exports = { ...nxPreset };
// jest.config.js
const { getJestProjects } = require('@nrwl/jest');
module.exports = {
projects: getJestProjects(),
};
I already searched a lot but couldn't find any answer. I hope any one could help. If you need more information please let me know.
Solution
It turned out that a dependency (I don't know which one) was broken or buggy. A clean reinstallation of all dependencies fixed the error message.
Answered By - Gerry
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.