【发布时间】:2021-08-26 02:37:58
【问题描述】:
我正在尝试模拟 nanoid 以进行测试,但它似乎不起作用。
我的功能
public async createApp(appDto: ApplicationDto): Promise<string> {
const appWithToken = { ...appDto, accessToken: nanoid() };
const application = await this.applicationModel.create(appWithToken);
return application.id;
}
我的测试:
beforeEach(() => {
mockRepository.create.mockResolvedValueOnce({ id: mockId });
});
test("creates application and returns an id", async () => {
const mockAppDto: ApplicationDto = { email: "123@mock.com" };
const application = await applicationService.createApplication(mockAppDto);
expect(mockRepository.create).toHaveBeenCalledWith(mockAppDto); //how do I mock the nanoid here?
expect(application).toBe(mockId);
});
所以基本上我正在努力弄清楚如何模拟函数内部生成的 nanoid。
我在文件顶部尝试了以下内容:
jest.mock('nanoid', () => 'mock id');
但它根本不起作用。
任何帮助将不胜感激!
【问题讨论】:
标签: javascript typescript jestjs mocking