MENU

Fun & Interesting

Mocking Asynchronous Functions with Jest

Swashbuckling with Code 77,028 lượt xem 4 years ago
Video Not Working? Fix It Now

Mocking is a fundamental skill in testing. It allows you to avoid testing parts of your code that are outside your control, or to get reliable return values from said code.

00:00 - What we'll cover
01:00 - Reviewing the project setup
03:02 - Creating the test
06:24 - Adding async await to pass test
07:17 - Why mock an api request for unit tests
08:32 - Adding jest mock for axios
14:06 - Check if mock function has been called
15:31 - Shorthand for mocking resolved promise value
16:23 - Gotcha for mock state, and how to clear it each test
18:24 - Alternate way of mocking within a test file
20:50 - Wrap-up

Repository for code example:
https://github.com/Jimmydalecleveland/jest-mock-async-example

Blog Post:
https://blog.jimmydc.com/mock-asynchronous-functions-with-jest/

Swapi Dev:
https://swapi.dev/

Follow Jimmy Cleveland's doings:
twitter: https://twitter.com/JimmyDCleveland
blog: https://blog.jimmydc.com/

#jest #mock #async

Comment