第一次使用Jest。使用以下代码初始化组件测试:
describe('CreateComponent', () => {
let component: CreateQuoteComponent;
let fixture: ComponentFixture<CreateQuoteComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ CreateQuoteComponent ]
});
fixture = TestBed.createComponent(CreateQuoteComponent);
component = fixture.componentInstance;
}));
test('should exist', () => {
expect(component).toBeDefined();
});
});
运行时,对于组件中的某些导入给出
Test suite failed to run Cannot find module' with '
at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:259:17)
at Object.<anonymous> (src/app/modules/xxx/components/landing/create/create.component.ts:19:1)
有任何潜在客户吗?还是有已知的解决方法?
答案 0 :(得分:4)
我发现了问题所在。似乎JEST无法解析我们在应用程序内部配置的相对路径,例如'@'符号,因此我们需要提供如下所示的jest moduleNameMapper:
"jest": {
"preset": "jest-preset-angular",
"setupTestFrameworkScriptFile": "<rootDir>/src/jest.ts",
"moduleNameMapper": {
"@oshc(.*)": "<rootDir>/src/app/modules/oshc/$1",
"@shared(.*)": "<rootDir>/src/app/@shared/$1"
}
},
更新:另一个问题是JEST无法在您的根文件夹的node_modules中找到模块。为此,请添加以下模块目录的配置: “ moduleDirectories”:[ “ node_modules”, “ src” ]