These assertions test Promises and asynchronous operations. Use expectAsync()
for these assertions.
Aliases:
to resolve
,to fulfill
Success:
await expectAsync(Promise.resolve(42), 'to resolve');
await expectAsync(Promise.resolve('success'), 'to fulfill');
// With async functions
await expectAsync(async () => 'result', 'to resolve');
Failure:
await expectAsync(Promise.reject('error'), 'to resolve');
// AssertionError: Expected Promise to resolve
Negation:
await expectAsync(Promise.reject('error'), 'not to resolve');
Success:
await expectAsync(Promise.reject('error'), 'to reject');
await expectAsync(Promise.reject(new Error('failed')), 'to reject');
// With async functions
await expectAsync(async () => {
throw new Error('async error');
}, 'to reject');
Failure:
await expectAsync(Promise.resolve(42), 'to reject');
// AssertionError: Expected Promise to reject
Negation:
await expectAsync(Promise.resolve(42), 'not to reject');
Aliases:
to reject with a <constructor>
,to reject with an <constructor>
Success:
await expectAsync(
Promise.reject(new TypeError('Type error')),
'to reject with a',
TypeError,
);
await expectAsync(
Promise.reject(new Error('Generic error')),
'to reject with an',
Error,
);
Failure:
await expectAsync(
Promise.reject(new TypeError('Type error')),
'to reject with a',
RangeError,
);
// AssertionError: Expected Promise to reject with a RangeError
Negation:
await expectAsync(
Promise.reject(new TypeError('Type error')),
'not to reject with a',
RangeError,
);
Success:
// String matching
await expectAsync(
Promise.reject(new Error('Specific error')),
'to reject with',
'Specific error',
);
// RegExp matching
await expectAsync(
Promise.reject(new Error('Error: Something failed')),
'to reject with',
/Something failed/,
);
// Object matching
await expectAsync(
Promise.reject({ message: 'Custom error', code: 500 }),
'to reject with',
{ message: 'Custom error' },
);
Failure:
await expectAsync(
Promise.reject(new Error('Different error')),
'to reject with',
'Specific error',
);
// AssertionError: Expected Promise to reject with 'Specific error'
Negation:
await expectAsync(
Promise.reject(new Error('Different error')),
'not to reject with',
'Specific error',
);
Aliases:
to fulfill with value satisfying <string | RegExp | object>
,to resolve to value satisfying <string | RegExp | object>
Success:
// String matching
await expectAsync(
Promise.resolve('Hello World'),
'to fulfill with value satisfying',
'Hello World',
);
// RegExp matching
await expectAsync(
Promise.resolve('Success: Operation completed'),
'to resolve to value satisfying',
/Success/,
);
// Object matching
await expectAsync(
Promise.resolve({ status: 'ok', data: [1, 2, 3] }),
'to fulfill with value satisfying',
{ status: 'ok' },
);
Failure:
await expectAsync(
Promise.resolve('Different value'),
'to fulfill with value satisfying',
'Expected value',
);
// AssertionError: Expected Promise to resolve to value satisfying 'Expected value'
Negation:
await expectAsync(
Promise.resolve('Different value'),
'not to fulfill with value satisfying',
'Expected value',
);