-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Docs: Improve mocks section for promise-based fs/promises #11136
Comments
As an examle, mocking current promises can be done like: const fs = jest.createMockFromModule('fs');
let mockFiles = {};
/**
* This is a custom function that our tests can use during setup to specify
* what the files on the "mock" filesystem should look like when any of the
* `fs` APIs are used.
*
* @param {Object} newMockFiles - The keys are the file paths and the values are
* the file contents.
*
* @example
* import * as fs from 'fs';
* jest.mock('fs');
* const files = {
* '/path/to/file1.js': 'console.log("file1 contents");',
* '/path/to/file2.txt': 'file2 contents',
* };
* fs.setMockFiles(files);
*/
function setMockFiles(newMockFiles) {
mockFiles = { ...newMockFiles };
}
fs.setMockFiles = setMockFiles;
fs.readFileSync = jest.fn((filePath) => mockFiles[filePath]);
fs.promises = {};
fs.promises.readFile = jest.fn(async (filePath) => mockFiles[filePath]);
module.exports = fs; But the docs does not explain how to do it when mocking the more modern |
doesn't Oh, you mean with a manual mock. I never use those, so haven't had the need for one. I guess |
@SimenB It did work like a charm, like this below. Does it seem functionally correct to you? I wonder if this would be a good addition to the docs somewhere, or would it be too verbose for the docs? node_modules/
__mocks__/
fs/
promises.js
fs.js // fs.js
const fs = jest.createMockFromModule('fs');
let mockFiles = {};
/**
* This is a custom function that our tests can use during setup to specify
* what the files on the "mock" filesystem should look like when any of the
* `fs` APIs are used.
*
* @param {Object} newMockFiles - The keys are the file paths and the values are
* the file contents.
*
* @example
* import * as fs from 'fs';
* jest.mock('fs');
* const files = {
* '/path/to/file1.js': 'console.log("file1 contents");',
* '/path/to/file2.txt': 'file2 contents',
* };
* fs.setMockFiles(files);
*/
function setMockFiles(newMockFiles) {
mockFiles = { ...newMockFiles };
}
/**
*
* @returns {Object} The mock files. The keys are the file paths and the values are
* the file contents.
*/
function getMockFiles() {
return mockFiles;
}
fs.setMockFiles = setMockFiles;
fs.getMockFiles = getMockFiles;
fs.readFileSync = jest.fn((filePath) => mockFiles[filePath]);
fs.existsSync = jest.fn((filePath) => filePath in mockFiles);
module.exports = fs; // fsp.js
const fs = require('fs');
const fsp = jest.createMockFromModule('fs/promises');
fsp.readFile = jest.fn(async (filePath) => fs.getMockFiles()[filePath]);
fsp.mkdir = jest.fn(async () => {});
module.exports = fsp; And then in the suite: // foo.unit.test.js
jest.mock('fs');
jest.mock('fs/promises');
fs.setMockFiles(...); |
The whole example is probably a bit too verbose, but we can definitely add a comment above the existent example saying how it applies to |
Yes, let this linger and maybe I can push a patch set when I get some time! Or if some other hero steps in before me. |
This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days. |
Hey, @SimenB is the documentation for fs.promises mocks still needed? I did mocks just like it fairly recently if help still wanted on this |
This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days. |
This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one. |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
馃殌 Feature Proposal
https://jestjs.io/docs/en/manual-mocks describes how to mock the 'fs' module. Although good, the latest versions of Node.js 15 puts promise-based version first and fore-most at the top: https://nodejs.org/api/fs.html
It would be cool if https://jestjs.io/docs/en/manual-mocks could describe how to e.g. mock the module
require('fs/promises')
.Motivation
It would help users moving towards more modern tools such as promised-based versions of e.g. 'fs'.
Example
n/a
Pitch
Because it is a core feature of Jest to mock.
The text was updated successfully, but these errors were encountered: