it('works with resolves', )_ => {
expect.assrtions(1);
returnexpect(user.getUserName(5)).resolves.toEqual('Paul');
});
async / await
it('works with async/awit', async () => {
expect.assertions(1);
constdata=awaituser.getUserName(4);
expect(data).toEqual('Mark');
});
it('works with aysnc/await and resolves', aysnc() => {
expect.assertions(1);
awaitexpect(user.getUserName(5)).resolves.toEqual('Paul');
});
Error handling
.rejects
// Testing for async errors using Promise.catch.
test('tests error with promises', () => {
expect.assertions(1);
returnuser.getUserName(2).catch(e =>
expect(e).toEqual({
error:'User with 2 not found.',
}),
);
});
// Or using async/await.
it('tests error with async/await', async () => {
expect.assertions(1);
try {
awaituser.getUserName(1);
} catch (e) {
expect(e).toEqual({
error:'User with 1 not found.',
});
}
});
.rejects
// Testing for async errors using `.rejects`.
it('tests error with rejects', () => {
expect.assertions(1);
return expect(user.getUserName(3)).rejects.toEqual({
error: 'User with 3 not found.',
});
});
// Or using async/await with `.rejects`.
it('tests error with async/await and rejects', async () => {
expect.assertions(1);
await expect(user.getUserName(3)).rejects.toEqual({
error: 'User with 3 not found.',
});
});