-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
ci: Add workflow to detect flaky tests #7497
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
name: 'Detect flaky tests' | ||
on: | ||
workflow_dispatch: | ||
pull_request: | ||
paths: | ||
- 'packages/browser-integration-tests/suites/**' | ||
|
||
env: | ||
HEAD_COMMIT: ${{ github.event.inputs.commit || github.sha }} | ||
|
||
NX_CACHE_RESTORE_KEYS: | | ||
nx-Linux-${{ github.ref }}-${{ github.event.inputs.commit || github.sha }} | ||
nx-Linux-${{ github.ref }} | ||
nx-Linux | ||
|
||
# Cancel in progress workflows on pull_requests. | ||
# https://docs.github.com/en/actions/using-jobs/using-concurrency#example-using-a-fallback-value | ||
concurrency: | ||
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} | ||
cancel-in-progress: true | ||
|
||
jobs: | ||
flaky-detector: | ||
runs-on: ubuntu-20.04 | ||
timeout-minutes: 60 | ||
name: 'Check tests for flakiness' | ||
steps: | ||
- name: Check out current branch | ||
uses: actions/checkout@v3 | ||
- name: Set up Node | ||
uses: volta-cli/action@v4 | ||
|
||
- name: Install dependencies | ||
run: yarn install --ignore-engines --frozen-lockfile | ||
|
||
- name: NX cache | ||
uses: actions/cache/restore@v3 | ||
with: | ||
path: .nxcache | ||
key: nx-Linux-${{ github.ref }}-${{ env.HEAD_COMMIT }} | ||
restore-keys: ${{ env.NX_CACHE_RESTORE_KEYS }} | ||
|
||
- name: Build packages | ||
run: yarn build | ||
|
||
- name: Get npm cache directory | ||
id: npm-cache-dir | ||
run: echo "dir=$(npm config get cache)" >> $GITHUB_OUTPUT | ||
- name: Get Playwright version | ||
id: playwright-version | ||
run: echo "version=$(node -p "require('@playwright/test/package.json').version")" >> $GITHUB_OUTPUT | ||
- uses: actions/cache@v3 | ||
name: Check if Playwright browser is cached | ||
id: playwright-cache | ||
with: | ||
path: ${{ steps.npm-cache-dir.outputs.dir }} | ||
key: ${{ runner.os }}-Playwright-${{steps.playwright-version.outputs.version}} | ||
- name: Install Playwright browser if not cached | ||
if: steps.playwright-cache.outputs.cache-hit != 'true' | ||
run: npx playwright install --with-deps | ||
env: | ||
PLAYWRIGHT_BROWSERS_PATH: ${{steps.npm-cache-dir.outputs.dir}} | ||
- name: Install OS dependencies of Playwright if cache hit | ||
if: steps.playwright-cache.outputs.cache-hit == 'true' | ||
run: npx playwright install-deps | ||
|
||
- name: Determine changed tests | ||
uses: getsentry/[email protected] | ||
id: changed | ||
with: | ||
list-files: json | ||
filters: | | ||
browser_integration: packages/browser-integration-tests/suites/** | ||
|
||
- name: Detect flaky tests | ||
run: yarn test:detect-flaky | ||
working-directory: packages/browser-integration-tests | ||
env: | ||
CHANGED_TEST_PATHS: ${{ steps.changed.outputs.browser_integration_files }} | ||
# Run 100 times when detecting changed test(s), else run all tests 5x | ||
TEST_RUN_COUNT: ${{ steps.changed.outputs.browser_integration == 'true' && 100 || 5 }} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
packages/browser-integration-tests/scripts/detectFlakyTests.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import * as glob from 'glob'; | ||
import * as path from 'path'; | ||
import * as childProcess from 'child_process'; | ||
import { promisify } from 'util'; | ||
|
||
const exec = promisify(childProcess.exec); | ||
|
||
async function run(): Promise<void> { | ||
let testPaths = getTestPaths(); | ||
let failed = []; | ||
|
||
try { | ||
const changedPaths: string[] = process.env.CHANGED_TEST_PATHS ? JSON.parse(process.env.CHANGED_TEST_PATHS) : []; | ||
|
||
if (changedPaths.length > 0) { | ||
console.log(`Detected changed test paths: | ||
${changedPaths.join('\n')} | ||
|
||
`); | ||
|
||
testPaths = testPaths.filter(p => changedPaths.some(changedPath => changedPath.includes(p))); | ||
} | ||
} catch { | ||
console.log('Could not detect changed test paths, running all tests.'); | ||
} | ||
|
||
const cwd = path.join(__dirname, '../'); | ||
const runCount = parseInt(process.env.TEST_RUN_COUNT || '10'); | ||
|
||
for (const testPath of testPaths) { | ||
console.log(`Running test: ${testPath}`); | ||
const start = Date.now(); | ||
|
||
try { | ||
await exec(`yarn playwright test ${testPath} --browser='all' --repeat-each ${runCount}`, { | ||
cwd, | ||
}); | ||
const end = Date.now(); | ||
console.log(` ☑️ Passed ${runCount} times, avg. duration ${Math.ceil((end - start) / runCount)}ms`); | ||
} catch (error) { | ||
logError(error); | ||
failed.push(testPath); | ||
} | ||
} | ||
|
||
console.log(''); | ||
console.log(''); | ||
|
||
if (failed.length > 0) { | ||
console.error(`⚠️ ${failed.length} test(s) failed.`); | ||
process.exit(1); | ||
} else { | ||
console.log(`☑️ ${testPaths.length} test(s) passed.`); | ||
} | ||
} | ||
|
||
function getTestPaths(): string[] { | ||
const paths = glob.sync('suites/**/test.{ts,js}', { | ||
cwd: path.join(__dirname, '../'), | ||
}); | ||
|
||
return paths.map(p => path.dirname(p)); | ||
} | ||
|
||
function logError(error: unknown) { | ||
if (process.env.CI) { | ||
console.log('::group::Test failed'); | ||
} else { | ||
console.error(' ⚠️ Test failed:'); | ||
} | ||
|
||
console.log((error as any).stdout); | ||
console.log((error as any).stderr); | ||
|
||
if (process.env.CI) { | ||
console.log('::endgroup::'); | ||
} | ||
} | ||
|
||
run(); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.