Skip to content

Instantly share code, notes, and snippets.

@metabacalhau
Created June 16, 2021 07:45
Show Gist options
  • Select an option

  • Save metabacalhau/9f9dc3ee2e8c65f428dc38cfb2b0724c to your computer and use it in GitHub Desktop.

Select an option

Save metabacalhau/9f9dc3ee2e8c65f428dc38cfb2b0724c to your computer and use it in GitHub Desktop.
fakeDefaultExport
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