我有一个在版本3.4.0中工作的uuid的jest spyOn,在将其升级到8.3.2后,测试中断,并在基元值上出现错误Cannot spyOn;未定义给定
import uuid from 'uuid';
jest.spyOn(uuid, 'v4');
模拟uuid lib版本8.x的最佳方法是使用Mocking Partials:
jest.mock( 'uuid', () => ({
v4: jest.fn(() => '1234567890' )
}));
您需要更改导入uuid 的方式
import { v4 as uuidv4 ) from uuid;
https://github.com/uuidjs/uuid#readme