首页 > 解决方案 > 开玩笑说即使在出错后也继续检查 expect()

问题描述

我是测试的新手。我在测试中遇到问题。我的测试在第一次不匹配后立即退出,并且不检查剩余的期望

我想开玩笑地完成对每个期望的测试,即使它发生错误或意外值

test("concrete scope test should pass", async () => {

  const scale = funcs.getScale(theJson);

  const concreteScopes = await s.getConcreteScope(
    theJson,
    lines,
    scale,
    data.scopeId,
    order.pdf
  );
  expect(concreteScopes.ScopeId).toBe(33);
  expect(concreteScopes.Name).toEqual(theJson.Name);
  expect(concreteScopes.Address).toEqual(theJson.Address);
  expect(concreteScopes.PDF).toEqual("dfa");

}, 300000);

我在第一个期望expect(concreteScopes.ScopeId).toBe(33);和最后一个期望中有错误expect(concreteScopes.PDF).toEqual("dfa");

但它在第一个错误时终止并且没有显示剩余的错误

expect(received).toBe(expected) // Object.is equality

    Expected: 33
    Received: "275188"

      45 |     order.pdf
      46 |   );
    > 47 |   expect(concreteScopes.ScopeId).toBe(33);
         |                                  ^
      48 |   expect(concreteScopes.Name).toEqual(theJson.Name);
      49 |   expect(concreteScopes.Address).toEqual(theJson.Address);
      50 |   expect(concreteScopes.PDF).toEqual("dfa");

      at Object.toBe (__test__/concretescope.test.js:47:34)`

它在第 50 行没有显示错误(顺便说一句,48、49 之间的值是正确的)。

标签: javascriptunit-testingjestjs

解决方案


Jest 将在一个断言失败后终止。这是为了支持测试应该准确断言一件事的想法。在一个测试中拥有多个相关的断言并不一定是坏事,但是如果你的单元测试测试一个单一的功能并且一个断言失败,那么其他的是否正确并不重要。如果您需要弄清楚您的测试结果出了什么问题,只需调试或 console.log 具体范围。


推荐阅读