-
Notifications
You must be signed in to change notification settings - Fork 86
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
Fix compatibility with Node 20 #384
Comments
For anyone trying to fix it, it's caused by node20 to use new binding method: It looks like we should just implement open+read+toUtf+close as one function here. The usage: https://github.com/nodejs/node/blob/a81788cb27b96579343765eeba07fd2b772829d0/lib/fs.js#L456 Implementation This is very naive and PoC binding that fixes
|
Facing the exact same issue, it was working completely fine then randomly broke. Below is an example of the setup:
Getting the error: |
For those like me who came here in a panic because their tests started failing, I had success (and minimal effort) replacing |
Do you mind post some examples please? I'm trying to use memfs but I'm struggling with it. I found an basic example with jest however, i'm using chai so nothing is working For reference, this is the basic example of or https://dev.to/julienp/node-js-testing-using-a-virtual-filesystem-as-a-mock-2jln |
@PedroS11 I also decided to switch using memfs. For me, it was mostly a matter of switching So for example (I am using vitest in this but jest should be pretty close as well) Using mock-fsconst mockFS = require("mock-fs");
import { afterEach, beforeEach, describe, expect, test } from "vitest";
describe("some test", () => {
beforeEach(() => {
mockFS({
Projects: {
"Projects - 23": {
"Test Client": {
"Projects 1": {
"Sub folder": {},
},
},
},
"Projects - 24": {},
},
});
});
afterEach(() => {
mockFS.restore();
});
test("project folder creation", () => {
const projectFolderPath = path.join(
"Projects",
"Projects - 23",
"Test Client",
"Projects 2",
);
const result = _createProjectFolder(projectFolderPath, projectTemplatePath);
expect(result).toEqual({ path: projectFolderPath, error: null });
});
}); Using memfsimport { fs, vol } from "memfs";
import { afterEach, beforeEach, describe, expect, test } from "vitest";
describe("some test", () => {
beforeEach(() => {
vol.fromNestedJSON({ // <-- Changed this from mockFS
Projects: {
"Projects - 23": {
"Test Client": {
"Projects 1": {
"Sub folder": {},
},
},
},
"Projects - 24": {},
},
});
});
afterEach(() => {
vol.reset(); // <-- changed this from mockFS.restore();
});
test("project folder creation", () => {
const projectFolderPath = path.join(
"Projects",
"Projects - 23",
"Test Client",
"Projects 2",
);
const result = _createProjectFolder(projectFolderPath, projectTemplatePath);
expect(result).toEqual({ path: projectFolderPath, error: null });
});
}); I hope that helps! |
Thank you for this snippet @JBloss1517 !! in the following weeks, I'll try to migrate too and I'll see how it works. I'll leave my progress here when I start so it helps everyone with this struggle |
Currently, can not use Node.js >= 20.10 because mock-fs used in the unit tests does not support it. tschaub/mock-fs#384
Currently, can not use Node.js >= 20.9 because mock-fs used in the unit tests does not support it. tschaub/mock-fs#384
Currently, can not use Node.js >= 20.8 because mock-fs used in the unit tests does not support it. tschaub/mock-fs#384
Currently, can not use Node.js >= 20.8 because mock-fs used in the unit tests does not support it. tschaub/mock-fs#384
Currently, can not use Node.js >= 20.8 because mock-fs used in the unit tests does not support it. tschaub/mock-fs#384
hi @tschaub, are there any plans to work on this? |
@dennismeister93 - I would be happy to review a pull request, but don't have plans to spend time on a fix myself. |
struggling with the same problem here. |
I was having issues with my Jest tests still using the built-in fs instead of the memfs implementation. This worked to mock out fs & fs/promises:
|
Here's an example of migrating a mock-fs test that used Before with mock-fs:
After with memfs:
|
What is the mocha equiv. of jest.mock? |
In mocha there is a little more work required, because it does not include a mocking system of its own. Usually you use the excellent Sinon for mocking, but in this special case this won't work since, by specification, imports must be unalterable. Instead, I use esmock to mock out modules. Something like this works for me:
|
mock-fs is incompatible with newer versions of Node (see tschaub/mock-fs#384) and in the long run unlikely to be fixable (tschaub/mock-fs#383).
Previously, the jest tests used mock-fs to mock the file system. However, there is compatibility issues between mock-fs and node 20. See this issue: tschaub/mock-fs#384
Since we upgraded to node 20.10.0 the method for mocking file creation doesn't work properly
The text was updated successfully, but these errors were encountered: