Skip to content

Commit

Permalink
build: add retry mechanism for test file import
Browse files Browse the repository at this point in the history
  • Loading branch information
kyubisation committed Oct 5, 2023
1 parent e3039d8 commit 1dc6c00
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
50 changes: 50 additions & 0 deletions ci/postinstall.mts
Original file line number Diff line number Diff line change
@@ -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<string, [string, string]>()
.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();
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions web-test-runner.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 } })]
Expand Down

0 comments on commit 1dc6c00

Please sign in to comment.