Jest 报告即使期望断言失败,测试仍然通过

Ole*_*Ole 6 javascript node.js typescript jestjs codesandbox

此代码沙箱中执行以下测试:

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

    const source = of("World");

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

尽管预期落空,但 Jest 仍将其报告为通过。想法?

Jag*_*ags 5

rxjs 可观察量是异步的。查看此页面以帮助测试异步代码

您想添加完成参数,如下所示 ->

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