Oba Api
Oba Api

Reputation: 261

Showing the state of variables when jest finds an error

I'm doing a tutorial on jest (link) and I wrote a sum function and tested it.

function sum(a, b) {
  return a + b;
}

Is it possible to show the state of variables when jest finds an error?

To give you an example Now you get this but not the state of variables:

enter image description here But I would like to see:

error when:
i=0 or j=0

Upvotes: 0

Views: 102

Answers (1)

Panczur
Panczur

Reputation: 651

When testing result of given function, it's better to run multiple tests with one expect instead of one test with multiple checks. To do so, I would recommend you checking out jest-each library. Then you can run multiple tests with one expect. jest-each gives you also possibility to insert tested arguments in the name of the test, so to wrapping this up, with jest-each you could write something like that:

each`
    a    | b    | expected
    ${1} | ${1} | ${2}
    ${6} | ${9} | ${15}
    ${4} | ${2} | ${6}
    `
.it('adding $a to $b should give $expected', ({ a, b, expected }) => {
    expect(sum(a, b)).toBe(expected)
})

I'm not sure but I guess if you go deep down in documentation you will find also a way to generate dynamically arguments to pass it to test.

Upvotes: 1

Related Questions