2

使用 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

我错过了什么?

4

2 回答 2

3

到目前为止,我想到的最好的解决方法是将each部件移动到describe包装器中,以便fakeAsync在“经典”中使用it

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();
        }));
    },
);

它给测试代码和控制台输出增加了一些噪音,但至少现在测试通过了。

于 2019-06-29T06:41:36.917 回答
0

这似乎是文件的jest-preset-angular问题zone-patch。它不会修补it.eachtest.eachfakeAsync区域中运行。我已经提交了一个错误https://github.com/thymikee/jest-preset-angular/issues/339

于 2019-12-31T19:01:29.353 回答