microsoft / playwright

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
https://playwright.dev
Apache License 2.0
64.21k stars 3.48k forks source link

[BUG] PW test run button disappears when a conditional exception is thrown in helper code #26609

Open pajdekPL opened 11 months ago

pajdekPL commented 11 months ago

Context:

Code Snippet Here is the repo that you can use to reproduce this bug: https://github.com/pajdekPL/pw-vcs-potential-bug

Describe the bug

The play test button arrow disappears when I throw an exception in one of my scripts: image

after commenting those two lines out:

https://github.com/pajdekPL/pw-vcs-potential-bug/blob/2df52bab0786792ad5c32307c0733529c76f0e3b/tests/example.spec.ts#L6 https://github.com/pajdekPL/pw-vcs-potential-bug/blob/2df52bab0786792ad5c32307c0733529c76f0e3b/tests/example.spec.ts#L7

The run button appears again: image

The test work when executed directly from the console or the test explorer:

image

Add any other details about the problem here.

yury-s commented 11 months ago

If you open 'Problems' tab you should see the error:

image

Make sure the test file is currently open in the editor.

This is just a workaround and we could do a better job here surfacing the error in a more prominent way.

jaktestowac commented 11 months ago

@yury-s This bug is more severe because tests working from command line but not from extension:

Tests will not work with dotenv initialized in global setup together with throwing exception like:

global.setup.ts

import dotenv from "dotenv";

async function globalSetup(): Promise<void> {
  dotenv.config({ override: true });
}

export default globalSetup;

playwright.config.ts

import { defineConfig, devices } from '@playwright/test';

export default defineConfig({
  globalSetup: 'global.setup.ts',
  testDir: './tests',
});

example.spec.ts

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

function raiseErrorEnvNotSet(env: string): never {
  throw new Error(`Please set ${env} env variable`);
}

const envVars = {
  test: process.env.TEST ?? raiseErrorEnvNotSet("TEST"),
};

test("environmental variable TEST", async () => {
  console.log("Env var TEST: ", process.env.TEST);
  const test = envVars.test;
});

.env

TEST='hello form dotenv'

This configuration is valid, but error is presented in VSC Playwright extension obraz

Setting dotenv directly in config like below do not produces error:

playwright.config.ts

import { defineConfig, devices } from '@playwright/test';

require('dotenv').config();

export default defineConfig({
  globalSetup: 'global.setup.ts',
  testDir: './tests',
});