Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jest to continue checking expect() even after error

I am a newbie in testing. I am having issues in test. my test quit immediately after its gets first not matched and does not check the remaining expectations

I would like to jest to finish testing each expectations even after it occur error or unexpected value

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);

I have errors in first expect expect(concreteScopes.ScopeId).toBe(33); and in last expect expect(concreteScopes.PDF).toEqual("dfa");

but it terminates at first error and does not show me the remaining errors

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)`

It does not show error at line 50 (btw the values in between 48, 49 are correct).

like image 729
Muhammad Sharif Avatar asked Apr 09 '26 12:04

Muhammad Sharif


2 Answers

Jest will terminate after one assertion fails. This is to support the idea that a test should assert exactly one thing. It's not necessarily bad to have multiple related assertions in a single test, but if your unit test tests a single piece of functionality and one assertion fails, it doesn't really matter if the others are correct or not. If you need to figure out what's wrong with your test result, just debug or console.log concreteScopes.

like image 80
apoteet Avatar answered Apr 12 '26 02:04

apoteet


Wow this is old, but maybe it helps someone else getting started, here goes.

I'm not arguing against apoteet - the sentiment that a unit test should test one thing is very, very sound. Also the timeout is 300 seconds, which is worrying for most any kind of automated test.

However, jest can easily facilitate more advanced testing than pure single unit testing, but no matter what - running one action and having a more complete scope of the result of that action without needing to debug or log can be a time saving habit in and of itself!

To this question, you can use toMatchObject to get the full scope of your expectation, something like:

expect(concreteScopes).toMatchObject({
  ScopeId: 33,
  Name: theJson.Name,
  Address: theJson.Address,
  PDF: "dfa",
});

Notably, .toMatchObject(expected) only compares the properties defined in the expected object, so concreteScopes can be a giant object, but this test will only confirm those four values match.

like image 41
synthmusic Avatar answered Apr 12 '26 03:04

synthmusic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!