Search code examples
playwrightplaywright-test

How to detect "Uncaught (in promise) TypeError" in Playwright?


I'm using Playwright to test my TypeScript webapp, and I want to ensure no errors are logged to the console. (I want my tests to fail if an error occurs.)

To do so, based on this post, I use the following code:

import { test as base, expect } from '@playwright/test';

export const test = base.extend({
  page: async ({ baseURL, page }, use) => {
    const messages: string[] = [];
    page.on('console', (msg) => {
      // Ignore regular log messages; we are only interested in errors.
      if (msg.type() === 'error') {
        messages.push(`[${msg.type()}] ${msg.text()}`);
      }
    });
    await use(page);
    expect(messages).toStrictEqual([]);
  },
});

export default test;

This code will correctly cause the test to fail if console.error is used in the app.

However, Uncaught (in promise) TypeError is ignored by this check; the test completes successfully even though the following message is logged to the console:

ion-refresher.js:526 Uncaught (in promise) TypeError: ee.componentOnReady is not a function
    at _class._callee8$ (ion-refresher.js:526:21)
    at tryCatch (regeneratorRuntime.js:86:17)
    at Generator._invoke (regeneratorRuntime.js:66:24)
    at Generator.next (regeneratorRuntime.js:117:21)
    at asyncGeneratorStep (asyncToGenerator.js:3:20)
    at _next (asyncToGenerator.js:25:9)
    at asyncToGenerator.js:32:7
    at new Promise (<anonymous>)
    at _class.<anonymous> (asyncToGenerator.js:21:12)
    at _class.connectedCallback (ion-refresher.js:187:51)

I want to catch this kind of error (Uncaught (in promise) TypeError) automatically when running my Playwright tests because it should never occur. How can I do that?

(I tried removing the msg.type() === 'error' check from my code, but that didn't help-- Uncaught (in promise) TypeError do not show up as console errors in Playwright, so where are they?)


Solution

  • Based on the comment from @stefan judis, I was able to capture this error by checking for page errors in addition to console errors.

    Here's the final code:

        import { test as base, expect } from '@playwright/test';
        
        export const test = base.extend({
          page: async ({ baseURL, page }, use) => {
            const messages: string[] = [];
            page.on('console', (msg) => {
              // Ignore regular log messages; we are only interested in errors.
              if (msg.type() === 'error') {
                messages.push(`[${msg.type()}] ${msg.text()}`);
              }
            });
            // Uncaught (in promise) TypeError + friends are page errors.
            page.on('pageerror', (error) => {
              messages.push(`[${error.name}] ${error.message}`);
            });
            await use(page);
            expect(messages).toStrictEqual([]);
          },
        });
        
        export default test;