Sha*_*lva 1 testing jestjs angular
第一次使用 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();
});
});
Run Code Online (Sandbox Code Playgroud)
运行时,对于组件中的一些导入给出
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)
Run Code Online (Sandbox Code Playgroud)
任何线索或是否有已知的解决方法?
我想通了这个问题。似乎 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"
}
Run Code Online (Sandbox Code Playgroud)
},
更新:还有另一个问题,JEST 无法在根文件夹的 node_modules 中找到模块。为此,在模块目录的 jest 配置中添加以下内容:"moduleDirectories": [ "node_modules", "src" ]
| 归档时间: |
|
| 查看次数: |
2108 次 |
| 最近记录: |