Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

1.1K
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda