简体   繁体   English

测试失败 - 多次调用 Mocha 的 done()

[英]Failing test - Mocha's done() called multiple times

I've tried looking at topics with a similar error but could not fit those solutions into the context of my issue.我尝试查看具有类似错误的主题,但无法将这些解决方案融入我的问题的上下文中。

When I try to run the the following test (function included that is tested):当我尝试运行以下测试时(包含的功能已测试):

function myFunc(next, obj) {
  const pairs = {};
  obj.listing.forEach((element) => {
    if (element.x in pairs && pairs[element.x] !== element.y) {
      const err = new Error('This was not ok');
      next(err);
    } else {
      pairs[element.x] = element.y;
    }
  });
  next();
}

it('should fail as 9 has been changed to 5 in the second object of the listing', function (done) {
  const callback = (err) => {
    if (err && err instanceof Error && err.message === 'This was not ok') {
      // test passed, called with an Error arg
      done();
    } else {
      // force fail the test, the `err` is not what we expect it to be
      done(new Error('Assertion failed'));
    }
  }
  myFunc(callback, {
    "listing": [
      { "x": 5, "y": 9 },
      { "x": 5, "y": 11 }
    ]
  });
});

I get this error:我收到此错误: 在此处输入图像描述 What is the cause of this and how can I fix it?这是什么原因,我该如何解决?

You need to add a return in the if block of your myFunc so that the callback function next is called only once and indeed the done() callback in the main test case:您需要在myFuncif块中添加一个return ,以便只调用一次回调next并且确实在主测试用例中调用done()回调:

function myFunc(next, obj) {
  const pairs = {};
  obj.listing.forEach((element) => {
    if (element.x in pairs && pairs[element.x] !== element.y) {
      const err = new Error('This was not ok');
      return next(err);
    } else {
      pairs[element.x] = element.y;
    }
  });
  next();
}

@Ankif Agarwal's solution was not the correct one but it did point me in the right direction. @Ankif Agarwal 的解决方案不是正确的,但它确实为我指明了正确的方向。

The forEach() method is not short circuited and therefor makes a call to next() more than once ( Short circuit Array.forEach like calling break ). forEach() 方法没有短路,因此多次调用 next() ( 短路 Array.forEach 就像调用 break 一样)。

I was able to solve this in one of two way's.我能够以两种方式之一解决这个问题。

By extracting the call to next() from the forEach() logic:通过从 forEach() 逻辑中提取对 next() 的调用:

function myFunc(next, obj) {
  const pairs = {};
  let err = null;
  obj.listing.forEach((element) => {
    if (element.x in pairs && pairs[element.x] !== element.y) {
      err = new Error('This was not ok');
    } else {
      pairs[element.x] = element.y;
    }
  });

  if (err !== null) {
    next(err);
  } else {
    next();
  }
}

However this still makes the forEach() run through all element.然而,这仍然使 forEach() 贯穿所有元素。 If possible it seems better to short circuit it and break out of it soon as a violation occurs that sets the error, like so:如果可能的话,最好将其短路并在发生设置错误的违规行为时立即将其断开,如下所示:

function myFunc(next, obj) {
  const pairs = {};
  const BreakException = {};
  let err = null;
  try {
    obj.listing.forEach((element) => {
      if (element.x in pairs && pairs[element.x] !== element.y) {
        err = new Error('This was not ok');
        throw BreakException;
      } else {
        pairs[element.x] = element.y;
      }
    });
    next();
  } catch (e) {
    if (e !== BreakException) throw e;
    next(err);
  }
}

Hopefully someone can use this in the future.希望将来有人可以使用它。

暂无
暂无

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

相关问题 “done() 被多次调用”带摩卡咖啡的 Moongoose - "done() called multiple times" moongoose with mocha 为什么 mocha 测试在钩子 &lt;“before each” ....&gt; 中多次调用“错误完成()”? - Why is mocha test throwing "Error done() called multiple times in hook <"before each" ....>? 确保在此测试中调用done()回调(Mocha,Chai,Sinon) - Ensure the done() callback is being called in this test (Mocha, Chai, Sinon) 与“getByTestId”一起使用时,Jest 的“it.each”会导致“预期完成被调用一次,但它被多次调用”错误 - Jest's `it.each` causes a 'Expected done to be called once, but it was called multiple times' error when used with 'getByTestId' 使用不同的数据多次运行相同的mocha测试 - Running the same mocha test multiple times with different data Mocha&Chai-超过2000毫秒的超时。 确保在此测试中调用done()回调。“ - Mocha & Chai- “timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.” 摩卡测试使用打字稿失败 - Mocha test failing using typescript “ it”块执行多次,直到在Jasmine 2.0中调用“ done”为止 - 'it' block executes multiple times until 'done' is called in Jasmine 2.0 基于事件的单元测试因“done() 被多次调用”而失败 - Event-based unit tests fail with “done() called multiple times” 为什么多次调用此jQuery函数失败? - Why is this jQuery function failing when called on multiple times?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM