Rayrun
← Back to Discord Forum

How to test match different login.setup.ts files per project using grep

Currently my playwright serves as a monorepo and I have multiple projects under /tests folder, each has different setup.ts file, when I try to run one project every other /setup file is running, I've tried multiple options but it either breaks one of the projects or all of them, is there a way to tackle that issue correctly? Meaning having multiple projects like so, every npx playwright test --grep xyz, runs both of the setup files

projects: [ { name: 'setup-xyz', testDir:'./tests/zyx-platform', testMatch: /..setup.ts/ }, { name: 'setup-abcd', testDir:'./tests/abcd-platform', testMatch: /..setup.ts/ },
{ name: 'zyx-tests', testMatch: '/*.spec.ts', testDir:'./tests/xyz-platform', dependencies: ['setup-abc'], use: { storageState: STORAGE_STATE_XYZ, baseURL: 'xyz.com' }, { name: 'abcd-tests', testMatch: '/*.spec.ts', testDir:'./tests/abcd-platform', dependencies: ['setup-abcd'], use: { storageState: STORAGE_STATE_ABCD, baseURL: 'abcd.com' }, },

This thread is trying to answer question "How to correctly test match different login.setup.ts files per project without running every other /setup file?"

3 replies

test match for above is testMatch: /.*\.setup\.ts/,

the issue is in fact with --grep and not setup i think, since --project=abdc-tests in cli runs fine, but when trying to run with @abcd setup runs for everything

The testMatch pattern you provided (/.*.setup.ts/) is a regular expression that will match any file ending with .setup.ts. If your setup files across different projects follow this pattern, and if there’s no additional configuration to distinguish between them, then it’s possible that Playwright is interpreting the --grep flag as a command to run all matching setup files.

You may want to take a closer look at your file organization, naming conventions, and the specific --grep pattern you are using to ensure that they are uniquely identifying the tests you want to run for each project.

Answer

Sure, you can easily manage the execution of specific setup.ts files for each project in your Playwright monorepo. This is done by leveraging the project dependencies feature in Playwright.

In your playwright.config.ts file, you can define multiple projects with the defineConfig() function. Each project represents a different test suite or scenario. You can specify the name of each project, along with other properties such as testMatch, which defines the pattern for matching test files.

To link projects, use the dependencies property within a specific project definition. This property allows you to specify an array of projects that need to run before the tests in another project can be executed.

Here's an example:

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

export default defineConfig({
  projects: [
    {
      name: 'setup',
      testMatch: /global.setup\.ts/,
    },
    {
      name: 'chromium',
      use: {
        // ...devices['Desktop Chrome']
      },
      dependencies: ['setup'],
    },
    {
      name: 'firefox',
      use: {
        // ...devices['Desktop Firefox']
      },
      dependencies: ['setup'],
    },
  ],
});

In this configuration, when running tests for either the 'chromium' or 'firefox' projects, Playwright will automatically execute any setup actions defined in the 'setup' project before running their respective tests.

By organizing your codebase and configuring these dependencies correctly, only relevant setup files will be executed for each project. This ensures that the necessary setup actions are performed before running the tests.

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].