Failing expect() inside subscribe() does not mark test as invalid

扶醉桌前 提交于 2020-02-24 10:18:49

问题


We recently upgraded to Angular 6.0.3, RxJs 6.2.0 and jest 23.1.0 (upgrading from RxJS 5 & Angular 4).

There seems to be a problem with Jest & RxJs as failing expect-statements inside a subscribe-Block do not mark the test as failed. Here's a minimal example:

    it("should fail", () => {

        const obs = Observable.create((observer) => {
            observer.next(false);
        });

        obs.subscribe((value) => {
            console.log(value); // => false
            expect(value).toBeTruthy();
        });

    });

The expect-Statement gets executed, but the test still passes. We didn't observe this behaviour with the previous RxJs version and Jest.


回答1:


Try to use done.

it("should fail", (done) => {

    const obs = Observable.create((observer) => {
        observer.next(false);
    });

    obs.subscribe((value) => {
        console.log(value); // => false
        expect(value).toBeTruthy();
        done();
    });

});

more info



来源:https://stackoverflow.com/questions/50930249/failing-expect-inside-subscribe-does-not-mark-test-as-invalid

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!