Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

184
Views
¿Cómo funciona el ámbito de ejecución en broma para la implementación simulada?

Estoy tratando de resolver los ámbitos de ejecución en las pruebas de broma.

Tengo un componente de reacción con la siguiente línea que se ejecuta durante el renderizado:

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

Aquí hay tres ejemplos de broma de pruebas.

 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(''); }); });

Salir:

 TypeError: Cannot read property 'm1' of undefined

Si muevo la implementación simulada a lo siguiente:

 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(''); }); });

Salir:

 TypeError: Cannot read property 'm1' of undefined

Y finalmente:

 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(''); }); });

Resultado: pasa.

Mi pregunta es si no puedo ejecutar la implementación simulada fuera de beforeEach o cada caso de prueba en particular, ¿cómo puedo agregar lo siguiente a los archivos de configuración de broma que parecen estar fallando en este momento?

 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 answers
Answer question

0

En tal caso, es posible que desee utilizar jest.spyOn . Esto agregará un espía sobre el método original y luego podrá realizar cualquier tipo de burla que necesite :)

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!