Mocking a Function without Jest
Learn to manually mock a function without employing Jest’s mocking features.
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";
Starter project
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 ()
...