gpt4 book ai didi

angular - Jest 无法加载 Primeng css 文件

转载 作者:行者123 更新时间:2023-12-04 13:24:44 24 4
gpt4 key购买 nike

我正在使用 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。我试过包括 schemas: [NO_ERRORS_SCHEMA]TestBed.configureTestingModule选项,没有变化。
什么可能导致此错误?我将在下面包含一些相关文件:
这是失败的完整测试功能
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"]
}

最佳答案

看起来这个问题是由Primeng组件无法加载styleUrls中指定的css文件引起的。测试运行时的组件装饰器。我能够通过在 beforeEach 中添加以下代码来解决这个问题函数和调用之前 TestBed.configureTestingModule :

TestBed.overrideComponent(Checkbox, { set: { styleUrls: [] } });
因为在我的一些测试中多个 Primeeng 组件导致了这个错误,我最终拉下了 TestBed.overrideComponent调用辅助函数:
export function removeStyleUrlsFrom(tokens: Type<any>[]): void {
tokens.forEach((token) => TestBed.overrideComponent(token, { set: { styleUrls: [] } }));
}
可以这样使用:
beforeEach(async () => {
removeStyleUrlsFrom([Checkbox, Calendar, Dropdown]);
await TestBed.configureTestingModule({
imports: [
CheckboxModule,
CalendarModule,
DropdownModule,
],
.....
}).compileComponents();

关于angular - Jest 无法加载 Primeng css 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69273235/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com