Skip to content

[Feature]: Set process.env.PLAYWRIGHT = 'true' in Playwright processes incl. webServer processes #37377

@karlhorky

Description

@karlhorky

🚀 Feature Request

Projects often branch behavior for Playwright tests (eg. disable reCAPTCHA, stub external 3rd party integrations).

Today this requires manually wiring an env var in multiple places: test workers, UI mode, each webServer command, framework dev server, and build scripts (duplication, brittle, easy to miss an environment):

playwright.config.ts

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

const config: PlaywrightTestConfig = {
  webServer: [
    {
      command: 'pnpm website build && pnpm website start',
      env: {
        PLAYWRIGHT: 'true',
      },
      port: 3000,
      stdout: 'pipe',
    },
    {
      command: 'pnpm api build && pnpm api start',
      env: {
        PLAYWRIGHT: 'true',
      },
      port: 3010,
      stdout: 'pipe',
    },
  ],

package.json

{
  "scripts": {
    "test": "PLAYWRIGHT=true pnpm playwright test --ui"
  }
}

Playwright could set process.env.PLAYWRIGHT = 'true' by default, similar to Prior Art below

Prior Art

Example

In runtime code or build configuration, process.env.PLAYWRIGHT could be used to gate behavior to avoid testing external services or provide mocks:

api/routes/signup.ts

if (!process.env.PLAYWRIGHT) {
  const success = await verifyRecaptcha(form.json.recaptchaToken);

webpack.config.ts

import { fileURLToPath } from 'node:url';
import type { Configuration } from 'webpack';

const config: Configuration = {
  resolve: {
    alias: {
      '@sendgrid/mail':
        process.env.PLAYWRIGHT === 'true'
          ? fileURLToPath(new URL('./test/mocks/sendgrid-stub.ts', import.meta.url))
          : '@sendgrid/mail',

Can also be forwarded + inlined in bundled frontend code via webpack's DefinePlugin or similar:

webpack.config.ts

import { type Configuration, DefinePlugin } from 'webpack';

const config: Configuration = {
  plugins: [
    new DefinePlugin({
      'process.env.PLAYWRIGHT': JSON.stringify(process.env.PLAYWRIGHT),
    }),

Motivation

Described in Feature Request above

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions