Image following test case:
it('valid emails checks', () => { ['[email protected]', '[email protected]'/*, ...*/].map(mail => { expect(isValid(mail)).toBe(true); }); });
I would like to add auto-generated message for each email like Email '[email protected]' should be valid
so that it's easy to find failing test cases.
Something like:
// .map(email => expect(isValid(email), `Email ${email} should be valid`).toBe(true);
Is it possible in Jest ?
In Chai it was possible to do with second parameter like expect(value, 'custom fail message').to.be...
and in Jasmine seems like it's done with .because
clause. But cannot find solution in Jest.
When you're writing tests, you often need to check that values meet certain conditions. expect gives you access to a number of "matchers" that let you validate different things. For additional Jest matchers maintained by the Jest Community check out jest-extended .
expect. assertions(number) will verify that a certain number of assertions are called during a test. Often, this is useful when testing asynchronous code, so as to make sure that assertions in a callback actually got called.
To check if a function was called correctly with Jest we use the expect() function with specific matcher methods to create an assertion. We can use the toHaveBeenCalledWith() matcher method to assert the arguments the mocked function has been called with.
Jest function toHaveBeenCalledWith to ignore object order. 3. Jest to match every object in array.
You try this lib that extends jest: https://github.com/mattphillips/jest-expect-message
test('returns 2 when adding 1 and 1', () => { expect(1 + 1, 'Woah this should be 2!').toBe(3); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With