Skip to content

[Bug]: JSON reporter emits a separate spec per project when testDir is not the current directory #43266

Closed

Description

Version

1.64.0

Steps to reproduce

playwright.config.ts

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

export default defineConfig({
  testDir: './tests',
  projects: [{ name: 'chromium' }, { name: 'firefox' }],
});

tests/example.spec.ts

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

test('has title', async () => {});

Run from the project root:

npx playwright test --reporter=json > report.json
node -e "console.log(JSON.stringify(require('./report.json').suites[0].specs.map(s => [s.title, s.tests.map(t => t.projectName)])))"

Expected behavior

A single spec with one test per project. This is what the same config produces when it is run from inside tests/:

cd tests && npx playwright test -c .. --reporter=json
[["has title",["chromium","firefox"]]]

Actual behavior

When run from the project root, the spec is duplicated once per project:

[["has title",["chromium"]],["has title",["firefox"]]]

Tests inside test.describe are duplicated the same way, so the shape of the report depends on the directory the command is run from.

Additional context

_mergeTestsFromSuite in packages/playwright/src/reporters/json.ts matches specs with s.file === toPosixPath(path.relative(this.config.rootDir, spec.file)). spec.file is already relative to rootDir (it comes from _relativeLocation), so path.relative resolves it against process.cwd() and the comparison only succeeds when cwd is rootDir. Suites in the same function are matched with _locationMatches, which would work for specs too.

This seems to date back to 1.27, when the comparison switched from test.location.file (absolute) to spec.file (relative).

I'd like to send a PR with this fix and a regression test in tests/playwright-test/reporter-json.spec.ts.

Environment

System:
  OS: Linux 6.18 Ubuntu 24.04.5 LTS 24.04.5 LTS (Noble Numbat)
  CPU: (4) x64 Intel(R) Xeon(R) Processor @ 2.80GHz
  Memory: 13.56 GB / 15.72 GB
Binaries:
  Node: 22.22.0
  npm: 10.9.4
npmPackages:
  @playwright/test: ^1.64.0 => 1.64.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions