pouya
pouya

Reputation: 3766

Jest alternative for `assert.IfError(value)`

Node's documentation of assert module for assert.ifError

Throws value if value is not undefined or null. This is useful when testing the error argument in callbacks. The stack trace contains all frames from the error passed to ifError() including the potential new frames for ifError() itself.

assert.ifError(null);
// OK
assert.ifError(0);
// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 0
assert.ifError('error');
// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 'error'
assert.ifError(new Error());
// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: Error

What is Jest alternative for asser.IfError ?

Upvotes: 0

Views: 264

Answers (1)

pouya
pouya

Reputation: 3766

OK i figured it out.

Solution 1. expect.toContain matcher.

expect([undefined, null]).toContain(value);

Solution 2. jest.extended module toBeNil method

installation

yarn add -D jest-extended

Use .toBeNil when checking a value is null or undefined.

test('passes when value is null or undefined', () => {
  expect(null).toBeNil();
  expect(undefined).toBeNil();
  expect(true).not.toBeNil();
});

Upvotes: 1

Related Questions