-
Notifications
You must be signed in to change notification settings - Fork 87
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
TypeError on import or require #344
Comments
I got no issue with both Node v17.0.1 and v17.1.0 and mock-fs v5.1.2. What's TS v10.2.1? |
TypeScript 🙂 |
Current typescript is v4. What's v10? |
Got the same problem after updating... something. I've tracked down the issue to It works with plain node, but it does not work with yarn PnP (3.1.1). When I run this code from the above mentioned file: getReadFileContextPrototype = function() {
const fs = require('fs');
const fsBinding = process.binding('fs');
const originalOpen = fsBinding.open;
let proto;
fsBinding.open = (_path, _flags, _mode, req) => {
proto = Object.getPrototypeOf(req.context);
return originalOpen.apply(fsBinding, [_path, _flags, _mode, req]);
};
fs.readFile('/ignored.txt', () => {});
fsBinding.open = originalOpen;
return proto;
} With just Are other people with this issue also using yarn? |
Seems like when using yarn, it only works with mock-fs@5.0.0 and node 14:
Using mock-fs@5.0.0 with node 16 gives a different error, but it seems to be the same underlying cause. |
I'm having it after migrating to Yarn 3 + PnP |
I assume yarn pnp monkey-patched nodejs internals (something like what mock-fs does). |
I found that the getReadFileContextPrototype will alwasy return null if you first call:
in your test before mock. within this function:
When all of the fs functions are mocked, and the fsBinding.open never gets called, so proto stays undefined. |
This is happening to me using jest + typescript + mock-fs on es6 module mode. When I was using commonjs without typescript, it ran with the |
I've also found the following if anyone's interested:
or a
so I converted all of my readFileSync -> readFile and writeFileSync -> writeFile within my tests + code and they seemed to work. I'm not sure why this is. I have my suspicion that there might be something funny when running everything with es6 modules + typescript. es6 module imports work asynchronously, where as commonjs require doesn't, so there might be something around that when loading the fsBinding. |
or
Results in
Node:
v17.0.1
TS:
v10.2.1
mock-fs:
5.1.2
The text was updated successfully, but these errors were encountered: