对于嘲弄uuidv4,我使用的是:
import { v4 as uuidv4 } from "uuid";
jest.mock("uuid");
uuidv4.mockReturnValue("uuid123");对于嘲弄window.confirm,我使用的是:
window.confirm = jest.fn().mockImplementation(() => true);这两种都很好用。但当我尝试这样做的时候
const uuidv4 = jest.fn.mockImplementation(() => "uuid123");我知道这个错误
TypeError: jest.fn.mockImplementation is not a function我在jest.fn()和jest.mock()之间感到困惑。
请有人详细说明该使用哪一种,何时使用,并举例说明好吗?
发布于 2021-01-25 08:59:32
给你一个简单的解释:
jest.mock是模拟某个模块。一旦编写了v4.mockReturnValue('yourV4Id');,就意味着所有导出的东西都将被转换为jest.Mock类型,这就是为什么您可以模拟v4方法:
jest.mock('aModule');
import {aMember} from "aModule";
// is now a jest mock type <=> jest.fn()
aMember.mockReturnValue('a value');jest.fn是一个函数,它返回一个jest.Mock类型,可以认为它是一个函数,可以用来创建任何您想要的东西:const aMock = jest.fn().mockReturnValue(1) // <=> const aMock = () => 1;
// The difference is jest mock type can be used to assert then. Most of cases is to check
// whether it gets called or nothttps://stackoverflow.com/questions/65880573
复制相似问题