-
Notifications
You must be signed in to change notification settings - Fork 85
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Using mock-fs with Jest results in open handles #341
Comments
There are quite few known issues with jest+mock-fs, you can find some in this repo's issues. As jest also modifies nodejs internals quite a lot, it's not surprising it doesn't play well with mock-fs which modifies fs internals too. Personally I would avoid using jest and mock-fs together. |
OK, upon slightly closer inspection it looks like it has something to do with how the readfile context is collected The following change (calling the original open binding in addition to grabbing the proto) seems to fix the issue. diff --git 1/readfilecontext2.js 2/readfilecontext.js
index 4bd66ee..f876db8 100644
--- 1/readfilecontext2.js
+++ 2/readfilecontext.js
@@ -17,6 +17,7 @@ exports.getReadFileContextPrototype = function() {
let proto;
fsBinding.open = (_path, _flags, _mode, req) => {
proto = Object.getPrototypeOf(req.context);
+ originalOpen.apply(fsBinding, [_path, _flags, _mode, req])
};
fs.readFile('/ignored.txt', () => {});
Is this an acceptable change? |
That looks like a good solution to bypass the jest issue. Pls fire a PR, thx. |
Hello!
When using
mock-fs
in combination withjest
an open handleFSREQCALLBACK
is created which (potentially) prevents Jest from exiting cleanly. This can be detected usingjest --detectOpenHandles
(docs). This seems to happen at import/require ofmock-fs
, not when callingmockFs()
to actually mock the fs.The following is output from Jest:
Minimal example to reproduce
Any advice on how this might be avoided/worked around would be greatly appreciated! Thanks in advance!
The text was updated successfully, but these errors were encountered: