How to mock an exported const in Jest?

Posted by Editorial Staff | Updated on

If you are searching for how to mock an exported const in jest? then this example for you. This example will work if you compile ES6 modules syntax into ES5, because in the end, all module exports belong to the same object, which can be modified.

Alternatively, you can switch to raw commonjs require function, and do it like this with the help of jest.mock():


If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.

Related Questions & Answers