如何解决 Angular 单元测试错误:“在 afterAll\n[object ErrorEvent] 中抛出了一个错误”
Posted
技术标签:
【中文标题】如何解决 Angular 单元测试错误:“在 afterAll\\n[object ErrorEvent] 中抛出了一个错误”【英文标题】:How can I resolve Angular unit test error: "An error was thrown in afterAll\n[object ErrorEvent] thrown"如何解决 Angular 单元测试错误:“在 afterAll\n[object ErrorEvent] 中抛出了一个错误” 【发布时间】:2019-05-05 15:28:19 【问题描述】:当我在我的 Angular 项目中运行 ng test
命令时出现错误,它会给出类似的错误
10% building modules 1/1 modules 0 active04 12 2018 11:29:43.408:WARN [karma]: No captured browser, open http://localhost:9876/
04 12 2018 11:29:43.414:INFO [karma]: Karma v2.0.0 server started at http://0.0.0.0:9876/
04 12 2018 11:29:43.414:INFO [launcher]: Launching browser Chrome with unlimited concurrency
04 12 2018 11:29:43.418:INFO [launcher]: Starting browser Chrome
04 12 2018 11:29:53.540:WARN [karma]: No captured browser, open http://localhost:9876/
04 12 2018 11:29:53.777:INFO [Chrome 70.0.3538 (Mac OS X 10.14.1)]: Connected on socket SKF3rI13kIK0WCqqAAAA with id 32081204
Chrome 70.0.3538 (Mac OS X 10.14.1): Executed 95 of 191 SUCCESS (0 secs / 0.131 secs)
Chrome 70.0.3538 (Mac OS X 10.14.1) ERROR
"message": "An error was thrown in afterAll\n[object ErrorEvent] thrown",
"str": "An error was thrown in afterAll\n[object ErrorEvent] thrown"
Chrome 70.0.3538 (Mac OS X 10.14.1): Executed 96 of 191 ERROR (0 secs / 0.826 secs)
Chrome 70.0.3538 (Mac OS X 10.14.1) ERROR
"message": "An error was thrown in afterAll\n[object ErrorEvent] thrown",
"str": "An error was thrown in afterAll\n[object ErrorEvent] thrown"
Chrome 70.0.3538 (Mac OS X 10.14.1): Executed 96 of 191 ERROR (1.386 secs / 0.826 secs)
这就是因果报应,每一个考验都通过了。
我的 package.json 内容
"devDependencies":
"@angular/cli": "~1.7.0",
"@angular/compiler-cli": "^5.2.0",
"@angular/language-service": "^5.2.0",
"@types/jasmine": "~2.8.3",
"@types/jasminewd2": "~2.0.2",
"@types/node": "~6.0.60",
"codelyzer": "^4.0.1",
"jasmine-core": "~2.8.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~2.0.0",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "^1.2.1",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.1.2",
"ts-node": "~4.1.0",
"tslint": "~5.9.1",
"typescript": "~2.5.3"
,
谢谢。
【问题讨论】:
你的 afterAll() 方法有什么? (如果有的话) 我没有使用afterAll()
方法。
【参考方案1】:
我找到了解决方案,就我而言
afterAll(() =>
TestBed.resetTestingModule();
);
方法所有错误消失,下面有示例代码
describe('Component', () =>
let component: Component;
beforeEach((() =>
TestBed.configureTestingModule(
declarations: [Component]
)
.compileComponents();
));
beforeEach(() =>
const fixture = TestBed.createComponent(Component);
component = fixture.componentInstance;
);
it('should have a defined component', () =>
expect(component).toBeDefined();
);
afterAll(() =>
TestBed.resetTestingModule();
);
);
【讨论】:
根据您的截图,共有 195 个测试需要执行。那么,您是否在应用程序的每个单元测试文件(xx.spec.ts)中都放置了 afterAll 方法? 是的,我确实使用 afterAll() 更改了所有测试文件【参考方案2】:afterEach(() =>
TestBed.resetTestingModule();
);
这为我清除了错误。
【讨论】:
是的,我也是以上是关于如何解决 Angular 单元测试错误:“在 afterAll\n[object ErrorEvent] 中抛出了一个错误”的主要内容,如果未能解决你的问题,请参考以下文章
Angular 单元测试 NullInjectorError:没有 HttpClient 的提供者!错误
如何对这个从管道可观察到的错误捕获的 Angular 打字稿 Http 错误拦截器进行单元测试?