とろろこんぶろぐ

かけだしR&Dフロントエンジニアの小言

Jest did not exit one second after the test run has completed

Headless chromeのpuppeteerを使ったサービスでjestのテストを動かしていたらテストには成功したのにエラーらしき文言が表示された。

f:id:ka2jun8:20181211231651p:plain

Jest did not exit one second after the test run has completed.

This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.

testは完了してもexitしてなかったけど?とのことだ。

testは大したことしておらず。 以下のハンドラが返ってくるかしかみていなかった。

export const handler = async (event) => {
    await launchChrome({
      flags: ["--headless", "--disable-gpu"],
    });
    const browser = await puppeteer.connect({
      browserWSEndpoint: debuggerUrl,
    });
    const context = browser.defaultBrowserContext();
    const page = await context.newPage();

    baseUrl = new URL(event.url).origin;
    await page.goto(href);
    const result = await page.content();
    return result;
};

async functionでもtestでdoneを呼び出さないといけないのかと一瞬思ったが、async/awaitならdoneは必要なかった。

An Async Example · Jest

--forceExit を使えというコメントもあるが、どう考えても本質的な解決策じゃない。

jestのpuppeteerを利用する例をみて見ると、

close puppeteer with Global Teardown

と書かれていた。

Using with puppeteer · Jest

どうやらjestはnetworkのconnectionが閉じていないと怒ってくれるっぽい。

    await page.close();
    await browser.close();

を入れたら無事エラーは出なくなった。