如何模拟回调函数接收到的数据(参数),以便开玩笑地测试回调函数中的逻辑?(例如 fs.readFile)



我有函数想要做单元测试,例如,它包含:

function foo (file, option) {
return new Promise((resolve, reject) => fs.readFile(file, option, (err, content) => {
if (err) return reject(new Error(`Failed to read the file: (${file})`));
else {
(...some operations, and that is the point for me to test it...)
return resolve(...some result...);
}
}));
}

就在测试文件的开头,我有:

jest.mock('fs', () => ({
readFile : jest.fn(),
}));
const fs = require('fs');

测试逻辑如下所示:

test('Should get context as string from the template file', async () => {
const mockContent = '<a>Hello World</a>';
fs.readFile.mockReturnValue(mockContent);
const result = (await foo('test', 'utf8')).then(() => 123);
//
expect(result).toEqual(123);
});

但是,当我尝试运行测试时,它显示:

- Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.`

我知道这是一个问题,因为 Jest 网站的承诺尚未解决; 但他们的建议不是我实际上拥有的......我认识到我的问题是由于fs.readFile(...)被返回undefined的模拟函数所取代,以便new Promise永远不会被拒绝/解决(我说得对吗?!

我的问题是我如何进行像这种情况这样的单元测试?我真的不想触发 I/O 事件,例如在 oder 中使用空文件来使其工作......有什么想法吗?!谢谢!

更新:我认为一个非常明确的问题是我们如何模拟回调函数接收的数据。在我的示例中,看起来像是针对它进行测试,但在我的实际代码中,我真正想要测试的是回调函数的逻辑。

问题是你嘲笑js.readFile的方式。mockReturnValue只是创建一个返回某些内容的函数,但您希望它调用其参数,因此您需要使用mockImplementation自己定义模拟函数

jest.fn().mockImplementation((file, option, cb) => cb(null, mockContent)) 

jest.fn().mockImplementation((file, option, cb) => cb('Some Error', null))

最新更新