[英]Angular Karma: await fixture.whenStable always times out after upgrade from Angular v8 to v12
I have some unit tests for a component that uses reactive forms.我对使用反应式形式的组件进行了一些单元测试。
In the tests, I will patch values on the form, and then the unit test will test what I expect from various form valueChanges
subscriptions.在测试中,我将修补表单上的值,然后单元测试将测试我对各种表单
valueChanges
订阅的期望。
So that the test waits for the valueChanges
to execute, I always used the following after the patch
:为了让测试等待
valueChanges
执行,我总是在patch
后使用以下内容:
fixture.detectChanges();
await fixture.whenStable();
and this always worked fine.这总是很好用。
After my upgrade (went from v8 to v12), all tests time out when I have the await fixture.whenStable()
.升级后(从 v8 升级到 v12),当我有
await fixture.whenStable()
时,所有测试都会超时。
Ie I get:即我得到:
Error: Timeout - Async function did not complete within 5000ms (set by jasmine.DEFAULT_TIMEOUT_INTERVAL)
error properties: Object({ originalStack: 'Error: Timeout - Async function did not complete within 5000ms (set by jasmine.DEFAULT_TIMEOUT_INTERVAL)
at new ZoneAwareError (http://localhost:9877/_karma_webpack_/webpack:/node_modules/zone.js/fesm2015/zone-error.js:92:33)
Of course, if I remove the await fixture.whenStable();
当然,如果我删除
await fixture.whenStable();
the test no longer works, as when I try and access the properties set is the form valueChanges
they are not set.测试不再有效,因为当我尝试访问属性集时,它们没有设置表单
valueChanges
。
Why would the await fixture.whenStable()
now seem to hang?为什么
await fixture.whenStable()
现在似乎挂起?
Coming back to this today, I think I found the problem (in my case).今天回到这个问题,我想我发现了问题(就我而言)。
I had a setTimeout
in the components ngAfterViewInit()
which was not being cleared.我在组件
ngAfterViewInit()
中有一个未清除的setTimeout
。 Infact having the setTimeout at all was a bug, it was only need to go off once, so should have been a setInterval
.事实上,有 setTimeout 是一个错误,它只需要关闭一次,所以应该是一个
setInterval
。 I did have some problems with the ngAfterViewInit
so I already had我确实对
ngAfterViewInit
有一些问题,所以我已经有了
spyOn(component, 'ngAfterViewInit').and.callFake(() => { });
as the code in the timer was not that important (just turing animations on/off), so still not 100% sure why a change in the real ngAfterViewInit
made a difference... and yet it did, my await fixture.whenStable();
由于计时器中的代码不是那么重要(只是打开/关闭动画),所以仍然不能 100% 确定为什么真正的
ngAfterViewInit
的更改会有所不同......但它确实如此,我的await fixture.whenStable();
no longer hangs, and the test now waits for the forms valueChanges
.不再挂起,测试现在等待表单
valueChanges
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.