Mocking a Function without Jest
Explore how to manually mock a function in React testing without relying on Jest's mocking features. Understand how to provide alternate implementations for API calls within tests, ensure tests pass by isolating mocks, and restore original functions to avoid side effects on other tests.
We'll cover the following...
Starter project
The project for this lesson contains a Hello component that renders “Hello” and a user’s name. The user’s name is retrieved from a web API request. The project also contains a test to verify that the message includes the user’s name.
A copy of the project is in the code widget below. Clicking the “Run” button executes the tests.
export default "test-file-stub";
Run the test.
Does the test pass or fail? Why?
In this lesson, we’ll resolve the failing test by providing an alternate implementation of the getUser function.
Manually mocking a function
Open the Hello.test.js file. The test is as follows:
test("Should include users name when rendered", async ...