Skip to content

Commit

Permalink
fix(testing): make the default react playwright test to pass
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongemi committed Aug 11, 2023
1 parent dcbba68 commit c54e6d0
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 9 deletions.
6 changes: 4 additions & 2 deletions e2e/playwright/src/playwright.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ describe('Playwright E2E Test runner', () => {
it(
'should test and lint example app',
() => {
runCLI(`g @nx/js:lib demo-e2e --unitTestRunner none --bundler none`);
runCLI(
`g @nx/web:app demo-e2e --unitTestRunner=none --bundler=vite --e2eTestRunner=none --style=css --no-interactive`
);
runCLI(`g @nx/playwright:configuration --project demo-e2e`);
ensurePlaywrightBrowsersInstallation();

Expand All @@ -34,7 +36,7 @@ describe('Playwright E2E Test runner', () => {
'should test and lint example app with js',
() => {
runCLI(
`g @nx/js:lib demo-js-e2e --unitTestRunner none --bundler none --js`
`g @nx/web:app demo-js-e2e --unitTestRunner=none --bundler=vite --e2eTestRunner=none --style=css --no-interactive`
);
runCLI(`g @nx/playwright:configuration --project demo-js-e2e --js`);
ensurePlaywrightBrowsersInstallation();
Expand Down
1 change: 0 additions & 1 deletion e2e/web/src/web.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
ensurePlaywrightBrowsersInstallation,
isNotWindows,
killPorts,
listFiles,
newProject,
readFile,
rmDist,
Expand Down
8 changes: 7 additions & 1 deletion packages/playwright/src/executors/playwright/playwright.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ 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) => {
Expand Down Expand Up @@ -117,7 +123,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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import { test, expect } from '@playwright/test';
test('has title', async ({ page }) => {
await page.goto('/');

// Expect a title "to contain" a substring.
await expect(page).toHaveTitle(/Welcome/);
// Expect h1 to contain a substring.
expect(await page.locator('h1').innerText()).toContain('Welcome');
});
Original file line number Diff line number Diff line change
Expand Up @@ -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: './<%= directory %>' }),
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
baseURL,
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/generators/application/lib/add-e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export async function addE2e(
webServerCommand: `${getPackageManagerCommand().exec} nx serve ${
options.name
}`,
webServerAddress: 'http://localhost:4200',
});
case 'none':
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ describe('app', () => {
"config": "apps/cool-app-e2e/playwright.config.ts",
},
"outputs": [
"dist/.playwright/apps/cool-app-e2e",
"{workspaceRoot}/dist/.playwright/apps/cool-app-e2e",
],
}
`);
Expand Down
5 changes: 5 additions & 0 deletions packages/web/src/generators/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
formatFiles,
generateFiles,
GeneratorCallback,
getPackageManagerCommand,
getWorkspaceLayout,
joinPathFragments,
names,
Expand Down Expand Up @@ -288,6 +289,10 @@ export async function applicationGenerator(host: Tree, schema: Schema) {
js: false,
linter: options.linter,
setParserOptionsProject: options.setParserOptionsProject,
webServerCommand: `${getPackageManagerCommand().exec} nx serve ${
options.name
}`,
webServerAddress: 'http://localhost:4200',
});
tasks.push(playwrightTask);
}
Expand Down

0 comments on commit c54e6d0

Please sign in to comment.