From b920ba096747160b2767310c444cfdc1bc42aeb3 Mon Sep 17 00:00:00 2001 From: Emily Xiong Date: Wed, 9 Aug 2023 18:10:22 -0400 Subject: [PATCH] fix(testing): make the default react playwright test to pass --- .../src/executors/playwright/playwright.ts | 15 +++++++++++++-- .../src/generators/configuration/configuration.ts | 2 +- .../files/__directory__/example.spec.ts.template | 2 +- .../files/playwright.config.ts.template | 2 +- .../src/generators/application/lib/add-e2e.ts | 1 + 5 files changed, 17 insertions(+), 5 deletions(-) diff --git a/packages/playwright/src/executors/playwright/playwright.ts b/packages/playwright/src/executors/playwright/playwright.ts index 7dc18423d03517..2b6597e158b937 100644 --- a/packages/playwright/src/executors/playwright/playwright.ts +++ b/packages/playwright/src/executors/playwright/playwright.ts @@ -81,10 +81,21 @@ export async function playwrightExecutor( const args = createArgs(options); const p = runPlaywright(args, context.root); + p.stdout.on('data', (message) => { + process.stdout.write(message); + }); + p.stderr.on('data', (message) => { + process.stderr.write(message); + }); return new Promise<{ success: boolean }>((resolve) => { p.on('close', (code) => { - resolve({ success: code === 0 }); + if (code !== 0) { + console.error(`Playwright exited with code ${code}`); + resolve({ success: false }); + } else { + resolve({ success: true }); + } }); }); } @@ -117,7 +128,7 @@ function runPlaywright(args: string[], cwd: string) { const cli = require.resolve('@playwright/test/cli'); return fork(cli, ['test', ...args], { - stdio: 'inherit', + stdio: ['pipe', 'pipe', 'pipe', 'ipc'], cwd, }); } catch (e) { diff --git a/packages/playwright/src/generators/configuration/configuration.ts b/packages/playwright/src/generators/configuration/configuration.ts index 139b7656e7977c..caa18204765f0d 100644 --- a/packages/playwright/src/generators/configuration/configuration.ts +++ b/packages/playwright/src/generators/configuration/configuration.ts @@ -90,7 +90,7 @@ Rename or remove the existing e2e target.`); projectConfig.targets ??= {}; projectConfig.targets.e2e = { executor: '@nx/playwright:playwright', - outputs: [`dist/.playwright/${projectConfig.root}`], + outputs: [`{workspaceRoot}/dist/.playwright/${projectConfig.root}`], options: { config: `${projectConfig.root}/playwright.config.${ options.js ? 'js' : 'ts' diff --git a/packages/playwright/src/generators/configuration/files/__directory__/example.spec.ts.template b/packages/playwright/src/generators/configuration/files/__directory__/example.spec.ts.template index f6f9f6eced511e..543df40339448c 100644 --- a/packages/playwright/src/generators/configuration/files/__directory__/example.spec.ts.template +++ b/packages/playwright/src/generators/configuration/files/__directory__/example.spec.ts.template @@ -4,5 +4,5 @@ test('has title', async ({ page }) => { await page.goto('/'); // Expect a title "to contain" a substring. - await expect(page).toHaveTitle(/Welcome/); + await expect(page).toHaveTitle(/<%= project %>/); }); diff --git a/packages/playwright/src/generators/configuration/files/playwright.config.ts.template b/packages/playwright/src/generators/configuration/files/playwright.config.ts.template index 8c7925f07e8195..a7abebe8c2ea56 100644 --- a/packages/playwright/src/generators/configuration/files/playwright.config.ts.template +++ b/packages/playwright/src/generators/configuration/files/playwright.config.ts.template @@ -16,7 +16,7 @@ const baseURL = process.env['BASE_URL'] || '<% if(webServerAddress) {%><%= webSe * See https://playwright.dev/docs/test-configuration. */ export default defineConfig({ - ...nxE2EPreset(__filename, { testDir: './<>' }), + ...nxE2EPreset(__filename, { testDir: './src' }), /* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */ use: { baseURL, diff --git a/packages/react/src/generators/application/lib/add-e2e.ts b/packages/react/src/generators/application/lib/add-e2e.ts index bd551cb9d29e2a..fe477e16906942 100644 --- a/packages/react/src/generators/application/lib/add-e2e.ts +++ b/packages/react/src/generators/application/lib/add-e2e.ts @@ -55,6 +55,7 @@ export async function addE2e( webServerCommand: `${getPackageManagerCommand().exec} nx serve ${ options.name }`, + webServerAddress: 'localhost:4200', }); case 'none': default: