I am using the fs module to import a html string to my module, like this:
const fs = require('fs');
const htmlString = fs.readFileSync("../utils/htmlString.html").toString();
Then, in my test file I am trying to mock the fs module like this:
const fs = require('fs');
jest.mock("fs", () => {
return {
readFileSync: jest.fn()
}
})
fs.readFileSync.mockReturnValue("test string");
My possibly wrong logic tells me that it should properly mock the original string import and replace it with a "test string" string. However, while running the test it throws:
TypeError: Cannot read property 'toString' of undefined
I understand that this means the mock was unsuccessful, as it should successfully call .toString() on a string instance.
What I am doing wrong in here?
CodePudding user response:
You don't need to provide the module factory argument explicitly for jest.mock('fs'). jest.mock() mocks a module with an auto-mocked version when it is being required. This means the fs.readFileSync is a mock method with same with jest.fn().
You need to make sure to require the module under test after mocking the return value, since the code in the module scope will be executed instantly when it's be required.
E.g.
index.js:
const fs = require('fs');
const htmlString = fs.readFileSync('../utils/htmlString.html').toString();
console.log('htmlString: ', htmlString);
index.test.js:
const fs = require('fs');
jest.mock('fs');
describe('70760704', () => {
test('should pass', () => {
expect(jest.isMockFunction(fs.readFileSync)).toBeTruthy();
fs.readFileSync.mockReturnValue('test string');
require('./');
});
});
Test result:
PASS stackoverflow/70760704/index.test.js (7.242 s)
70760704
✓ should pass (14 ms)
console.log
htmlString: test string
at Object.<anonymous> (stackoverflow/70760704/index.js:3:9)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 7.279 s, estimated 8 s
jest.config.js:
module.exports = {
testEnvironment: 'node',
};
package versions:
"jest": "^26.6.3"
