Skip to content

Commit 2128f8e

Browse files
feat(testing): add @nx/playwright plugin (nrwl#17975)
1 parent 460e469 commit 2128f8e

24 files changed

+709
-1
lines changed

CODEOWNERS

+2
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ pnpm-lock.yaml @nrwl/nx-pipelines-reviewers
9999
/e2e/cypress/** @nrwl/nx-testing-tools-reviewers
100100
/packages/jest/** @nrwl/nx-testing-tools-reviewers
101101
/e2e/jest/** @nrwl/nx-testing-tools-reviewers
102+
/packages/playwright/** @nrwl/nx-testing-tools-reviewers
103+
/e2e/playwright/** @nrwl/nx-testing-tools-reviewers
102104

103105
# Linter
104106
/docs/generated/packages/eslint-plugin/** @nrwl/nx-linter-reviewers @nrwl/nx-docs-reviewers

e2e/playwright/jest.config.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* eslint-disable */
2+
export default {
3+
transform: {
4+
'^.+\\.[tj]sx?$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
5+
},
6+
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html'],
7+
maxWorkers: 1,
8+
globals: {},
9+
globalSetup: '../utils/global-setup.ts',
10+
globalTeardown: '../utils/global-teardown.ts',
11+
displayName: 'e2e-playwright',
12+
preset: '../../jest.preset.js',
13+
};

e2e/playwright/project.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "e2e-playwright",
3+
"$schema": "../../node_modules/nx/schemas/project-schema.json",
4+
"sourceRoot": "e2e/playwright",
5+
"projectType": "application",
6+
"targets": {
7+
"e2e": {},
8+
"run-e2e-tests": {}
9+
},
10+
"implicitDependencies": ["playwright"]
11+
}

e2e/playwright/src/playwright.test.ts

+171
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
import {
2+
cleanupProject,
3+
newProject,
4+
uniq,
5+
createFile,
6+
runCLI,
7+
packageInstall,
8+
runCommand,
9+
} from '@nx/e2e/utils';
10+
11+
const TEN_MINS_MS = 600_000;
12+
describe('Playwright E2E Test runner', () => {
13+
beforeAll(() => {
14+
newProject({ name: uniq('playwright') });
15+
packageInstall('@playwright/test', undefined, '^1.30.0', 'dev');
16+
runCommand('npx playwright install --with-deps');
17+
});
18+
19+
afterAll(() => cleanupProject());
20+
21+
it(
22+
'should test example app',
23+
() => {
24+
//TODO: remove when generators are setup.Need to have basic workspace deps setup
25+
runCLI(`g @nx/js:init`);
26+
addSampleProject();
27+
28+
// NOTE: playwright throws errors if it detects running inside jest process. tmp remove and restore the env var for playwright to run
29+
const results = runCLI(`e2e demo-e2e`);
30+
expect(results).toContain('6 passed');
31+
expect(results).toContain('Successfully ran target e2e for project');
32+
},
33+
TEN_MINS_MS
34+
);
35+
});
36+
37+
// TODO: remove this when there are project generators
38+
function addSampleProject() {
39+
createFile(
40+
'apps/demo-e2e/src/example.spec.ts',
41+
`
42+
import { test, expect } from '@playwright/test';
43+
44+
test('has title', async ({ page }) => {
45+
await page.goto('https://playwright.dev/');
46+
47+
// Expect a title "to contain" a substring.
48+
await expect(page).toHaveTitle(/Playwright/);
49+
});
50+
51+
test('get started link', async ({ page }) => {
52+
await page.goto('https://playwright.dev/');
53+
54+
// Click the get started link.
55+
await page.getByRole('link', { name: 'Get started' }).click();
56+
57+
// Expects the URL to contain intro.
58+
await expect(page).toHaveURL(/.*intro/);
59+
});
60+
`
61+
);
62+
createFile(
63+
'apps/demo-e2e/playwright.config.ts',
64+
`
65+
import { defineConfig, devices } from '@playwright/test';
66+
67+
/**
68+
* Read environment variables from file.
69+
* https://github.com/motdotla/dotenv
70+
*/
71+
// require('dotenv').config();
72+
73+
/**
74+
* See https://playwright.dev/docs/test-configuration.
75+
*/
76+
export default defineConfig({
77+
testDir: './src',
78+
outputDir: '../../dist/playwright/apps/demo-e2e/output',
79+
/* Run tests in files in parallel */
80+
fullyParallel: true,
81+
/* Fail the build on CI if you accidentally left test.only in the source code. */
82+
forbidOnly: !!process.env.CI,
83+
/* Retry on CI only */
84+
retries: process.env.CI ? 2 : 0,
85+
/* Opt out of parallel tests on CI. */
86+
workers: process.env.CI ? 1 : undefined,
87+
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
88+
reporter: [
89+
[
90+
'html',
91+
{
92+
outputFolder:
93+
'../../dist/playwright/apps/demo-e2e/playwright-report',
94+
},
95+
],
96+
],
97+
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
98+
use: {
99+
/* Base URL to use in actions like await page.goto('/'). */
100+
// baseURL: 'http://127.0.0.1:3000',
101+
102+
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
103+
trace: 'on-first-retry',
104+
},
105+
106+
/* Configure projects for major browsers */
107+
projects: [
108+
{
109+
name: 'chromium',
110+
use: { ...devices['Desktop Chrome'] },
111+
},
112+
113+
{
114+
name: 'firefox',
115+
use: { ...devices['Desktop Firefox'] },
116+
},
117+
118+
{
119+
name: 'webkit',
120+
use: { ...devices['Desktop Safari'] },
121+
},
122+
123+
/* Test against mobile viewports. */
124+
// {
125+
// name: 'Mobile Chrome',
126+
// use: { ...devices['Pixel 5'] },
127+
// },
128+
// {
129+
// name: 'Mobile Safari',
130+
// use: { ...devices['iPhone 12'] },
131+
// },
132+
133+
/* Test against branded browsers. */
134+
// {
135+
// name: 'Microsoft Edge',
136+
// use: { ...devices['Desktop Edge'], channel: 'msedge' },
137+
// },
138+
// {
139+
// name: 'Google Chrome',
140+
// use: { ...devices['Desktop Chrome'], channel: 'chrome' },
141+
// },
142+
],
143+
144+
/* Run your local dev server before starting the tests */
145+
// webServer: {
146+
// command: 'npm run start',
147+
// url: 'http://127.0.0.1:3000',
148+
// reuseExistingServer: !process.env.CI,
149+
// },
150+
});
151+
`
152+
);
153+
createFile(
154+
'apps/demo-e2e/project.json',
155+
JSON.stringify(
156+
{
157+
name: 'demo-e2e',
158+
root: 'apps/demo-e2e',
159+
sourceRoot: 'apps/demo-e2e/src',
160+
targets: {
161+
e2e: {
162+
executor: '@nx/playwright:playwright',
163+
options: {},
164+
},
165+
},
166+
},
167+
null,
168+
2
169+
)
170+
);
171+
}

e2e/playwright/tsconfig.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"extends": "../../tsconfig.base.json",
3+
"compilerOptions": {
4+
"types": ["node", "jest"]
5+
},
6+
"include": [],
7+
"files": [],
8+
"references": [
9+
{
10+
"path": "./tsconfig.spec.json"
11+
}
12+
]
13+
}

e2e/playwright/tsconfig.spec.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": "../../dist/out-tsc",
5+
"module": "commonjs",
6+
"types": ["jest", "node"]
7+
},
8+
"include": [
9+
"**/*.test.ts",
10+
"**/*.spec.ts",
11+
"**/*.spec.tsx",
12+
"**/*.test.tsx",
13+
"**/*.spec.js",
14+
"**/*.test.js",
15+
"**/*.spec.jsx",
16+
"**/*.test.jsx",
17+
"**/*.d.ts",
18+
"jest.config.ts"
19+
]
20+
}

e2e/utils/create-project-utils.ts

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export function newProject({
7272
`@nx/next`,
7373
`@nx/node`,
7474
`@nx/plugin`,
75+
`@nx/playwright`,
7576
`@nx/rollup`,
7677
`@nx/react`,
7778
`@nx/storybook`,

e2e/utils/get-env-info.ts

+4
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ export function getStrippedEnvironmentVariables() {
122122
return false;
123123
}
124124

125+
if (key === 'JEST_WORKER_ID') {
126+
return false;
127+
}
128+
125129
return true;
126130
})
127131
);

jest.config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { getJestProjects } = require('@nx/jest');
1+
import { getJestProjects } from '@nx/jest';
22

33
export default {
44
projects: getJestProjects(),

packages/nx/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
"@nrwl/next",
125125
"@nx/node",
126126
"@nrwl/node",
127+
"@nx/playwright",
127128
"@nx/plugin",
128129
"@nrwl/nx-plugin",
129130
"@nx/react",

packages/playwright/.eslintrc.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"extends": ["../../.eslintrc.json"],
3+
"ignorePatterns": ["!**/*"],
4+
"overrides": [
5+
{
6+
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
7+
"rules": {}
8+
},
9+
{
10+
"files": ["*.ts", "*.tsx"],
11+
"rules": {}
12+
},
13+
{
14+
"files": ["*.js", "*.jsx"],
15+
"rules": {}
16+
},
17+
{
18+
"files": ["./package.json", "./executors.json"],
19+
"parser": "jsonc-eslint-parser",
20+
"rules": {
21+
"@nx/nx-plugin-checks": "error"
22+
}
23+
}
24+
]
25+
}

packages/playwright/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<p style="text-align: center;"><img src="https://raw.githubusercontent.com/nrwl/nx/master/images/nx.png" width="600" alt="Nx - Smart, Fast and Extensible Build System"></p>
2+
3+
{{links}}
4+
5+
<hr>
6+
7+
# Nx: Smart, Fast and Extensible Build System
8+
9+
Nx is a next generation build system with first class monorepo support and powerful integrations.
10+
11+
{{content}}

packages/playwright/executors.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"builders": {
3+
"playwright": {
4+
"implementation": "./src/executors/playwright/compat",
5+
"schema": "./src/executors/playwright/schema.json",
6+
"description": "Run Playwright tests."
7+
}
8+
},
9+
"executors": {
10+
"playwright": {
11+
"implementation": "./src/executors/playwright/playwright",
12+
"schema": "./src/executors/playwright/schema.json",
13+
"description": "Run Playwright tests."
14+
}
15+
}
16+
}

packages/playwright/jest.config.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* eslint-disable */
2+
export default {
3+
displayName: 'playwright',
4+
preset: '../../jest.preset.js',
5+
transform: {
6+
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
7+
},
8+
moduleFileExtensions: ['ts', 'js', 'html'],
9+
coverageDirectory: '../../coverage/packages/playwright',
10+
};

packages/playwright/package.json

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"name": "@nx/playwright",
3+
"version": "0.0.1",
4+
"type": "commonjs",
5+
"homepage": "https://nx.dev",
6+
"private": true,
7+
"description": "The Nx Plugin for Playwright contains executors and generators allowing your workspace to use the powerful Playwright integration testing capabilities.",
8+
"keywords": [
9+
"Monorepo",
10+
"Angular",
11+
"React",
12+
"Web",
13+
"Node",
14+
"Nest",
15+
"Jest",
16+
"Playwright",
17+
"CLI"
18+
],
19+
"main": "./index",
20+
"typings": "./index.d.ts",
21+
"author": "Victor Savkin",
22+
"license": "MIT",
23+
"bugs": {
24+
"url": "https://github.com/nrwl/nx/issues"
25+
},
26+
"repository": {
27+
"type": "git",
28+
"url": "https://github.com/nrwl/nx.git",
29+
"directory": "packages/playwright"
30+
},
31+
"dependencies": {
32+
"@nx/devkit": "file:../devkit"
33+
},
34+
"peerDependencies": {
35+
"@playwright/test": "^1.30.0"
36+
},
37+
"peerDependenciesMeta": {
38+
"@playwright/test": {
39+
"optional": true
40+
}
41+
},
42+
"executors": "./executors.json"
43+
}

0 commit comments

Comments
 (0)