Jest reports that test passes even though expect assertion fails

与世无争的帅哥 提交于 2021-01-28 04:45:09

问题


In this code sandbox the following test is executed:

    import { of } from "rxjs";
    import { map } from "rxjs/operators";

    const source = of("World");

    test("It should fail", () => {
      source.subscribe(x => expect(x).toContain("NOTHING"));
    });

Jest reports it as passing, even though the expectation fails. Thoughts?


回答1:


rxjs observables are asynchronous. Take a look at this page to help with testing asynchronous code

You want to add the done argument as below ->

test("It should fail", done => {
  source.subscribe(
    x => {
      expect(x).toContain("NOTHING")
      done();
    });
});


来源:https://stackoverflow.com/questions/53197952/jest-reports-that-test-passes-even-though-expect-assertion-fails

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