MochaでPromiseのテストの際にTimeout of 2000ms exceeded.

開発Tips 2017年4月9日

vue-cliを使うと自動でテストに入ってくるKarma, Mochaですが、PromiseのUnitテストの際にタイトルのエラーがかなりの頻度で出ます。
Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

外部APIを叩いたりすると、普通にタイムアウトします。そこで、以下のように設定することで回避できます。

it('should success fetch', function() {
  // ここでset timeout
  this.timeout(10000)
  // promiseをリターン
  return promise.then(res => {
    expect(res.hoge).to.equal(true)
  })
})

テストが多くなっていちいち設定を書くのが面倒になったら、以下に共通設定を書くことができます。

config.set({
  ...
  client: {
    mocha: {
      timeout: 10000
    }
  }
})

結構探し回ったので、参考になれば幸いです。

slont

金融ベンチャーでWebエンジニア

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.