Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

playwright-plugin.ts

456 lines13.0 KB

1import { dirname } from 'node:path'
2import { createNodesFromFiles, readJsonFile } from '@nx/devkit'
3import type {
4 CreateNodes,
5 CreateNodesContext,
6 TargetConfiguration,
7} from '@nx/devkit'
8​
9export const createNodes: CreateNodes = [
10 '**/package.json',
11 async (configFiles, options, context) => {
12 return await createNodesFromFiles(
13 (configFile, _options, context) =>
14 createNodesInternal(configFile, context),
15 configFiles,
16 options,
17 context,
18 )
19 },
21​
22function createNodesInternal(
23 configFilePath: string,
24 _context: CreateNodesContext,
25) {
26 const projectConfiguration = readJsonFile<{
27 name?: string
28 nx?: {
29 metadata?: {
30 playwrightShards?: number
31 playwrightModes?: Array<RawPlaywrightModeMetadata>
32 }
33 }
34 }>(configFilePath)
35 const root = dirname(configFilePath)
36 const packageName = projectConfiguration.name ?? root
37 const playwrightModes =
38 projectConfiguration.nx?.metadata?.playwrightModes ?? []
39​
40 if (playwrightModes.length > 0) {
41 const { targets, targetGroupEntries } = buildModeTargets(
42 root,
43 packageName,
44 playwrightModes,
45 )
46​
47 return {
48 projects: {
49 [root]: {
50 targets,
51 metadata: {
52 targetGroups: {
53 playwright: targetGroupEntries,
54 },
55 },
56 tags: ['playwright:sharded'],
57 },
58 },
59 }
60 }
61​
62 if (!projectConfiguration.nx?.metadata?.playwrightShards) {
63 return {
64 projects: {
65 [root]: {},
66 },
67 }
68 }
69​
70 const { targets, targetGroupEntries } = buildShardedTargets(
71 root,
72 projectConfiguration.nx.metadata.playwrightShards,
73 )
74 // Project configuration to be merged into the rest of the Nx configuration
75 return {
76 projects: {
77 [root]: {
78 targets,
79 metadata: {
80 targetGroups: {
81 playwright: targetGroupEntries,
82 },
83 },
84 tags: ['playwright:sharded'],
85 },
86 },
87 }
89​
90const CI_TARGET_NAME = 'test:e2e'
91const MODE_TARGET_SEPARATOR = '--'
92const TEST_INPUTS: TargetConfiguration['inputs'] = [
93 'default',
94 'dependentTaskOutputs',
95 '^buildProduction',
97const BUILD_INPUTS: TargetConfiguration['inputs'] = [
98 'buildProduction',
99 'dependentTaskOutputs',
100 '^buildProduction',
102​
103const PLAYWRIGHT_TOOLCHAINS = ['vite', 'rsbuild'] as const
104const PLAYWRIGHT_MODES = ['ssr', 'spa', 'prerender', 'preview'] as const
105​
106type PlaywrightToolchain = (typeof PLAYWRIGHT_TOOLCHAINS)[number]
107type PlaywrightMode = (typeof PLAYWRIGHT_MODES)[number]
108​
109const PLAYWRIGHT_BUILD_COMMANDS: Record<PlaywrightToolchain, string> = {
110 vite: 'vite build && tsc --noEmit',
111 rsbuild: 'rsbuild build && tsc --noEmit',
113​
114function captureCommandOutput(command: string, outputFile?: string): string {
115 if (!outputFile) {
116 return command
117 }
118​
119 if (!/^[a-zA-Z0-9._-]+$/.test(outputFile)) {
120 throw new Error(
121 `[Playwright Sharding Plugin] Invalid E2E_BUILD_LOG value: ${outputFile}. ` +
122 `Expected a filename containing only letters, numbers, dots, dashes, and underscores.`,
123 )
124 }
125​
126 const escapedCommand = command.replaceAll("'", "'\\''")
127 return `bash -o pipefail -c '{ ${escapedCommand}; } 2>&1 | tee ${outputFile}'`
129​
130function getTestOutputs(
131 modeKey: string,
132 taskKey: string,
133): TargetConfiguration['outputs'] {
134 return [
135 `{projectRoot}/test-results/${taskKey}`,
136 `{projectRoot}/violations.${modeKey}.*.json`,
137 ]
139​
140type PlaywrightModeMetadata = {
141 toolchain: PlaywrightToolchain
142 mode: PlaywrightMode
143 shards?: number
144 name?: string
145 env: Record<string, string>
147​
148type RawPlaywrightModeMetadata = {
149 toolchain?: string
150 mode?: string
151 shards?: number
152 name?: string
153 env?: Record<string, string>
155​
156function validateModeMetadata(
157 modeMetadata: RawPlaywrightModeMetadata,
158 index: number,
159): PlaywrightModeMetadata {
160 const targetName = `${modeMetadata.toolchain ?? '<missing toolchain>'}:${modeMetadata.mode ?? '<missing mode>'}`
161​
162 if (!modeMetadata.toolchain || !modeMetadata.mode) {
163 throw new Error(
164 `[Playwright Sharding Plugin] Invalid playwrightModes[${index}] entry: ` +
165 `${JSON.stringify(modeMetadata)}. Both "toolchain" and "mode" are required.`,
166 )
167 }
168​
169 if (
170 !PLAYWRIGHT_TOOLCHAINS.includes(
171 modeMetadata.toolchain as PlaywrightToolchain,
172 )
173 ) {
174 throw new Error(
175 `[Playwright Sharding Plugin] Invalid toolchain for playwrightModes[${index}]: ` +
176 `${modeMetadata.toolchain}. Supported toolchains: ${PLAYWRIGHT_TOOLCHAINS.join(', ')}.`,
177 )
178 }
179​
180 if (!PLAYWRIGHT_MODES.includes(modeMetadata.mode as PlaywrightMode)) {
181 throw new Error(
182 `[Playwright Sharding Plugin] Invalid mode for playwrightModes[${index}]: ` +
183 `${modeMetadata.mode}. Supported modes: ${PLAYWRIGHT_MODES.join(', ')}.`,
184 )
185 }
186​
187 if (modeMetadata.shards !== undefined) {
188 const shardCount = Number(modeMetadata.shards)
189​
190 if (!Number.isInteger(shardCount) || shardCount < 1) {
191 throw new Error(
192 `[Playwright Sharding Plugin] Invalid shard count for ${targetName}: ${modeMetadata.shards}. ` +
193 `Expected a positive integer.`,
194 )
195 }
196 }
197​
198 if (
199 modeMetadata.name !== undefined &&
200 !/^[a-z0-9][a-z0-9-]*$/.test(modeMetadata.name)
201 ) {
202 throw new Error(
203 `[Playwright Sharding Plugin] Invalid name for playwrightModes[${index}]: ` +
204 `${modeMetadata.name}. Expected lowercase letters, numbers, and dashes.`,
205 )
206 }
207​
208 if (modeMetadata.env !== undefined) {
209 if (
210 typeof modeMetadata.env !== 'object' ||
211 Array.isArray(modeMetadata.env)
212 ) {
213 throw new Error(
214 `[Playwright Sharding Plugin] Invalid env for playwrightModes[${index}]: ` +
215 `Expected an object of string values.`,
216 )
217 }
218​
219 for (const [key, value] of Object.entries(modeMetadata.env)) {
220 if (typeof value !== 'string') {
221 throw new Error(
222 `[Playwright Sharding Plugin] Invalid env value for playwrightModes[${index}].${key}: ` +
223 `Expected a string.`,
224 )
225 }
226 }
227 }
228​
229 return {
230 toolchain: modeMetadata.toolchain as PlaywrightToolchain,
231 mode: modeMetadata.mode as PlaywrightMode,
232 shards: modeMetadata.shards,
233 name: modeMetadata.name,
234 env: modeMetadata.env ?? {},
235 }
237​
238function buildModeTargets(
239 projectRoot: string,
240 packageName: string,
241 modes: Array<RawPlaywrightModeMetadata>,
242): {
243 targets: Record<string, TargetConfiguration>
244 targetGroupEntries: Array<string>
245} {
246 const targets: Record<string, TargetConfiguration> = {}
247 const targetGroup: Array<string> = []
248 const ciDependsOnTargets: Array<{
249 target: string
250 params: 'forward'
251 }> = []
252​
253 for (const [index, rawModeMetadata] of modes.entries()) {
254 const modeMetadata = validateModeMetadata(rawModeMetadata, index)
255 const variantTargetSuffix = modeMetadata.name
256 ? `${MODE_TARGET_SEPARATOR}${modeMetadata.name}`
257 : ''
258 const variantPathSuffix = modeMetadata.name ? `-${modeMetadata.name}` : ''
259 const modeTargetName = `${CI_TARGET_NAME}--${modeMetadata.toolchain}-${modeMetadata.mode}${variantTargetSuffix}`
260 const buildTargetName = `build:${modeMetadata.toolchain}:${modeMetadata.mode}${modeMetadata.name ? `:${modeMetadata.name}` : ''}`
261 const shardCount = modeMetadata.shards ?? 1
262 const distDir = `dist-${modeMetadata.toolchain}-${modeMetadata.mode}${variantPathSuffix}`
263 const modeKey = `${modeMetadata.toolchain}-${modeMetadata.mode}${variantPathSuffix}`
264 const modeEnv: Record<string, string> = {
265 ...modeMetadata.env,
266 MODE: modeMetadata.mode,
267 TOOLCHAIN: modeMetadata.toolchain,
268 E2E_TOOLCHAIN: modeMetadata.toolchain,
269 E2E_MODE_KEY: modeKey,
270 E2E_DIST: distDir,
271 E2E_DIST_DIR: distDir,
272 }
273 const buildLog = modeEnv.E2E_BUILD_LOG
274 const modeTaskKey = `${packageName}-${modeKey}-e2e`
275 const modeDescription = `${modeMetadata.toolchain}/${modeMetadata.mode}${modeMetadata.name ? `/${modeMetadata.name}` : ''}`
276 const modeShardTargets: Array<string> = []
277​
278 targets[buildTargetName] = {
279 executor: 'nx:run-commands',
280 options: {
281 command: captureCommandOutput(
282 PLAYWRIGHT_BUILD_COMMANDS[modeMetadata.toolchain],
283 buildLog,
284 ),
285 cwd: projectRoot,
286 env: modeEnv,
287 },
288 parallelism: false,
289 cache: true,
290 inputs: BUILD_INPUTS,
291 dependsOn: ['^build'],
292 outputs: [
293 `{projectRoot}/${distDir}`,
294 ...(buildLog ? [`{projectRoot}/${buildLog}`] : []),
295 ],
296 metadata: {
297 technologies: ['playwright'],
298 description: `Build artifacts for ${modeDescription} e2e tests`,
299 },
300 }
301 targetGroup.push(buildTargetName)
302​
303 if (shardCount === 1) {
304 targets[modeTargetName] = {
305 executor: 'nx:run-commands',
306 options: {
307 command: `playwright test --project=chromium --output=test-results/${modeTaskKey}`,
308 cwd: projectRoot,
309 env: modeEnv,
310 },
311 parallelism: false,
312 cache: true,
313 inputs: TEST_INPUTS,
314 outputs: getTestOutputs(modeKey, modeTaskKey),
315 dependsOn: [buildTargetName],
316 metadata: {
317 technologies: ['playwright'],
318 description: `Run Playwright tests for ${modeDescription}`,
319 },
320 }
321 targetGroup.push(modeTargetName)
322 } else {
323 for (let shardIndex = 1; shardIndex <= shardCount; shardIndex++) {
324 const shardTargetName = `${modeTargetName}${MODE_TARGET_SEPARATOR}shard-${shardIndex}-of-${shardCount}`
325 const shardTaskKey = `${modeTaskKey}-shard-${shardIndex}-of-${shardCount}`
326​
327 targets[shardTargetName] = {
328 executor: 'nx:run-commands',
329 options: {
330 command: `playwright test --project=chromium --shard=${shardIndex}/${shardCount} --output=test-results/${shardTaskKey}`,
331 cwd: projectRoot,
332 env: modeEnv,
333 },
334 parallelism: false,
335 cache: true,
336 inputs: TEST_INPUTS,
337 outputs: getTestOutputs(modeKey, shardTaskKey),
338 dependsOn: [buildTargetName],
339 metadata: {
340 technologies: ['playwright'],
341 description: `Run Playwright ${modeDescription} shard ${shardIndex} of ${shardCount}`,
342 help: {
343 command: `npx playwright run --help`,
344 example: {
345 options: {
346 shard: `${shardIndex}/${shardCount}`,
347 },
348 },
349 },
350 },
351 }
352​
353 modeShardTargets.push(shardTargetName)
354 targetGroup.push(shardTargetName)
355 }
356​
357 targets[modeTargetName] = {
358 executor: 'nx:noop',
359 cache: true,
360 inputs: TEST_INPUTS,
361 dependsOn: modeShardTargets.map((shardTargetName) => ({
362 target: shardTargetName,
363 params: 'forward' as const,
364 })),
365 metadata: {
366 technologies: ['playwright'],
367 description: `Run all Playwright ${modeDescription} shards (${shardCount} shards)`,
368 },
369 }
370 targetGroup.push(modeTargetName)
371 }
372​
373 ciDependsOnTargets.push({
374 target: modeTargetName,
375 params: 'forward',
376 })
377 }
378​
379 targets[CI_TARGET_NAME] = {
380 executor: 'nx:noop',
381 cache: true,
382 inputs: TEST_INPUTS,
383 dependsOn: ciDependsOnTargets,
384 metadata: {
385 technologies: ['playwright'],
386 description: `Run all Playwright modes (${modes.length} modes)`,
387 },
388 }
389​
390 return {
391 targets,
392 targetGroupEntries: [...targetGroup, CI_TARGET_NAME],
393 }
395​
396function buildShardedTargets(
397 projectRoot: string,
398 shardCount: number,
399): {
400 targets: Record<string, TargetConfiguration>
401 targetGroupEntries: Array<string>
402} {
403 const targets: Record<string, TargetConfiguration> = {}
404 const targetGroup: Array<string> = []
405​
406 // Create individual shard targets
407 for (let shardIndex = 1; shardIndex <= shardCount; shardIndex++) {
408 const shardTargetName = `${CI_TARGET_NAME}--shard-${shardIndex}-of-${shardCount}`
409​
410 targets[shardTargetName] = {
411 executor: 'nx:run-commands',
412 options: {
413 command: `playwright test --project=chromium --shard=${shardIndex}/${shardCount}`,
414 cwd: projectRoot,
415 },
416 parallelism: false,
417 cache: true,
418 inputs: TEST_INPUTS,
419 dependsOn: [`^build`, 'build'],
420 metadata: {
421 technologies: ['playwright'],
422 description: `Run Playwright shard ${shardIndex} of ${shardCount}`,
423 help: {
424 command: `npx playwright run --help`,
425 example: {
426 options: {
427 shard: `${shardIndex}/${shardCount}`,
428 },
429 },
430 },
431 },
432 }
433​
434 targetGroup.push(shardTargetName)
435 }
436​
437 // Create the parent CI target that depends on merge (which itself depends on all shards)
438 targets[CI_TARGET_NAME] = {
439 executor: 'nx:noop',
440 cache: true,
441 inputs: TEST_INPUTS,
442 dependsOn: targetGroup.map((shardTargetName) => ({
443 target: shardTargetName,
444 params: 'forward' as const,
445 })),
446 metadata: {
447 technologies: ['playwright'],
448 description: `Run all Playwright shards (${shardCount} shards)`,
449 },
450 }
451​
452 return {
453 targets,
454 targetGroupEntries: [...targetGroup, CI_TARGET_NAME],
455 }