【问题标题】:Jest reports that test passes even though expect assertion failsJest 报告测试通过,即使期望断言失败
【发布时间】:2019-04-11 09:12:43
【问题描述】:

this code sandbox 中执行以下测试:

    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 将其报告为通过,即使期望失败。想法?

【问题讨论】:

  • 期望在测试结束后到来。

标签: javascript node.js typescript jestjs codesandbox


【解决方案1】:

rxjs observables 是异步的。看看this page 帮助测试异步代码

你想添加 done 参数如下 ->

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

【讨论】:

  • 有趣的是,他正在测试的 observable 不是异步的。所以这似乎是 Jest 中的一个错误。
  • observable 本身可能不是异步的,但订阅调用是异步的,并且 Jest 在达到期望调用之前完成了测试
猜你喜欢
  • 2021-12-31
  • 2020-12-31
  • 2017-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-10
  • 1970-01-01
  • 2020-08-06
相关资源
最近更新 更多