如何在 Jest 中正确模拟 fs.readFileSync()?

How to properly mock fs.readFileSync() in Jest?

我正在使用 fs 模块将 html 字符串导入我的模块,如下所示:

const fs = require('fs');    
const htmlString = fs.readFileSync("../utils/htmlString.html").toString();

然后,在我的测试文件中,我试图像这样模拟 fs 模块:

const fs = require('fs');
jest.mock("fs", () => {
  return {
    readFileSync: jest.fn()
  }
})
fs.readFileSync.mockReturnValue("test string");

我可能错误的逻辑告诉我,它应该正确地模拟原始字符串导入并将其替换为“测试字符串”字符串。然而,虽然 运行 它抛出的测试:

TypeError: Cannot read property 'toString' of undefined

我知道这意味着模拟不成功,因为它应该在字符串实例上成功调用 .toString()。

我在这里做错了什么?

您不需要为 jest.mock('fs') 显式提供模块工厂参数。 jest.mock() 在需要时模拟具有自动模拟版本的模块。这意味着 fs.readFileSync 是一个模拟方法,与 jest.fn().

相同

您需要确保在模拟 return 值后需要被测模块,因为模块范围内的代码将在需要时立即执行。

例如

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('./');
  });
});

测试结果:

 PASS  Whosebug/70760704/index.test.js (7.242 s)
  70760704
    ✓ should pass (14 ms)

  console.log
    htmlString:  test string

      at Object.<anonymous> (Whosebug/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',
};

包版本:

"jest": "^26.6.3"