()
| 9 | } |
| 10 | |
| 11 | async function generateTests() { |
| 12 | // Go from `internal/packages/playwright/dist/scripts` to `internal/packages/playwright` |
| 13 | const packageRoot = path.resolve(__dirname, '../..') |
| 14 | // Go from `internal/packages/playwright` to the root of the repo |
| 15 | const rootDir = path.join(packageRoot, '../../..') |
| 16 | // This is where all the tests will be generated |
| 17 | const relativeTestDir = path.join('src', 'e2e', 'tests', 'generated') |
| 18 | const testDir = path.join(packageRoot, relativeTestDir) |
| 19 | const getFiles = (folder: string): Promise<[string, string[]]> => |
| 20 | fs.readdir(path.join(rootDir, folder)).then((paths) => [folder, paths]) |
| 21 | const examplesBySection = await Promise.all([ |
| 22 | getFiles('edge-middleware'), |
| 23 | getFiles('solutions'), |
| 24 | getFiles('starter'), |
| 25 | ]) |
| 26 | let count = 0 |
| 27 | |
| 28 | // Ensure that the test directory exists |
| 29 | await fs.mkdir(testDir, { recursive: true }) |
| 30 | // Write all tests to the test directory |
| 31 | await Promise.all( |
| 32 | examplesBySection.map(([folder, paths]) => |
| 33 | Promise.all( |
| 34 | paths.map(async (exampleName) => { |
| 35 | const examplePath = path.join(rootDir, folder, exampleName) |
| 36 | const stat = await fs.stat(examplePath) |
| 37 | |
| 38 | if (!stat.isDirectory()) return |
| 39 | |
| 40 | const exampleFiles = await fs.readdir(examplePath) |
| 41 | const readme = exampleFiles.find((file) => /readme\.md$/i.test(file)) |
| 42 | |
| 43 | if (!readme) return |
| 44 | |
| 45 | const content = await fs.readFile( |
| 46 | path.join(examplePath, readme), |
| 47 | 'utf8' |
| 48 | ) |
| 49 | const { attributes } = frontMatter<Attributes>(content) |
| 50 | |
| 51 | if (!attributes.demoUrl || attributes.ignoreE2E) return |
| 52 | |
| 53 | const testContent = getTest(attributes.demoUrl) |
| 54 | |
| 55 | await fs.writeFile( |
| 56 | path.join(testDir, `${exampleName}.spec.ts`), |
| 57 | testContent |
| 58 | ) |
| 59 | count++ |
| 60 | }) |
| 61 | ) |
| 62 | ) |
| 63 | ).finally(() => { |
| 64 | console.log(`\n Added ${count} tests to ${relativeTestDir}\n`) |
| 65 | }) |
| 66 | } |
| 67 | |
| 68 | generateTests().catch((error) => { |
no test coverage detected