我正在尝试为我的一项测试模拟UUID模块,它应该在每次测试后恢复到原始状态,这是我的代码。
import uuid from "uuid";
describe("test", () => {
beforeAll(() => {
// checking if it hasn't been mocked elsewhere
console.log(uuid.v4());
});
afterAll(() => {
jest.resetAllMocks();
});
it("should set a mock of uuid.v4", () => {
(uuid.v4 as jest.Mock) = jest.fn();
(uuid.v4 as jest.Mock).mockReturnValueOnce("tester");
console.log(1, uuid.v4());
});
it("should restore the mocked version", () => {
// this should be different to the mocked version above.
console.log(2, uuid.v4());
});
});
当前,第一个控制台日志返回模拟值,然后下一个控制台日志返回undefined
,我希望它返回到其原始状态,以便它按原样返回UUID字符串。
有什么建议吗?