【问题标题】:How does the execution scope work in jest for mockImplementationmockImplementation 的执行范围是如何工作的?
【发布时间】:2021-11-20 14:39:59
【问题描述】:

我正在尝试在玩笑测试中确定执行范围。

我有一个反应组件,在渲染期间执行了以下行:

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

这里是三个测试玩笑的例子

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

结果:

TypeError: Cannot read property 'm1' of undefined

如果我将 mockImplementation 移动到以下位置:

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

结果:

TypeError: Cannot read property 'm1' of undefined

最后:

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

结果:通过了。

我的问题是,如果我无法在 beforeEach 或每个特定测试用例之外执行模拟实现,我如何将以下内容添加到目前似乎失败的 jest 设置文件中:

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

【问题讨论】:

    标签: javascript reactjs unit-testing jestjs


    【解决方案1】:

    在这种情况下,您可能需要使用jest.spyOn。这将在原始方法上添加一个间谍,然后您可以执行您需要的任何类型的模拟:)

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

    【讨论】:

    • 谢谢,我知道 spyOn,但正在寻找执行范围的明确性。特别是在特定测试之外的模拟实现中创建 jest fn 的情况。然后更进一步,在设置文件中添加模拟函数 - 以便它可用于所有测试。
    猜你喜欢
    • 2013-11-24
    • 1970-01-01
    • 2017-04-05
    • 1970-01-01
    • 2020-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    相关资源
    最近更新 更多