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

135
Visualizações
Jest beforeAll vs beforeEach unexpected behaviour

I have a quite simple test, basically I'm trying to mock i18next's t function:

import { t } from 'i18next';
import { changeDocumentTitle } from './utils';

jest.mock('i18next');

const tMock = (key: string): string => key;

beforeAll(() => {
  (t as jest.Mock).mockImplementation(tMock);
});

test('test changeDocumentTitle function', () => {
  changeDocumentTitle('string');
  expect(document.title).toEqual(tMock('string'));
});

and the changeDocumentTitle implementation:

import { t } from 'i18next';

export const changeDocumentTitle = (titleKey: string): void => {
  document.title = t(`${titleKey}`);
};

Unfortunately, the test fails. But if I change it from beforeAll to beforeEach, everything's fine. How is that? Why beforeAll is not applying my mock unlike beforeEach?

Thanks in advance.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Since react-scripts v4 it added the default jest config resetMocks: true: https://github.com/facebook/create-react-app/releases/tag/v4.0.0

This means that the mocks are being reset before the tests runs with beforeAll. I recommended keeping resetMocks: true, because tests should be isolated.

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