LoginSignup
4
1

More than 5 years have passed since last update.

Puppeteer で JavaScript の実行時エラーを検知し、テストを失敗させる

Last updated at Posted at 2019-03-29

実行環境(package.json)

"engines": {
    "node": "^10.9.0",
    "yarn": "^1.12.0"
},
"dependencies": {
    "ava": "^1.2.0",
    "puppeteer": "^1.12.1"
},

現在開発しているプロダクトで、フロントエンドを Vue.js でつくっていて UI の見た目上は問題なさそうだけど、実は裏で JavaScript の実行エラーが発生していたケースが何度かあり、そういったエラーを E2E テストでも拾いたいと考え、このような仕組みを入れています。

参考:event: 'console'

  // JavaScript、Vue コンポーネント関連のエラーを捕捉し、テストを失敗させる
  // NOTE: page.on("error"), page.on("pageerror") でも捕捉できそうだが
  //       期待通り動かなかったので console のエラーメッセージをチェックする
  page.on("console", msg => {
    const type = msg.type();
    const text = msg.text();
    if (type === "error" || type === "warning") {
      test.fail(text);
    }
  });
4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1