From 1dc6c00461add27bc2fd4589488489f331a5a9ee Mon Sep 17 00:00:00 2001 From: Lukas Spirig Date: Thu, 5 Oct 2023 19:30:08 +0200 Subject: [PATCH] build: add retry mechanism for test file import --- ci/postinstall.mts | 50 +++++++++++++++++++++++++++++++++++++++ package.json | 2 +- web-test-runner.config.js | 6 ++--- 3 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 ci/postinstall.mts diff --git a/ci/postinstall.mts b/ci/postinstall.mts new file mode 100644 index 00000000000..1743c1d69ec --- /dev/null +++ b/ci/postinstall.mts @@ -0,0 +1,50 @@ +import { execSync } from 'child_process'; +import { readFileSync, writeFileSync } from 'fs'; + +const root = new URL('..', import.meta.url); + +main(); + +function main() { + exec('yarn playwright install --with-deps'); + + new Map() + .set('node_modules/@web/test-runner-mocha/dist/autorun.js', [ + 'await import(new URL(r,f).href)', + 'await import(new URL(r,f).href)' + + '.catch(()=>{console.warn(`Failed to load test file ${r}. Retrying...`);return new Promise(r=>setTimeout(r, 100)).then(()=>import(new URL(r,f).href))})'.repeat(3), + ]) + .forEach(([search, replace], filePath) => searchAndReplace(search, replace, filePath)); +} + +/** + * Schedules an edit where the specified file is read and its content replaced based on + * the given search expression and corresponding replacement. Throws if no changes were made + * and the patch has not been applied. + */ +function searchAndReplace(search: string, replacement: string, relativeFilePath: string) { + const filePath = new URL(relativeFilePath, root); + const originalContent = readFileSync(filePath, 'utf8'); + const newFileContent = originalContent.replace(search, replacement); + if (originalContent === newFileContent) { + throw Error(`Could not perform replacement in: ${filePath}.\nSearched for pattern: ${search}`); + } + writeFileSync(filePath, newFileContent, 'utf8'); +} + +/** + * Executes the given command in the project directory. + * @param command The command to run + * @param captureStdout Whether the stdout should be captured and + * returned. + */ +function exec(command: string, captureStdout = false) { + const stdout = execSync(command, { + cwd: root, + stdio: ['inherit', captureStdout ? 'pipe' : 'inherit', 'inherit'], + }); + if (captureStdout) { + process.stdout.write(stdout); + return stdout.toString().trim(); + } +} diff --git a/package.json b/package.json index 22820ee3dac..0e99ceae125 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "src/global/**/*.scss" ], "scripts": { - "postinstall": "yarn playwright install --with-deps", + "postinstall": "tsx ci/postinstall.mts", "build:components": "vite build", "build:storybook": "storybook build --quiet", "build:chromatic-stories": "tsx ci/chromatic-stories-generator.ts", diff --git a/web-test-runner.config.js b/web-test-runner.config.js index 64f27e424a6..6676abba5e9 100644 --- a/web-test-runner.config.js +++ b/web-test-runner.config.js @@ -26,9 +26,9 @@ const e2eFiles = glob const browsers = isCIEnvironment ? [ - playwrightLauncher({ product: 'chromium', concurrency: 1 }), - playwrightLauncher({ product: 'firefox', concurrency: 1 }), - playwrightLauncher({ product: 'webkit', concurrency: 1 }), + playwrightLauncher({ product: 'chromium' }), + playwrightLauncher({ product: 'firefox' }), + playwrightLauncher({ product: 'webkit' }), ] : isDebugMode ? [puppeteerLauncher({ concurrency: 1, launchOptions: { headless: false, devtools: true } })]