Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Mock without replacing the original module #324

Open
gsuess opened this issue May 2, 2021 · 2 comments
Open

Create Mock without replacing the original module #324

gsuess opened this issue May 2, 2021 · 2 comments

Comments

@gsuess
Copy link

gsuess commented May 2, 2021

I usually like to pass all environment bound interfaces as a parameter, including fs.

Here is a simple Example:

import * as fs from "fs";
function  doSomething(something, fsEnv = fs) {
  // Do something with fsEnv
}

With this practice there is no necessity to temporarily disable an interface globally, which can cause some very unexpected issues (one of such issues is seen on the Readme with Jest Snapshots).

Is there a possibility to create a fs mock without actually messing with the origina fs?

With the example above, I would like to implement tests like this:

const fs = mockFs(fileTree);
doSomething(something, fs);

Without needing to call restore on anything.

@gsuess
Copy link
Author

gsuess commented May 2, 2021

After looking into the source-code, it doesn't seem to be possible currently.

With a bit of refactoring however, this feature could be integrated though, by adding a doNotPatch option and return a mocked fs map that implements all interfaces that fs exports.

@thekip
Copy link

thekip commented Jan 30, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants