Search code examples
playwrightplaywright-test

Playwright ONLY run tests from a specified folder and ignore all other tests


Given the following directory structure...

- /src/__mocks__
  - X.spec.tsx <-- mock test
  - Y.spec.tsx <-- mock test
  - Z.spec.tsx <-- mock test
- /src/e2e_tests <-------------- only run the tests inside this directory
  - X.spec.tsx <-- end-to-end test
  - Y.spec.tsx <-- end-to-end test
  - Z.spec.tsx <-- end-to-end test
- /src/components
  - A.tsx
  - A.spec.tsx <-- unit test
  - B.tsx
  - B.spec.tsx <-- unit test
  - C.tsx
  - C.spec.tsx <-- unit test
- /src/views
  - X.tsx
  - X.spec.tsx <-- unit test
  - Y.tsx
  - Y.spec.tsx <-- unit test
  - Z.tsx
  - Z.spec.tsx <-- unit test
- /src
  - App.tsx
  - index.tsx
- jest.config.ts
- playwright.config.ts
- package.json
- tsconfig.json
- tsconfig.spec.json
- ect...

I've tried the following: How do you specify a test folder/path with Playwright?, however, this will also attempt to run all the tests outside the folder.

I've tried reading through the docs here: https://playwright.dev/docs/test-configuration and have tried the following combination:

  • testDir
  • testIgnore
  • testMatch

but can't get only e2e_tests directory tests to run while ignoring all other tests.


Solution

  • One way to do this is to create a project in your existing playwright config file and just add the testDir there, something like:

    projects: [
      {
        name: 'chromium',
        use: {...devices['Desktop Chrome']},
      },
      {
        name: 'firefox',
        use: {...devices['Desktop Firefox']},
      },
      {
        name: 'webkit',
        use: {...devices['Desktop Safari']},
      },
      {
        name: 'e2e_tests',
        testDir: './src/e2e_tests',
        testMatch: /.*.spec.tsx/,
      },
    ]
    

    And then run your tests like:

    npx playwright test --project=e2e_tests