使用 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);
tick(999);
expect(fn).not.toHaveBeenCalled();
tick(1);
expect(fn).toHaveBeenCalled();
}));
我想使用 it.each
编写几个类似的测试
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
我错过了什么?
最佳答案
到目前为止,我想到的最好的解决方法是将 each
部分移动到 describe
包装器,以便 fakeAsync
在“经典”它
。
describe.each([[1000], [2000], [3000]])(
'test %d milliseconds',
milliseconds => {
it('', fakeAsync(() => {
const fn = jest.fn();
setTimeout(() => {
fn();
}, milliseconds);
tick(milliseconds - 1);
expect(fn).not.toHaveBeenCalled();
tick(1);
expect(fn).toHaveBeenCalled();
}));
},
);
它给测试代码和控制台输出增加了一些噪音,但至少,测试现在通过了。
关于angular - 将 Angular Testing fakeAsync 与 jest it.each 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56529330/