Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chrore(ng-schematics): Update
ng e2e
to custom builder (#9300)
**What kind of change does this PR introduce?** This replaces the default `ng e2e` with our custom builder. In the feature it seem possible to remove the necessity of the user running the server separately and run it from the builder - that will improve the easy of use and CI of this schematic. **Did you add tests for your changes?** **If relevant, did you update the documentation?** Yes - Updated `@puppeteer/ng-schematics` README.md **Summary** We want to not see the default `ng e2e` and we want to make it easier for the user to run commands. Angular Developer are likely to also use its' CLI. **Does this PR introduce a breaking change?** Yes. Users need to delete the default and initialize the schematics again. **Other information**
- Loading branch information
1 parent
0107ad8
commit 4613f03
Showing
14 changed files
with
340 additions
and
56 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"$schema": "../../../../node_modules/@angular-devkit/architect/src/builders-schema.json", | ||
"builders": { | ||
"puppeteer": { | ||
"implementation": "./puppeteer", | ||
"schema": "./puppeteer/schema.json", | ||
"description": "Run e2e test with Puppeteer" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { | ||
createBuilder, | ||
BuilderContext, | ||
BuilderOutput, | ||
} from '@angular-devkit/architect'; | ||
import {spawn} from 'child_process'; | ||
|
||
import {PuppeteerBuilderOptions} from './types.js'; | ||
|
||
function getError(executable: string, args: string[]) { | ||
return ( | ||
`Puppeteer E2E tests failed!` + | ||
'\n' + | ||
`Error running '${executable}' with arguments '${args.join(' ')}'.` + | ||
`\n` + | ||
'Please look at the output above to determine the issue!' | ||
); | ||
} | ||
|
||
function getExecutable(command: string[]) { | ||
const executable = command.shift()!; | ||
const error = getError(executable, command); | ||
|
||
if (executable === 'node') { | ||
return { | ||
executable: executable, | ||
args: command, | ||
error, | ||
}; | ||
} | ||
|
||
return { | ||
executable: `./node_modules/.bin/${executable}`, | ||
args: command, | ||
error, | ||
}; | ||
} | ||
|
||
async function executeCommand(context: BuilderContext, command: string[]) { | ||
await new Promise((resolve, reject) => { | ||
const {executable, args, error} = getExecutable(command); | ||
|
||
const child = spawn(executable, args, { | ||
cwd: context.workspaceRoot, | ||
stdio: 'inherit', | ||
}); | ||
|
||
child.on('error', message => { | ||
console.log(message); | ||
reject(error); | ||
}); | ||
|
||
child.on('exit', code => { | ||
if (code === 0) { | ||
resolve(true); | ||
} else { | ||
reject(error); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
async function executeE2ETest( | ||
options: PuppeteerBuilderOptions, | ||
context: BuilderContext | ||
): Promise<BuilderOutput> { | ||
context.logger.debug('Running commands for E2E test.'); | ||
try { | ||
for (const command of options.commands) { | ||
await executeCommand(context, command); | ||
} | ||
|
||
return {success: true}; | ||
} catch (error) { | ||
if (error instanceof Error) { | ||
return {success: false, error: error.message}; | ||
} | ||
return {success: false, error: error as any}; | ||
} | ||
} | ||
|
||
export default createBuilder<PuppeteerBuilderOptions>(executeE2ETest) as any; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"title": "Puppeteer", | ||
"description": "Options for Puppeteer Angular Schematics", | ||
"type": "object", | ||
"properties": { | ||
"commands": { | ||
"type": "array", | ||
"items": { | ||
"type": "array", | ||
"item": { | ||
"type": "string" | ||
} | ||
}, | ||
"description": "Commands to execute in the repo. Commands prefixed with `./node_modules/bin` (Exception: 'node')." | ||
} | ||
}, | ||
"additionalProperties": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* Copyright 2022 Google Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import {JsonObject} from '@angular-devkit/core'; | ||
|
||
type Command = [string, ...string[]]; | ||
|
||
export interface PuppeteerBuilderOptions extends JsonObject { | ||
commands: Command[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
packages/ng-schematics/src/schematics/ng-add/files/node/e2e/.gitignore.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# Compiled e2e tests output | ||
# Compiled e2e tests output Node auto resolves files in folders named 'test' | ||
|
||
/out-tsc | ||
test/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.