Jest Mock Imported Module Again in Middle of Tests Change

Photograph by Sharon McCutcheon on Unsplash

Module mocks are a powerful tool to write unit tests with Jest. They permit you to isolate the code under test from its dependencies, leading to focused, less brittle tests. But, as many other powerful tools, module mocks can exist tricky at times.

In this mail service we'll explore how to mock different values for the same module in different tests.

Say yous have a greetings module exporting a hullo role which depends on another module to know the current language of the application. Something like this:

Mock me once, shame on yous. Mock me twice… shame on you once again?

Writing a unit test for hullo involves mocking the lang dependency in gild to control the current language:

You tin use jest.mock (line iv) to mock the lang dependency. In the example higher up, the mock module has a current field which is gear up to a mock office. You desire to test both branches of hello, and so yous use mockReturnValueOnce to make the mock function return "GL" in the showtime invocation, and"EN" in the second 1.

You run jest, both tests pass, mission accomplished. You lot are a happy developer.

But wait. Attempt to focus the second test using information technology.only. Aw fish! Now the exam fails:

          Expected value to equal:
"How-do-you-do!"
Received:
"Ola!"

Well, it seems that the mock module setup is too brittle: yous wait the mock role to be called in the same order you are defining it. That couples your exam execution order to the mock setup, and that is… well, not good :)

Go along your friends close, and your mocks closer

In that location is a ameliorate way to setup a exam like this one:

The key difference lies in lines 3, thirteen and 20. You import the mocked module (line 3) to gain access to the mock function. Then, you telephone call mockImplementation (lines 13 and 20) inside the test body to setup the right return value. Now you can use it.only whenever you want!

Mocking values, not functions

Suppose greetings changes: now it must use a different module to get the current linguistic communication value. The new module is called appEnv and it exports the current linguistic communication every bit a value. Now greetings looks similar this:

You endeavour and change the examination appropriately:

Y'all run jest once again and… information technology fails! You lot get an fault message:

          greetings.exam.js: "currentLanguage" is read-only        

The problem is that you tin't assign a value to something you have imported. In the previous examples, you imported the mock function electric current, and yous used mockImplementation to modify its render value, just the imported value stayed the aforementioned¹. At present you tin't do that.

Don't import, require

What you need is a mode to use a unlike mock for each examination. If you lot try something like this, you'll still see a failing exam:

In the previous code snippet, hello is imported before its dependency is mocked, so the tests are executed using the actual implementation of appEnv. It'due south time to ditch all that ES6 fancy stuff. Just use a skilful ol' require once you are done setting up the module mock:

Run the tests at present… Notwithstanding scarlet, right? Well, you need to tell Jest to clear the module registry earlier each exam, so each time you phone call require you get a fresh version of the required module.

This is the final, working lawmaking:

Summing upwards

We've seen how to mock a module to export different values for unlike tests. When the export is a role, you can mock it with jest.fn() and change its implementation for each test. When the export is a value, you need to go back to the basics and use crave (and jest.resetModules) to ensure the order of execution doesn't interfere with your mock setup².

¹ Well, technically it is the binding (not the value) what stays the same.

² You might want to accept a look at jest.doMock if you want to change the mock value between two unlike assertions of the same test.

overstreetknetch.blogspot.com

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 Change"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel