摩卡同步“之前”挂钩超时

时间:2019-01-18 15:03:22

标签: javascript mocha

我有一个Mocha测试套件,间歇性地失败,并显示以下消息:

  1) "before all" hook

  0 passing (2s)
  1 failing

  1) "before all" hook:
     Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

精简的代码如下:

const rewiremock      = require("rewiremock").default;
const sinon           = require("sinon");

// Some more imports

let readFileSync;
let serverConfiguration;
let testingModule;

before(
  function ()
  {
    // Some mocking of the form:

    rewiremock(
      "fs"
    ).by(
      (
        mock
      ) =>
      {
        const realFs  = mock.requireActual("fs");
        readFileSync = sinon.spy(realFs, "readFileSync");

        return realFs;
      }
    );

    const originalModule = require("./path/to/my/mod");

    const fakeModule = {
      myMethod() {
        // Some behaviour-altering code and then:
        originalModule.myMethod();
      }
    };

    testingModule = rewiremock.proxy(
      "./path/to/testing/module",
      {
        "./path/to/my/mod"  : fakeHttpUiServerFactory
      }
    );

    console.log("one");

    // Some more mocking

    console.log("two");

    // Aaaand more mocking

    console.log("three");

    // Initialization of a configuration object

    serverConfiguration = {
        "nothing" : "special here"
    };

    console.log("four");
  }
);

挂钩是同步的;它没有done参数(如图所示),代码也不返回任何内容。我添加了一些日志,以尝试查看该功能在超时之前在哪里停止,但是每条日志都会被打印出来。似乎可以很好地执行该功能,但是随后它停止了足够长的时间,以至于发生超时。

还有什么我可以尝试添加的方法来帮助解决此问题?同样,这是间歇性发生的,因此,除了“我运行此代码”之外,很难给出适当的再现步骤。如果完整的代码版本有帮助,我可以提供它(有点,它是专有的,因此您会看到很多myThismyThat,但是结构本身将保持不变)。

1 个答案:

答案 0 :(得分:0)

在before函数中,您可以设置 this.timeout = xxxx 并增加时间。我通常将其设置为10000。可能会有更好的解决方案,但这就是我为我解决此问题的方式。

相关问题