Skip to content

Commit

Permalink
feat(testing): add options for frameworks in playwright configuration…
Browse files Browse the repository at this point in the history
… generator
  • Loading branch information
barbados-clemens committed Jul 19, 2023
1 parent 8d160b2 commit f753e6f
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 6 deletions.
4 changes: 2 additions & 2 deletions packages/playwright/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"Playwright",
"CLI"
],
"main": "./index",
"typings": "./index.d.ts",
"main": "./src/index",
"typings": "./src/index.d.ts",
"author": "Victor Savkin",
"license": "MIT",
"bugs": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export async function configurationGenerator(
generateFiles(tree, path.join(__dirname, 'files'), projectConfig.root, {
offsetFromRoot: offsetFromRoot(projectConfig.root),
projectRoot: projectConfig.root,
webServerCommand: options.webServerCommand ?? null,
webServerAddress: options.webServerAddress ?? null,
...options,
});

Expand Down Expand Up @@ -67,8 +69,10 @@ function addE2eTarget(tree: Tree, options: ConfigurationGeneratorSchema) {
throw new Error(`Project ${options.project} already has an e2e target.
Rename or remove the existing e2e target.`);
}
projectConfig.targets ??= {};
projectConfig.targets.e2e = {
executor: '@nx/playwright:playwright',
outputs: [`dist/playwright/${projectConfig.root}`],
options: {},
};
updateProjectConfiguration(tree, options.project, projectConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { defineConfig, devices } from '@playwright/test';
*/
export default defineConfig({
testDir: './<%= directory %>',
/* When updating this path, make sure to update the outputs in the project.json */
outputDir: '<%= offsetFromRoot %>dist/playwright/<%= projectRoot %>/test-output',
/* Run tests in files in parallel */
fullyParallel: true,
/* Fail the build on CI if you accidentally left test.only in the source code. */
Expand All @@ -25,7 +27,7 @@ export default defineConfig({
'html',
{
outputFolder:
'<%= offsetFromRoot %>/dist/playwright/<%= projectRoot %>/playwright-report',
'<%= offsetFromRoot %>dist/playwright/<%= projectRoot %>/playwright-report',
},
],
],
Expand Down Expand Up @@ -76,10 +78,14 @@ export default defineConfig({
// },
],

/* Run your local dev server before starting the tests */
// webServer: {
/* Run your local dev server before starting the tests */<% if(webServerCommand && webServerAddress) {%>
webServer: {
command: '<%= webServerCommand %>',
url: '<%= webServerAddress %>',
reuseExistingServer: !process.env.CI,
},<% } else {%>// webServer: {
// command: 'npm run start',
// url: 'http://127.0.0.1:3000',
// reuseExistingServer: !process.env.CI,
// },
// },<% } %>
});
10 changes: 10 additions & 0 deletions packages/playwright/src/generators/configuration/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,14 @@ export interface ConfigurationGeneratorSchema {
js: boolean; // default is false
skipFormat: boolean;
skipPackageJson: boolean;
/**
* command to give playwright to run the web server
* @example: "npx nx serve my-fe-app"
**/
webServerCommand?: string;
/**
* address
* @example: "http://localhost:4200"
**/
webServerAddress?: string;
}
8 changes: 8 additions & 0 deletions packages/playwright/src/generators/configuration/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@
"description": "Generate JavaScript files rather than TypeScript files.",
"default": false
},
"webServerCommand": {
"type": "string",
"description": "The command to start the web server."
},
"webServerAddress": {
"type": "string",
"description": "The address of the web server."
},
"skipFormat": {
"description": "Skip formatting files.",
"type": "boolean",
Expand Down
25 changes: 25 additions & 0 deletions packages/playwright/src/generators/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
GeneratorCallback,
runTasksInSerial,
Tree,
updateJson,
} from '@nx/devkit';
import { InitGeneratorSchema } from './schema';
import { nxVersion, playwrightVersion } from '../../utils/versions';
Expand All @@ -26,6 +27,30 @@ export async function initGenerator(tree: Tree, options: InitGeneratorSchema) {
if (!options.skipFormat) {
await formatFiles(tree);
}

if (tree.exists('.vscode/extensions.json')) {
updateJson(tree, '.vscode/extensions.json', (json) => {
json.recommendations ??= [];

const recs = new Set(json.recommendations);
recs.add('ms-playwright.playwright');

json.recommendations = Array.from(recs);
return json;
});
} else {
tree.write(
'.vscode/extensions.json',
JSON.stringify(
{
recommendations: ['ms-playwright.playwright'],
},
null,
2
)
);
}

return runTasksInSerial(...tasks);
}

Expand Down
2 changes: 2 additions & 0 deletions packages/playwright/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ export {
playwrightExecutor,
PlaywrightExecutorSchema,
} from './executors/playwright/playwright';
export { initGenerator } from './generators/init/init';
export { configurationGenerator } from './generators/configuration/configuration';

0 comments on commit f753e6f

Please sign in to comment.