Closed jloleysens closed 2 years ago
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 exports.getReadFileContextPrototype
and the call to fs.readFile('/ignore.txt...
. Which I don't think is specific to Jest.
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
```js // my.test.js const mockFs = require('mock-fs'); test('nothing', () => {}); ```Any advice on how this might be avoided/worked around would be greatly appreciated! Thanks in advance!