Created
June 16, 2021 07:45
-
-
Save metabacalhau/9f9dc3ee2e8c65f428dc38cfb2b0724c to your computer and use it in GitHub Desktop.
fakeDefaultExport
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function fakeDefaultExport(moduleRelativePath, stubs) { | |
| if (require.cache[require.resolve(moduleRelativePath)]) { | |
| delete require.cache[require.resolve(moduleRelativePath)]; | |
| } | |
| Object.keys(stubs).forEach(function (dependencyRelativePath) { | |
| require.cache[require.resolve(dependencyRelativePath)] = { | |
| exports: stubs[dependencyRelativePath] | |
| }; | |
| }); | |
| return require(moduleRelativePath); | |
| } | |
| const moduleUnderTest = fakeDefaultExport("./module-under-test", { | |
| './module-you-wanna-mock': function () { | |
| console.log("Look ma, I'm mocked"); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment