我被要求通过使用 it.each 或创建一个函数来减少测试中的重复。我不知道该怎么做。我在测试套件中还有其他预计不会出错的功能。我假设我可以将下面两个重复的断言放在辅助函数中,但如果您有更好的建议,请告诉我。谢谢。
expect(<jest.Mock>fetch).not.toHaveBeenCalled();
expect(e).toBeInstanceOf(TypeError);
测试文件:
it('should throw if no args were passed', async () => {
expect.assertions(3);
return readPricing().catch((e: Error) => {
expect(<jest.Mock>fetch).not.toHaveBeenCalled();
expect(e).toBeInstanceOf(TypeError);
expect(e.message).toBe(
`The 'products' information is required to successfully be able to execute this request.`
);
});
});
it('should throw if no product IDs were passed', () => {
expect.assertions(3);
const noIdData = {
products: [{ salesPrice: '60.00' }, { salesPrice: '80.00' }],
};
// @ts-expect-error
return readPricing(noIdData).catch((e: Error) => {
expect(<jest.Mock>fetch).not.toHaveBeenCalled();
expect(e).toBeInstanceOf(TypeError);
expect(e.message).toBe(`The 'productId' is required to successfully be able to execute this request.`);
});
});
it('should throw if an empty array of products was passed', () => {
expect.assertions(3);
const noIdData = { products: [] };
return readPricing(noIdData).catch((e: Error) => {
expect(<jest.Mock>fetch).not.toHaveBeenCalled();
expect(e).toBeInstanceOf(TypeError);
expect(e.message).toBe(
`The 'products' information is required to successfully be able to execute this request.`
);
});
});
我尝试使用它。每个但未能成功设置它,我不确定给定的输入是否可行。