我想通过扩展默认mock的行为并在执行以下测试时将其恢复为原始实现来更改每个测试基础上的模拟依赖项的实现.
更简单地说,这就是我想要实现的目标:
我正在使用Jest v21.
以下是典型的Jest测试:
__mocks__/myModule.js
const myMockedModule = jest.genMockFromModule('../myModule');
myMockedModule.a = jest.fn(() => true);
myMockedModule.b = jest.fn(() => true);
export default myMockedModule;
Run Code Online (Sandbox Code Playgroud)
__tests__/myTest.js
import myMockedModule from '../myModule';
// Mock myModule
jest.mock('../myModule');
beforeEach(() => {
jest.clearAllMocks();
});
describe('MyTest', () => {
it('should test with default mock', () => {
myMockedModule.a(); // === true
myMockedModule.b(); // === true
});
it('should override …Run Code Online (Sandbox Code Playgroud) 我有一套简单的测试,在某些情况下我想模拟一个模块,而在某些情况下则不想。但是,jest.mock()只有在测试之外才有效。任何人都知道为什么会这样以及我做错了什么?
这是我想要模拟的函数的实际导入
import {hasSupport, getCallingCode} from 'utils/countryCallingCode';
Run Code Online (Sandbox Code Playgroud)
这是这个函数的模拟:
jest.mock('utils/countryCallingCode', () => ({
getCallingCode: () => '1',
hasSupport: () => true,
}));
Run Code Online (Sandbox Code Playgroud)
现在,工作场景是:
//imports
//mock
describe('...', () -> {
it('...', () -> {
});
});
Run Code Online (Sandbox Code Playgroud)
这不起作用:
//imports
describe('...', () -> {
//mock
it('...', () -> {
});
});
Run Code Online (Sandbox Code Playgroud)
这也不起作用:
//imports
describe('...', () -> {
it('...', () -> {
//mock
});
});
Run Code Online (Sandbox Code Playgroud) 我真的不确定我是否以正确的方式这样做!
我有一个正在测试的模块和一个测试脚本。MUT 中有 2 个函数,其中一个函数使用另一个函数。我尝试模拟该模块以用模拟替换实际的支持功能,但它似乎没有被调用。我这样做是正确的还是你不能因为引用平等而这样做?
穆特
const total_users = 100;
export function getRandomIntInclusive(min=0, max=0) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1) + min); //The maximum is inclusive and the minimum is inclusive
}
export default function getUsername(name='user'){
return `${name}_${getRandomIntInclusive(1, total_users)}`;
}
Run Code Online (Sandbox Code Playgroud)
测试代码:
import getUsername, {getRandomIntInclusive} from "../generateUsername";
const randomSpy = jest.spyOn(Math, 'random');
jest.mock('../generateUsername', () => {
const originalModule = jest.requireActual('../generateUsername');
return {
__esModule: true,
...originalModule,
getRandomIntInclusive: jest.fn((min=0, max=0) => 5), // <-- …Run Code Online (Sandbox Code Playgroud) 这个问题真的很好:Jest Mock module per test
它只是没有回答我的问题,即如何仅在一个测试中模拟它并使用原始组件保留其余部分。
如果我想对快照进行一些模拟而不是对其他测试进行一些模拟,那就有意义了
这是我目前的尝试
import React from 'react'
import { render } from '@testing-library/react'
jest.mock('components/Photo', () =>
jest.fn(() => jest.requireActual('components/Photo'))
)
import Photo from 'components/Photo'
import PhotoGrid from '.'
beforeEach(() => {
Photo.mockReset()
})
test('default renders 9 photos if provided 9', () => {
const photos = [...Array(9).keys()]
const { getAllByTestId, debug } = render(<PhotoGrid photos={photos} />)
debug()
expect(getAllByTestId(PHOTO_COMP_TEST_ID)).toHaveLength(9)
})
test('renders with masonry grid style', () => {
Photo.mockImplementation(() => <div />)
const photos = …Run Code Online (Sandbox Code Playgroud) javascript unit-testing reactjs jestjs react-testing-library
出于测试目的,我需要模拟 jwt-decode 库。我使用它如下:
const decodedToken: { exp: number } = jwt_decode(token);
Run Code Online (Sandbox Code Playgroud)
然后在测试中尝试了以下并得到如下错误:
jest.mock('jwt-decode');
Run Code Online (Sandbox Code Playgroud)
类型错误:无法读取未定义的属性“exp”
jest.mock('jwt-decode', () => ({
exp: 123,
}));
Run Code Online (Sandbox Code Playgroud)
TypeError: (0 , _jwtDecode.default) 不是函数