簡體   English   中英

摩卡同步“之前”掛鈎超時

[英]Mocha synchronous “before all” hook timeout

我有一個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 ,但是結構本身保持不變)。

在您的before函數中,您可以設置this.timeout = xxxx並增加時間。 我通常將其設置為10000。可能會有更好的解決方案,但這就是我為我解決此問題的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM