Karma + Jasmine:无法阅读 属性 'getComponentFromError'
Karma + Jasmine: Cannot read property 'getComponentFromError'
我正在学习本教程:https://angular.io/guide/testing#component-test-scenarios 用于 karma+jasmine 单元测试。这是我的代码:
import { AppComponent } from "./app.component";
import { ComponentFixture, TestBed } from "@angular/core/testing";
describe('AppComponent', () => {
let component: AppComponent;
let fixture: ComponentFixture<AppComponent>;
let h1: HTMLElement;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [ AppComponent ],
});
fixture = TestBed.createComponent(AppComponent);
component = fixture.componentInstance;
h1 = fixture.nativeElement.querySelector('h1');
});
it('should display original title', () => {
expect(h1.textContent).toContain("Ciao");
});
});
当我 运行 测试时,我得到以下异常:
TypeError: Cannot read property 'getComponentFromError' of null
at TestBed._initIfNeeded (D:/Users/apwzp/AppData/Local/Temp/karma-typescript-bundle-11944DDd01l2f5Y0P.js:1020:52)
at TestBed.createComponent (D:/Users/apwzp/AppData/Local/Temp/karma-typescript-bundle-11944DDd01l2f5Y0P.js:1174:14)
at Function.TestBed.createComponent (D:/Users/apwzp/AppData/Local/Temp/karma-typescript-bundle-11944DDd01l2f5Y0P.js:876:29)
at UserContext.<anonymous> (assets/app/app.component.spec.ts:14:20 <- assets/app/app.component.spec.js:13:37)
at ZoneDelegate.invoke (node_modules/zone.js/dist/zone.js:388:26)
at ProxyZoneSpec.onInvoke (node_modules/zone.js/dist/proxy.js:79:39)
at ZoneDelegate.invoke (node_modules/zone.js/dist/zone.js:387:32)
at Zone.run (node_modules/zone.js/dist/zone.js:138:43)
at UserContext.<anonymous> (node_modules/zone.js/dist/jasmine-patch.js:106:34)
有人知道怎么解决吗?
我 运行 找到了类似的东西并在这里找到了答案:。
我刚刚将它添加到我的 beforeEach()
方法中,它为我解决了这个错误(在我完全正常工作之前,我还必须克服其他一些问题)。
TestBed.resetTestEnvironment();
TestBed.initTestEnvironment(BrowserDynamicTestingModule,
platformBrowserDynamicTesting());
基本上这只是一个改变这个的问题:
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [ AppComponent ],
});
fixture = TestBed.createComponent(AppComponent);
component = fixture.componentInstance;
h1 = fixture.nativeElement.querySelector('h1');
});
到
import { BrowserDynamicTestingModule,
platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';
beforeEach(() => {
TestBed.resetTestEnvironment();
TestBed.initTestEnvironment(BrowserDynamicTestingModule,
platformBrowserDynamicTesting());
TestBed.configureTestingModule({
declarations: [ AppComponent ],
});
fixture = TestBed.createComponent(AppComponent);
component = fixture.componentInstance;
h1 = fixture.nativeElement.querySelector('h1');
});
如果您要升级到 jest-preset-angular
版本 >= 9,将 import 'jest-preset-angular';
替换为 import 'jest-preset-angular/setup-jest';
似乎可以解决问题,而无需添加 beforeAll
每次测试。
只需将此添加到根文件夹中的 setupJest.ts
:
import {TestBed} from '@angular/core/testing';
import {BrowserDynamicTestingModule, platformBrowserDynamicTesting} from '@angular/platform-browser-dynamic/testing';
TestBed.resetTestEnvironment();
TestBed.initTestEnvironment(BrowserDynamicTestingModule,
platformBrowserDynamicTesting());
然后将其添加到 jest 配置中:
"setupFilesAfterEnv": [
"<rootDir>/setupJest.ts"
],
我正在学习本教程:https://angular.io/guide/testing#component-test-scenarios 用于 karma+jasmine 单元测试。这是我的代码:
import { AppComponent } from "./app.component";
import { ComponentFixture, TestBed } from "@angular/core/testing";
describe('AppComponent', () => {
let component: AppComponent;
let fixture: ComponentFixture<AppComponent>;
let h1: HTMLElement;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [ AppComponent ],
});
fixture = TestBed.createComponent(AppComponent);
component = fixture.componentInstance;
h1 = fixture.nativeElement.querySelector('h1');
});
it('should display original title', () => {
expect(h1.textContent).toContain("Ciao");
});
});
当我 运行 测试时,我得到以下异常:
TypeError: Cannot read property 'getComponentFromError' of null
at TestBed._initIfNeeded (D:/Users/apwzp/AppData/Local/Temp/karma-typescript-bundle-11944DDd01l2f5Y0P.js:1020:52)
at TestBed.createComponent (D:/Users/apwzp/AppData/Local/Temp/karma-typescript-bundle-11944DDd01l2f5Y0P.js:1174:14)
at Function.TestBed.createComponent (D:/Users/apwzp/AppData/Local/Temp/karma-typescript-bundle-11944DDd01l2f5Y0P.js:876:29)
at UserContext.<anonymous> (assets/app/app.component.spec.ts:14:20 <- assets/app/app.component.spec.js:13:37)
at ZoneDelegate.invoke (node_modules/zone.js/dist/zone.js:388:26)
at ProxyZoneSpec.onInvoke (node_modules/zone.js/dist/proxy.js:79:39)
at ZoneDelegate.invoke (node_modules/zone.js/dist/zone.js:387:32)
at Zone.run (node_modules/zone.js/dist/zone.js:138:43)
at UserContext.<anonymous> (node_modules/zone.js/dist/jasmine-patch.js:106:34)
有人知道怎么解决吗?
我 运行 找到了类似的东西并在这里找到了答案:
我刚刚将它添加到我的 beforeEach()
方法中,它为我解决了这个错误(在我完全正常工作之前,我还必须克服其他一些问题)。
TestBed.resetTestEnvironment();
TestBed.initTestEnvironment(BrowserDynamicTestingModule,
platformBrowserDynamicTesting());
基本上这只是一个改变这个的问题:
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [ AppComponent ],
});
fixture = TestBed.createComponent(AppComponent);
component = fixture.componentInstance;
h1 = fixture.nativeElement.querySelector('h1');
});
到
import { BrowserDynamicTestingModule,
platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';
beforeEach(() => {
TestBed.resetTestEnvironment();
TestBed.initTestEnvironment(BrowserDynamicTestingModule,
platformBrowserDynamicTesting());
TestBed.configureTestingModule({
declarations: [ AppComponent ],
});
fixture = TestBed.createComponent(AppComponent);
component = fixture.componentInstance;
h1 = fixture.nativeElement.querySelector('h1');
});
如果您要升级到 jest-preset-angular
版本 >= 9,将 import 'jest-preset-angular';
替换为 import 'jest-preset-angular/setup-jest';
似乎可以解决问题,而无需添加 beforeAll
每次测试。
只需将此添加到根文件夹中的 setupJest.ts
:
import {TestBed} from '@angular/core/testing';
import {BrowserDynamicTestingModule, platformBrowserDynamicTesting} from '@angular/platform-browser-dynamic/testing';
TestBed.resetTestEnvironment();
TestBed.initTestEnvironment(BrowserDynamicTestingModule,
platformBrowserDynamicTesting());
然后将其添加到 jest 配置中:
"setupFilesAfterEnv": [
"<rootDir>/setupJest.ts"
],