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

179
Vistas
How does the execution scope work in jest for mockImplementation

I'm trying to work out execution scopes in jest tests.

I have a react component with the following line being executed during the render:

console.log(fun().m1());   

Here's three example of jest of tests

import React from 'react';
import Enzyme, { shallow } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import SimpleInput from './SimpleInput';
import { fun } from './util';
jest.mock('./util', () => ({
  fun: jest.fn().mockImplementation(() => ({ m1: jest.fn() })),
}));

describe('SimpleInput', () => {
  test("should have '' initialValue by default", () => {
    const simpleInput = shallow(<SimpleInput />);
    expect(simpleInput.prop('value')).toBe('');
  });
});

Outcome:

TypeError: Cannot read property 'm1' of undefined

If I move the mockImplementation to the following:

import React from 'react';
import Enzyme, { shallow } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import SimpleInput from './SimpleInput';
import { fun } from './util';
jest.mock('./util');

describe('SimpleInput', () => {
  fun.mockImplementation(() => ({ m1: jest.fn() }));

  test("should have '' initialValue by default", () => {
    const simpleInput = shallow(<SimpleInput />);
    expect(simpleInput.prop('value')).toBe('');
  });
});

Outcome:

TypeError: Cannot read property 'm1' of undefined

And finally:

import React from 'react';
import Enzyme, { shallow } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import SimpleInput from './SimpleInput';
import { fun } from './util';
jest.mock('./util');

describe('SimpleInput', () => {
  beforeEach(() => {
    fun.mockImplementation(() => ({ m1: jest.fn() }));
  });

  test("should have '' initialValue by default", () => {
    const simpleInput = shallow(<SimpleInput />);
    expect(simpleInput.prop('value')).toBe('');
  });
});

Outcome: it passes.

My question is if I can't execute the mock implementation outside of the beforeEach or each particular test case, how can I add the following to the jest setup files which seems to be failing at the moment:

Object.defineProperty(window, 'matchMedia', {
   writable: true,
   value: jest.fn().mockImplementation((query) => ({
     matches: false,
     media: query,
     onchange: null,
     addListener, // Deprecated
     removeListener: jest.fn(), // Deprecated
     addEventListener: jest.fn(),
     removeEventListener: jest.fn(),
     dispatchEvent: jest.fn(),
   })),
}); 
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

In such case, you may want to use jest.spyOn. This will add a spy over the original method and you then can perform any kind of mocking you need :)


let performanceNowSpy

beforeEach(() => {
    performanceNowSpy = jest.spyOn(window.performance, 'now')
})

afterEach(() => {
    performanceNowSpy.mockReset()
})

afterAll(() => {
    performanceNowSpy.mockRestore()
})

test('meaningful test name', () => {
    performanceNowSpy.mockReturnValueOnce(0)
    // A simple example.
    // `performance.now()` can be called from another module too.
    expect(performance.now()).toBe(0)
})
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