Jest Mock Imported Module Again in Middle of Tests
Mocking different values for the aforementioned module using Jest
Module mocks are a powerful tool to write unit tests with Jest. They allow you to isolate the lawmaking under test from its dependencies, leading to focused, less brittle tests. Only, as many other powerful tools, module mocks can be tricky at times.
In this mail nosotros'll explore how to mock unlike values for the aforementioned module in unlike tests.
Say y'all take a greetings module exporting a hello function which depends on some other module to know the current language of the application. Something like this:
Mock me once, shame on you. Mock me twice… shame on you again?
Writing a unit examination for hello involves mocking the lang dependency in order to control the electric current language:
You lot can use jest.mock (line 4) to mock the lang dependency. In the example above, the mock module has a current field which is gear up to a mock function. Y'all want to test both branches of hi, so y'all use mockReturnValueOnce to make the mock function return "GL" in the first invocation, and"EN" in the 2d 1.
You run jest, both tests laissez passer, mission achieved. You are a happy programmer.
Only await. Try to focus the second examination using information technology.just. Aw fish! At present the test fails:
Expected value to equal:
"How-do-you-do!"
Received:
"Ola!" Well, it seems that the mock module setup is likewise breakable: you expect the mock function to be called in the same club you lot are defining it. That couples your test execution order to the mock setup, and that is… well, not good :)
Keep your friends shut, and your mocks closer
There is a amend way to setup a test like this one:
The key difference lies in lines three, xiii and twenty. You import the mocked module (line 3) to gain admission to the mock function. Then, you call mockImplementation (lines thirteen and 20) inside the test body to setup the right return value. At present yous can employ it.only whenever yous want!
Mocking values, not functions
Suppose greetings changes: now it must use a different module to get the current language value. The new module is called appEnv and it exports the current language as a value. Now greetings looks like this:
You try and change the test appropriately:
You run jest again and… it fails! You become an error message:
greetings.test.js: "currentLanguage" is read-only The trouble is that y'all can't assign a value to something yous have imported. In the previous examples, y'all imported the mock role current, and you lot used mockImplementation to change its return value, just the imported value stayed the same¹. Now you lot tin can't do that.
Don't import, crave
What yous demand is a way to utilize a dissimilar mock for each test. If you attempt something similar this, y'all'll all the same meet a failing test:
In the previous code snippet, hi is imported earlier its dependency is mocked, and so the tests are executed using the actual implementation of appEnv. It's time to ditch all that ES6 fancy stuff. Only use a practiced ol' require once you are done setting up the module mock:
Run the tests at present… Withal crimson, right? Well, you need to tell Jest to clear the module registry before each exam, so each fourth dimension y'all call require you lot get a fresh version of the required module.
This is the final, working code:
Summing up
We've seen how to mock a module to consign different values for different tests. When the export is a function, yous tin can mock information technology with jest.fn() and change its implementation for each test. When the export is a value, you need to go back to the nuts and use crave (and jest.resetModules) to ensure the guild of execution doesn't interfere with your mock setup².
¹ Well, technically it is the binding (not the value) what stays the aforementioned.
² You lot might desire to have a look at jest.doMock if y'all want to modify the mock value between two different assertions of the aforementioned test.
Source: https://medium.com/trabe/mocking-different-values-for-the-same-module-using-jest-a7b8d358d78b
0 Response to "Jest Mock Imported Module Again in Middle of Tests"
Post a Comment