简体   繁体   English

带回调的单元测试异步功能

[英]Unit testing async function with callback

Trying to write unit tests for reading a json file with the readFile function however I get the error: Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout. 尝试编写使用readFile函数读取json文件的单元测试,但出现错误:超时-在jest.setTimeout指定的5000ms超时内未调用异步回调。 I must be doing something wrong when mocking the json file. 模拟json文件时,我一定做错了。

The function: 功能:

function jsonReader(filePath, cb) {
  fs.readFile(filePath, (err, fileData) => {
    if (err) {
      return cb && cb(err);
    }
    try {
      const object = JSON.parse(fileData);
      return object;
    } catch (err) {
      return cb && cb(err);
    }
  });
}
module.exports = jsonReader;

Then the testfile: 然后是测试文件:

const jsonReader = require('.././ReadJson');
jest.mock('fs', () => {
  const MOCK_FILE_INFO = { 'test.json': JSON.stringify({ name: 'myname' }) };
  return {
    readFile: (fpath, opts) => {
      if (fpath in MOCK_FILE_INFO) {
        return MOCK_FILE_INFO[fpath];
      }
    }
  };
});


test('Test file', (done) => {
  function callback(data) {
    expect(data.name).toBe('myname');
    done();
  }

  jsonReader('test.json', callback);
});

I tried to change the timeout but if I put it higher the execution also takes longer and it's still giving the same error. 我试图更改超时时间,但是如果我将超时时间设置得更高一些,则执行也将花费更长的时间,并且仍然出现相同的错误。

You're trying to use your functions synchronously? 您正在尝试同步使用功能吗?

jest.mock('fs', () => {
  const MOCK_FILE_INFO = { 'test.json': JSON.stringify({ name: 'myname' }) };
  return {
    readFile: (fpath, callback) => {
      if (fpath in MOCK_FILE_INFO) {
        callback(null, MOCK_FILE_INFO[fpath]);
      }
    }
  };
});
function jsonReader(filePath, cb) {
  fs.readFile(filePath, (err, fileData) => {
    if (err) {
      return cb && cb(err);
    }
    try {
      const object = JSON.parse(fileData);
      cb(object);
    } catch (err) {
      return cb && cb(err);
    }
  });
}
module.exports = jsonReader;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单元测试嵌套异步时异步回调超时 function - Async callback timeout when unit testing nested async function 测试用作setTimeout回调的异步函数 - Testing an async function used as the callback from setTimeout 用Jest进行单元测试Redux异步功能 - Unit Testing redux async function with Jest 使用Jasmine进行回调测试异步 - Async with callback testing with Jasmine undefined不是函数:单元测试护照身份验证自定义回调 - undefined is not a function: unit testing passport authentication custom callback Sinon 如何存根方法以对异步函数进行单元测试 - Sinon how to stub method for unit testing a Async function 如何在单元测试之前等待异步调用在组件的挂载回调中完成? - How to wait for an async call to finish in component's mount callback before unit testing it? Sinon 单元测试。 如何对将返回 promise 的 function 进行单元测试,该 function 将通过回调调用另一个 function? - Sinon unit testing. How to unit test a function that will return a promise that will call another function with callback? 在Mocha中测试无回调的异步方法 - Testing an async method with no callback in mocha 异步等待单元测试问题 - Async await unit testing issue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM