如何模拟返回图像缓冲区的axios调用



我试图模拟axios api调用,它返回图像缓冲区,如下所示:

<Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 00 00 01 00 01 00 00 ff e1 00 de 45 78 69 66 00 00 49 49 2a 00 08 00 00 00 06 00 12 01 03 00 01 00 00 00 01 00 ... 12688 more bytes>

我的fetch函数是单独实现的。

const axios = require('axios');
const logger = require('./logger');
const imageApi = axios.create({
baseURL: 'https://endpoint',
timeout: 5000,
});
const fetchRandomImage = async ({ imageId, width, height }) => {
try {
// Fetch image
const response = await imageApi.get(`/id/${imageId}/${width}/${height}`, {
responseType: 'arraybuffer',
});
return response.data;
} catch (error) {
return null;
}
};
module.exports = fetchRandomImage;

使用jest和mock来实现这个测试的正确方法是什么?

我模拟promise方法的方法:

jest.mock("./path-to-fetchRandomImage", () => jest.fn(() => Promise.resolve('whatever-you-want')));

最新更新