Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

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

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?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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"
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda