-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(testing): add playwright configuration generator (#18013)
- Loading branch information
Showing
14 changed files
with
353 additions
and
145 deletions.
There are no files selected for viewing
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
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,28 @@ | ||
{ | ||
"name": "Nx Playwright", | ||
"version": "0.1", | ||
"schematics": { | ||
"configuration": { | ||
"factory": "./src/generators/configuration/configuration#configurationSchematic", | ||
"schema": "./src/generators/configuration/schema.json", | ||
"description": "Add Nx Playwright configuration to your project" | ||
}, | ||
"init": { | ||
"factory": "./src/generators/init/init#initSchematic", | ||
"schema": "./src/generators/init/schema.json", | ||
"description": "Initializes a Playwright project in the current workspace" | ||
} | ||
}, | ||
"generators": { | ||
"configuration": { | ||
"factory": "./src/generators/configuration/configuration", | ||
"schema": "./src/generators/configuration/schema.json", | ||
"description": "Add Nx Playwright configuration to your project" | ||
}, | ||
"init": { | ||
"factory": "./src/generators/init/init", | ||
"schema": "./src/generators/init/schema.json", | ||
"description": "Initializes a Playwright project in the current workspace" | ||
} | ||
} | ||
} |
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
80 changes: 80 additions & 0 deletions
80
packages/playwright/src/generators/configuration/configuration.ts
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,80 @@ | ||
import { | ||
convertNxGenerator, | ||
formatFiles, | ||
generateFiles, | ||
offsetFromRoot, | ||
readNxJson, | ||
readProjectConfiguration, | ||
toJS, | ||
Tree, | ||
updateNxJson, | ||
updateProjectConfiguration, | ||
} from '@nx/devkit'; | ||
import * as path from 'path'; | ||
import { ConfigurationGeneratorSchema } from './schema'; | ||
import initGenerator from '../init/init'; | ||
|
||
export async function configurationGenerator( | ||
tree: Tree, | ||
options: ConfigurationGeneratorSchema | ||
) { | ||
const projectConfig = readProjectConfiguration(tree, options.project); | ||
generateFiles(tree, path.join(__dirname, 'files'), projectConfig.root, { | ||
offsetFromRoot: offsetFromRoot(projectConfig.root), | ||
projectRoot: projectConfig.root, | ||
...options, | ||
}); | ||
|
||
addE2eTarget(tree, options); | ||
setupE2ETargetDefaults(tree); | ||
|
||
if (options.js) { | ||
toJS(tree); | ||
} | ||
if (!options.skipFormat) { | ||
await formatFiles(tree); | ||
} | ||
|
||
return initGenerator(tree, { | ||
skipFormat: true, | ||
skipPackageJson: options.skipPackageJson, | ||
}); | ||
} | ||
|
||
function setupE2ETargetDefaults(tree: Tree) { | ||
const nxJson = readNxJson(tree); | ||
|
||
if (!nxJson.namedInputs) { | ||
return; | ||
} | ||
|
||
// E2e targets depend on all their project's sources + production sources of dependencies | ||
nxJson.targetDefaults ??= {}; | ||
|
||
const productionFileSet = !!nxJson.namedInputs?.production; | ||
nxJson.targetDefaults.e2e ??= {}; | ||
nxJson.targetDefaults.e2e.inputs ??= [ | ||
'default', | ||
productionFileSet ? '^production' : '^default', | ||
]; | ||
|
||
updateNxJson(tree, nxJson); | ||
} | ||
|
||
function addE2eTarget(tree: Tree, options: ConfigurationGeneratorSchema) { | ||
const projectConfig = readProjectConfiguration(tree, options.project); | ||
if (projectConfig?.targets?.e2e) { | ||
throw new Error(`Project ${options.project} already has an e2e target. | ||
Rename or remove the existing e2e target.`); | ||
} | ||
projectConfig.targets.e2e = { | ||
executor: '@nx/playwright:playwright', | ||
options: {}, | ||
}; | ||
updateProjectConfiguration(tree, options.project, projectConfig); | ||
} | ||
|
||
export default configurationGenerator; | ||
export const configurationSchematic = convertNxGenerator( | ||
configurationGenerator | ||
); |
18 changes: 18 additions & 0 deletions
18
...ages/playwright/src/generators/configuration/files/__directory__/example.spec.ts.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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { test, expect } from '@playwright/test'; | ||
|
||
test('has title', async ({ page }) => { | ||
await page.goto('https://playwright.dev/'); | ||
|
||
// Expect a title "to contain" a substring. | ||
await expect(page).toHaveTitle(/Playwright/); | ||
}); | ||
|
||
test('get started link', async ({ page }) => { | ||
await page.goto('https://playwright.dev/'); | ||
|
||
// Click the get started link. | ||
await page.getByRole('link', { name: 'Get started' }).click(); | ||
|
||
// Expects the URL to contain intro. | ||
await expect(page).toHaveURL(/.*intro/); | ||
}); |
Oops, something went wrong.
e5e561d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
nx-dev – ./
nx-dev-nrwl.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx.dev
nx-five.vercel.app