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

134
Vistas
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 Respuestas
Responde la pregunta

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 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