Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Handling Errors in Async Tests | Writing and Running Tests Effectively
Testing JavaScript Code

bookHandling Errors in Async Tests

test.js

test.js

copy

Testing asynchronous code often means checking not only for successful outcomes, but also for correct error handling. In Jest, you can use expect().resolves to check that a promise resolves as expected, and expect().rejects to check that a promise rejects. When you want to ensure an async function throws or rejects with a specific error, you should use expect().rejects.toThrow(errorMessage).

In the code above, the fetchData function either resolves with "Success!" or throws an error, depending on the input. The first test uses resolves.toBe to confirm a successful result, while the second test uses rejects.toThrow to assert that the error is thrown as expected. This approach allows you to verify that your code handles errors properly in asynchronous scenarios, making your tests more robust and reliable.

1. How can you check if an async function throws or rejects in Jest?

2. Why is it important to test error handling in asynchronous code?

question mark

How can you check if an async function throws or rejects in Jest?

Select the correct answer

question mark

Why is it important to test error handling in asynchronous code?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 5

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Suggested prompts:

Can you show an example of how to use `expect().resolves` and `expect().rejects` in a Jest test?

What happens if the async function doesn't throw the expected error?

Are there best practices for testing async error handling in Jest?

Awesome!

Completion rate improved to 7.14

bookHandling Errors in Async Tests

Swipe um das Menü anzuzeigen

test.js

test.js

copy

Testing asynchronous code often means checking not only for successful outcomes, but also for correct error handling. In Jest, you can use expect().resolves to check that a promise resolves as expected, and expect().rejects to check that a promise rejects. When you want to ensure an async function throws or rejects with a specific error, you should use expect().rejects.toThrow(errorMessage).

In the code above, the fetchData function either resolves with "Success!" or throws an error, depending on the input. The first test uses resolves.toBe to confirm a successful result, while the second test uses rejects.toThrow to assert that the error is thrown as expected. This approach allows you to verify that your code handles errors properly in asynchronous scenarios, making your tests more robust and reliable.

1. How can you check if an async function throws or rejects in Jest?

2. Why is it important to test error handling in asynchronous code?

question mark

How can you check if an async function throws or rejects in Jest?

Select the correct answer

question mark

Why is it important to test error handling in asynchronous code?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 5
some-alt