playwright-vscode icon indicating copy to clipboard operation
playwright-vscode copied to clipboard

[Bug] Improve handling of Playwright config throwing errors

Open weyert opened this issue 2 years ago • 4 comments

If you have a Playwright configuration that does some sanity checks, such as throwing an error when an environment variable is missing then the extension under the hood throws an error similar as listed below. I think it woud be nice to have a dedicated Playwright item in the Output-panel so you can easily spot issues when executing the tests.

Maybe it could show such errors in a showMessage-dialog?

[2022-03-18 11:30:32.486] [renderer2] [error] [Extension Host] SyntaxError: Unexpected token i in JSON at position 0
    at JSON.parse (<anonymous>)
    at qe.listFiles (/Users/weyert/.vscode/extensions/ms-playwright.playwright-0.2.2/out/extension.js:14:3481)
[2022-03-18 11:30:34.407] [renderer2] [error] ENOENT: no such file or directory, stat 'node_modules': Error: ENOENT: no such file or directory, stat 'node_modules'

Reproducible, by generating a new Playwright example project, and then update the playwright.config.ts by adding the following config entry:

globalSetup: path.resolve(process.cwd(), 'setupPlaywright'),

and then create a new setupPlaywright.ts-file which does some sanity checks whether some environment variables are available, to allow to fail fast:

import { PlaywrightTestConfig } from "@playwright/test";

/**
 * Returns the value of the environment variable by its name
 * @param name the name of the environment value
 * @returns string | number | boolean
 *
 * TODO: Drop the CYPRESS prefix now that Cypress is banned
 */
export function getConfig(name: string) {
  if (!name) {
    throw new Error(`Missing configuration with the name ${name}`);
  }

  const actualName = `CYPRESS_${name}`;

  // eslint-disable-next-line no-prototype-builtins
  if (!process.env.hasOwnProperty(actualName)) {
    // console.warn(process.env)
    console.log(`Missing configuration with the name ${name} -> ${actualName}`)
    throw new Error(
      `Missing configuration with the name ${name} -> ${actualName}`
    );
  }

  return process.env[actualName] as string;
}

/*
 * Setup the global setup test environment
 */
async function globalSetup(_config: PlaywrightTestConfig) {
     // Check if mandatory environment variables exists
     const baseUrl = getConfig('BASE_URL')
    console.log(`Under test using base url: ${baseUrl}`)
}

weyert avatar Mar 18 '22 12:03 weyert