Angular 使用角度测试fakeAsync和jest it.each
使用Angular 8、@Angular builders/jest 8.0.2和jest 24.8,并给出以下测试通过Angular 使用角度测试fakeAsync和jest it.each,angular,jestjs,angular-test,ts-jest,Angular,Jestjs,Angular Test,Ts Jest,使用Angular 8、@Angular builders/jest 8.0.2和jest 24.8,并给出以下测试通过 import { tick, fakeAsync } from '@angular/core/testing'; it('test 1000 milliseconds', fakeAsync(() => { const fn = jest.fn(); setTimeout(() => { fn(); }, 1000);
import { tick, fakeAsync } from '@angular/core/testing';
it('test 1000 milliseconds', fakeAsync(() => {
const fn = jest.fn();
setTimeout(() => {
fn();
}, 1000);
tick(999);
expect(fn).not.toHaveBeenCalled();
tick(1);
expect(fn).toHaveBeenCalled();
}));
我想用it编写几个类似的测试。每个
it.each([[1000], [2000], [3000]])(
'test %d milliseconds',
fakeAsync(milliseconds => {
const fn = jest.fn();
setTimeout(() => {
fn();
}, milliseconds);
tick(milliseconds - 1);
expect(fn).not.toHaveBeenCalled();
tick(1);
expect(fn).toHaveBeenCalled();
}),
);
但我在每次测试中都会遇到这样的错误:
Expected to be running in 'ProxyZone', but it was not found.
at Function.Object.<anonymous>.ProxyZoneSpec.assertPresent (node_modules/zone.js/dist/proxy.js:42:19)
at node_modules/zone.js/dist/fake-async-test.js:588:47
应在“ProxyZone”中运行,但未找到它。
位于Function.Object..ProxyZoneSpec.assertPresent(node_modules/zone.js/dist/proxy.js:42:19)
在node_modules/zone.js/dist/fake async test.js:588:47
我遗漏了什么?到目前为止,我想到的最好的解决方法是将每个部分移动到描述包装器中,以便在“经典”it
中使用fakeAsync
description.每个([[1000]、[2000]、[3000]])(
“测试%d毫秒”,
毫秒=>{
它(“”,fakeAsync(()=>{
const fn=jest.fn();
设置超时(()=>{
fn();
}毫秒);
滴答声(毫秒-1);
expect(fn).not.tohavebeincall();
勾选(1);
expect(fn).tohavebeincall();
}));
},
);
它给测试代码和控制台输出增加了一些噪音,但至少现在测试通过了。这似乎是jest preset ANGUAL
和区域补丁
文件的问题。它不会修补它。每个或测试。每个在fakeAsync
区域中运行。我提交了一个bug你找到修复方法了吗?@distante:没有我想要的那么整洁,但作为一种解决方法,我用一个描述将标准包装起来。每个都可以使用fakeAsync
。这样做的工作,但增加了一些噪音在规范文件和控制台输出谢谢你。你应该考虑写一个问题的答案。也许它不像你说的那样漂亮,但它可以帮助其他人找到更好的方法。