From d2f4b9ca53642ac9ccae9a22fd3138698990387b Mon Sep 17 00:00:00 2001 From: Nikolay Vitkov <34244704+Lightning00Blade@users.noreply.github.com> Date: Tue, 5 Dec 2023 13:12:07 +0100 Subject: [PATCH] fix: get port from created server (#11495) --- packages/ng-schematics/package.json | 5 +++-- .../ng-schematics/src/builders/puppeteer/index.ts | 15 +++++++++++++-- .../files/common/e2e/tests/utils.ts.template | 2 +- packages/ng-schematics/tools/sandbox.mjs | 2 +- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/packages/ng-schematics/package.json b/packages/ng-schematics/package.json index 0dde2b8bc3e..1eba0951707 100644 --- a/packages/ng-schematics/package.json +++ b/packages/ng-schematics/package.json @@ -8,7 +8,7 @@ "dev:test": "npm run test --watch", "dev": "npm run build --watch", "sandbox:test": "node tools/sandbox.js --test", - "sandbox": "node tools/sandbox.js", + "sandbox": "node tools/sandbox.mjs", "test": "wireit", "test:smoke": "wireit" }, @@ -24,7 +24,8 @@ ], "output": [ "lib/**", - "test/build/**" + "test/build/**", + "*.tsbuildinfo" ] }, "test": { diff --git a/packages/ng-schematics/src/builders/puppeteer/index.ts b/packages/ng-schematics/src/builders/puppeteer/index.ts index 268e2cddd9c..2a49dae9b27 100644 --- a/packages/ng-schematics/src/builders/puppeteer/index.ts +++ b/packages/ng-schematics/src/builders/puppeteer/index.ts @@ -73,7 +73,11 @@ function updateExecutablePath(command: string, root?: string) { return normalize(path); } -async function executeCommand(context: BuilderContext, command: string[]) { +async function executeCommand( + context: BuilderContext, + command: string[], + env: Record = {} +) { let project: JsonObject; if (context.target) { project = await context.getProjectMetadata(context.target.project); @@ -92,6 +96,10 @@ async function executeCommand(context: BuilderContext, command: string[]) { cwd: path, stdio: 'inherit', shell: true, + env: { + ...process.env, + ...env, + }, }); child.on('error', message => { @@ -166,10 +174,13 @@ async function executeE2ETest( await executeCommand(context, [`tsc`, '-p', 'e2e/tsconfig.json']); server = await startServer(options, context); + const result = await server.result; message('\n Running tests ๐Ÿงช ... \n', context); const testRunnerCommand = getCommandForRunner(options.testRunner); - await executeCommand(context, testRunnerCommand); + await executeCommand(context, testRunnerCommand, { + baseUrl: result['baseUrl'], + }); message('\n ๐Ÿš€ Test ran successfully! ๐Ÿš€ ', context, 'success'); return {success: true}; diff --git a/packages/ng-schematics/src/schematics/ng-add/files/common/e2e/tests/utils.ts.template b/packages/ng-schematics/src/schematics/ng-add/files/common/e2e/tests/utils.ts.template index cd00dc3ab0c..083cce4fe00 100644 --- a/packages/ng-schematics/src/schematics/ng-add/files/common/e2e/tests/utils.ts.template +++ b/packages/ng-schematics/src/schematics/ng-add/files/common/e2e/tests/utils.ts.template @@ -3,7 +3,7 @@ import {before, beforeEach, after, afterEach} from 'node:test'; <% } %> import * as puppeteer from 'puppeteer'; -const baseUrl = '<%= baseUrl %>'; +const baseUrl = process.env['baseUrl'] ?? '<%= baseUrl %>'; let browser: puppeteer.Browser; let page: puppeteer.Page; diff --git a/packages/ng-schematics/tools/sandbox.mjs b/packages/ng-schematics/tools/sandbox.mjs index 89bf3ba665a..e4c45e79623 100644 --- a/packages/ng-schematics/tools/sandbox.mjs +++ b/packages/ng-schematics/tools/sandbox.mjs @@ -199,7 +199,7 @@ async function main() { }); if (isShell) { - await runNgSchematicsSandbox(getOptions()).catch(error => { + await runNgSchematicsSandbox(options).catch(error => { console.log('Something went wrong'); console.error(error); });