Skip to content

Commit 107a753

Browse files
authored
fix(testing): make the default react playwright test to pass (#18559)
1 parent c785871 commit 107a753

File tree

10 files changed

+51
-12
lines changed

10 files changed

+51
-12
lines changed

e2e/next/src/next-appdir.test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ describe('Next.js App Router', () => {
2323
const appName = uniq('app');
2424
const jsLib = uniq('tslib');
2525

26-
runCLI(`generate @nx/next:app ${appName} --e2eTestRunner=playwright`);
26+
runCLI(
27+
`generate @nx/next:app ${appName} --e2eTestRunner=playwright --appDir=true`
28+
);
2729
runCLI(`generate @nx/js:lib ${jsLib} --no-interactive`);
2830

2931
updateFile(
@@ -40,6 +42,20 @@ describe('Next.js App Router', () => {
4042
`
4143
);
4244

45+
updateFile(
46+
`apps/${appName}-e2e/src/example.spec.ts`,
47+
`
48+
import { test, expect } from '@playwright/test';
49+
50+
test('has ${jsLib}', async ({ page }) => {
51+
await page.goto('/');
52+
53+
// Expect h1 to contain a substring.
54+
expect(await page.locator('p').innerText()).toContain('${jsLib}');
55+
});
56+
`
57+
);
58+
4359
await checkApp(appName, {
4460
checkUnitTest: false,
4561
checkLint: true,

e2e/playwright/src/playwright.test.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@ import {
44
uniq,
55
runCLI,
66
ensurePlaywrightBrowsersInstallation,
7+
getPackageManagerCommand,
8+
getSelectedPackageManager,
79
} from '@nx/e2e/utils';
810

911
const TEN_MINS_MS = 600_000;
1012
describe('Playwright E2E Test runner', () => {
13+
const pmc = getPackageManagerCommand({
14+
packageManager: getSelectedPackageManager(),
15+
});
16+
1117
beforeAll(() => {
1218
newProject({ name: uniq('playwright') });
1319
});
@@ -17,8 +23,12 @@ describe('Playwright E2E Test runner', () => {
1723
it(
1824
'should test and lint example app',
1925
() => {
20-
runCLI(`g @nx/js:lib demo-e2e --unitTestRunner none --bundler none`);
21-
runCLI(`g @nx/playwright:configuration --project demo-e2e`);
26+
runCLI(
27+
`g @nx/web:app demo-e2e --unitTestRunner=none --bundler=vite --e2eTestRunner=none --style=css --no-interactive`
28+
);
29+
runCLI(
30+
`g @nx/playwright:configuration --project demo-e2e --webServerCommand="${pmc.runNx} serve demo-e2e" --webServerAddress="http://localhost:4200"`
31+
);
2232
ensurePlaywrightBrowsersInstallation();
2333

2434
const e2eResults = runCLI(`e2e demo-e2e`);
@@ -34,9 +44,11 @@ describe('Playwright E2E Test runner', () => {
3444
'should test and lint example app with js',
3545
() => {
3646
runCLI(
37-
`g @nx/js:lib demo-js-e2e --unitTestRunner none --bundler none --js`
47+
`g @nx/web:app demo-js-e2e --unitTestRunner=none --bundler=vite --e2eTestRunner=none --style=css --no-interactive`
48+
);
49+
runCLI(
50+
`g @nx/playwright:configuration --project demo-js-e2e --js --webServerCommand="${pmc.runNx} serve demo-e2e" --webServerAddress="http://localhost:4200"`
3851
);
39-
runCLI(`g @nx/playwright:configuration --project demo-js-e2e --js`);
4052
ensurePlaywrightBrowsersInstallation();
4153

4254
const e2eResults = runCLI(`e2e demo-js-e2e`);

e2e/web/src/web.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
ensurePlaywrightBrowsersInstallation,
77
isNotWindows,
88
killPorts,
9-
listFiles,
109
newProject,
1110
readFile,
1211
rmDist,

packages/playwright/src/executors/playwright/playwright.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ export async function playwrightExecutor(
8181

8282
const args = createArgs(options);
8383
const p = runPlaywright(args, context.root);
84+
p.stdout.on('data', (message) => {
85+
process.stdout.write(message);
86+
});
87+
p.stderr.on('data', (message) => {
88+
process.stderr.write(message);
89+
});
8490

8591
return new Promise<{ success: boolean }>((resolve) => {
8692
p.on('close', (code) => {
@@ -117,7 +123,7 @@ function runPlaywright(args: string[], cwd: string) {
117123
const cli = require.resolve('@playwright/test/cli');
118124

119125
return fork(cli, ['test', ...args], {
120-
stdio: 'inherit',
126+
stdio: ['pipe', 'pipe', 'pipe', 'ipc'],
121127
cwd,
122128
});
123129
} catch (e) {

packages/playwright/src/generators/configuration/configuration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ Rename or remove the existing e2e target.`);
9090
projectConfig.targets ??= {};
9191
projectConfig.targets.e2e = {
9292
executor: '@nx/playwright:playwright',
93-
outputs: [`dist/.playwright/${projectConfig.root}`],
93+
outputs: [`{workspaceRoot}/dist/.playwright/${projectConfig.root}`],
9494
options: {
9595
config: `${projectConfig.root}/playwright.config.${
9696
options.js ? 'js' : 'ts'

packages/playwright/src/generators/configuration/files/__directory__/example.spec.ts.template

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ import { test, expect } from '@playwright/test';
33
test('has title', async ({ page }) => {
44
await page.goto('/');
55

6-
// Expect a title "to contain" a substring.
7-
await expect(page).toHaveTitle(/Welcome/);
6+
// Expect h1 to contain a substring.
7+
expect(await page.locator('h1').innerText()).toContain('Welcome');
88
});

packages/playwright/src/generators/configuration/files/playwright.config.ts.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const baseURL = process.env['BASE_URL'] || '<% if(webServerAddress) {%><%= webSe
1616
* See https://playwright.dev/docs/test-configuration.
1717
*/
1818
export default defineConfig({
19-
...nxE2EPreset(__filename, { testDir: './<>' }),
19+
...nxE2EPreset(__filename, { testDir: './<%= directory %>' }),
2020
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
2121
use: {
2222
baseURL,

packages/react/src/generators/application/lib/add-e2e.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export async function addE2e(
5555
webServerCommand: `${getPackageManagerCommand().exec} nx serve ${
5656
options.name
5757
}`,
58+
webServerAddress: 'http://localhost:4200',
5859
});
5960
case 'none':
6061
default:

packages/web/src/generators/application/application.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ describe('app', () => {
156156
"config": "apps/cool-app-e2e/playwright.config.ts",
157157
},
158158
"outputs": [
159-
"dist/.playwright/apps/cool-app-e2e",
159+
"{workspaceRoot}/dist/.playwright/apps/cool-app-e2e",
160160
],
161161
}
162162
`);

packages/web/src/generators/application/application.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
formatFiles,
99
generateFiles,
1010
GeneratorCallback,
11+
getPackageManagerCommand,
1112
getWorkspaceLayout,
1213
joinPathFragments,
1314
names,
@@ -288,6 +289,10 @@ export async function applicationGenerator(host: Tree, schema: Schema) {
288289
js: false,
289290
linter: options.linter,
290291
setParserOptionsProject: options.setParserOptionsProject,
292+
webServerCommand: `${getPackageManagerCommand().exec} nx serve ${
293+
options.name
294+
}`,
295+
webServerAddress: 'http://localhost:4200',
291296
});
292297
tasks.push(playwrightTask);
293298
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy