【发布时间】:2021-11-15 06:31:46
【问题描述】:
我正在使用 Jest 版本 26.6.3 来测试 Angular 组件。使用 Primeng's checkbox component 的任何组件的单元测试在 compileComponents 步骤中失败,并出现错误“无法加载 checkbox.css”:
Failed: "Failed to load checkbox.css"
7 |
8 | describe('CheckboxComponent', () => {
> 9 | beforeEach(async () => {
| ^
10 | await TestBed.configureTestingModule({
11 | imports: [CheckboxModule],
12 | declarations: [CheckboxComponent, FieldLabelComponent],
at Env.beforeEach (node_modules/jest-jasmine2/build/jasmineAsyncInstall.js:46:24)
at context.<computed> (node_modules/zone.js/bundles/zone-testing-bundle.umd.js:4309:39)
at Suite.<anonymous> (src/app/@shared/forms/checkbox/checkbox.component.spec.ts:9:3)
at ZoneDelegate.Object.<anonymous>.ZoneDelegate.invoke (node_modules/zone.js/bundles/zone-testing-bundle.umd.js:407:30)
at Zone.Object.<anonymous>.Zone.run (node_modules/zone.js/bundles/zone-testing-bundle.umd.js:167:47)
at Suite.<anonymous> (node_modules/zone.js/bundles/zone-testing-bundle.umd.js:4227:33)
我的代码库中没有名为 checkbox.css 的文件,也没有在任何地方引用它,所以我假设 css 文件来自 Primeng。我尝试在TestBed.configureTestingModule 选项中包含schemas: [NO_ERRORS_SCHEMA],但没有任何更改。
什么可能导致此错误?我将在下面包含一些相关文件:
这是失败的完整测试功能
import { TestBed } from '@angular/core/testing';
import { FormControl } from '@angular/forms';
import { CheckboxModule } from 'primeng/checkbox';
import { createComponent, getControlDisplayValue } from 'src/tests/test-utils';
import { FieldLabelComponent } from '../form-field/field-label/field-label.component';
import { CheckboxComponent } from './checkbox.component';
describe('CheckboxComponent', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [CheckboxModule],
declarations: [CheckboxComponent, FieldLabelComponent],
}).compileComponents();
});
这是组件的模板:
<p-checkbox [inputId]="inputId" [formControl]="control" [binary]="true" [class.small-checkbox]="small"></p-checkbox>
<label [for]="inputId" [class.small-label]="small">
<app-field-label [label]="label" [required]="required" [extraSmall]="small"></app-field-label>
</label>
这是 jest.config.js 文件:
module.exports = {
preset: "jest-preset-angular",
roots: ["src"],
coverageDirectory: "reports",
setupFilesAfterEnv: ["<rootDir>/src/setup-jest.ts"],
moduleNameMapper: {
"@app/(.*)": "<rootDir>/src/app/$1",
"@core": ["<rootDir>/src/app/@core"],
"@core/(.*)": ["<rootDir>/src/app/@core/$1"],
"@shared": ["<rootDir>/src/app/@shared"],
"@shared/(.*)": ["<rootDir>/src/app/@shared/$1"],
"@env": "<rootDir>/src/environments/environment",
},
globals: {
"ts-jest": {
allowSyntheticDefaultImports: true,
tsconfig: "<rootDir>/tsconfig.spec.json",
},
},
// Do not ignore librairies such as ionic, ionic-native or bootstrap to transform them during unit testing.
transformIgnorePatterns: ["node_modules/(?!(jest-test|@ng-bootstrap))"],
};
这是 tsconfig.spec.json 文件
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./out-tsc/spec",
"allowJs": true,
"types": ["jest", "node"]
},
"files": ["src/setup-jest.ts", "src/polyfills.ts"],
"include": ["src/**/*.spec.ts", "src/**/*.mock.ts", "src/**/*.d.ts"]
}
【问题讨论】:
-
嗨,你能找到解决办法吗?谢谢。
-
嘿,这里也一样 :(
标签: angular typescript jestjs primeng