Skip to content

Commit 6b8ba97

Browse files
authored
fix(testing): infer correct outputs when absolute paths are provided in playwright config (#29549)
Update `@nx/playwright/plugin` to properly handle absolute paths set in the playwright config to infer outputs correctly. <!-- Please make sure you have read the submission guidelines before posting an PR --> <!-- https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr --> <!-- Please make sure that your commit message follows our format --> <!-- Example: `fix(nx): must begin with lowercase` --> <!-- If this is a particularly complex change or feature addition, you can request a dedicated Nx release for this pull request branch. Mention someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they will confirm if the PR warrants its own release for testing purposes, and generate it for you if appropriate. --> ## Current Behavior <!-- This is the behavior we have today --> ## Expected Behavior <!-- This is the behavior we should expect with the changes in this PR --> ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #
1 parent 160800e commit 6b8ba97

File tree

2 files changed

+50
-13
lines changed

2 files changed

+50
-13
lines changed

packages/playwright/src/plugins/plugin.spec.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { CreateNodesContext } from '@nx/devkit';
22
import { TempFs } from '@nx/devkit/internal-testing-utils';
3-
4-
import { createNodesV2 } from './plugin';
53
import { PlaywrightTestConfig } from '@playwright/test';
4+
import { join } from 'node:path';
5+
import { createNodesV2 } from './plugin';
66

77
describe('@nx/playwright/plugin', () => {
88
let createNodesFunction = createNodesV2[1];
@@ -139,7 +139,12 @@ describe('@nx/playwright/plugin', () => {
139139
await mockPlaywrightConfig(tempFs, {
140140
reporter: [
141141
['list'],
142-
['json', { outputFile: 'test-results/report.json' }],
142+
[
143+
'json',
144+
// test absolute path
145+
{ outputFile: join(tempFs.tempDir, 'test-results/report.json') },
146+
],
147+
// test relative path
143148
['html', { outputFolder: 'test-results/html' }],
144149
],
145150
});

packages/playwright/src/plugins/plugin.ts

+42-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { existsSync, readdirSync } from 'node:fs';
2-
import { dirname, join, parse, relative } from 'node:path';
2+
import { dirname, join, parse, relative, resolve } from 'node:path';
33

44
import {
55
CreateNodes,
@@ -188,7 +188,12 @@ async function buildPlaywrightTargets(
188188
: ['default', '^default']),
189189
{ externalDependencies: ['@playwright/test'] },
190190
],
191-
outputs: getTargetOutputs(testOutput, reporterOutputs, projectRoot),
191+
outputs: getTargetOutputs(
192+
testOutput,
193+
reporterOutputs,
194+
context.workspaceRoot,
195+
projectRoot
196+
),
192197
};
193198

194199
if (options.ciTargetName) {
@@ -201,7 +206,12 @@ async function buildPlaywrightTargets(
201206
: ['default', '^default']),
202207
{ externalDependencies: ['@playwright/test'] },
203208
],
204-
outputs: getTargetOutputs(testOutput, reporterOutputs, projectRoot),
209+
outputs: getTargetOutputs(
210+
testOutput,
211+
reporterOutputs,
212+
context.workspaceRoot,
213+
projectRoot
214+
),
205215
};
206216

207217
const groupName = 'E2E (CI)';
@@ -236,6 +246,7 @@ async function buildPlaywrightTargets(
236246
outputs: getTargetOutputs(
237247
testOutput,
238248
reporterOutputs,
249+
context.workspaceRoot,
239250
projectRoot,
240251
outputSubfolder
241252
),
@@ -394,27 +405,48 @@ function getReporterOutputs(
394405
function getTargetOutputs(
395406
testOutput: string,
396407
reporterOutputs: Array<[string, string]>,
408+
workspaceRoot: string,
397409
projectRoot: string,
398410
scope?: string
399411
): string[] {
400412
const outputs = new Set<string>();
401413
outputs.add(
402-
normalizeOutput(projectRoot, scope ? join(testOutput, scope) : testOutput)
414+
normalizeOutput(
415+
scope ? join(testOutput, scope) : testOutput,
416+
workspaceRoot,
417+
projectRoot
418+
)
403419
);
404420
for (const [, output] of reporterOutputs) {
405421
outputs.add(
406-
normalizeOutput(projectRoot, scope ? join(output, scope) : output)
422+
normalizeOutput(
423+
scope ? join(output, scope) : output,
424+
workspaceRoot,
425+
projectRoot
426+
)
407427
);
408428
}
409429
return Array.from(outputs);
410430
}
411431

412-
function normalizeOutput(projectRoot: string, path: string): string {
413-
if (path.startsWith('..')) {
414-
return join('{workspaceRoot}', join(projectRoot, path));
415-
} else {
416-
return join('{projectRoot}', path);
432+
function normalizeOutput(
433+
path: string,
434+
workspaceRoot: string,
435+
projectRoot: string
436+
): string {
437+
const fullProjectRoot = resolve(workspaceRoot, projectRoot);
438+
const fullPath = resolve(fullProjectRoot, path);
439+
const pathRelativeToProjectRoot = normalizePath(
440+
relative(fullProjectRoot, fullPath)
441+
);
442+
if (pathRelativeToProjectRoot.startsWith('..')) {
443+
return joinPathFragments(
444+
'{workspaceRoot}',
445+
relative(workspaceRoot, fullPath)
446+
);
417447
}
448+
449+
return joinPathFragments('{projectRoot}', pathRelativeToProjectRoot);
418450
}
419451

420452
function getOutputEnvVars(

0 commit comments

Comments
 (0)