Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/flaky.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ live quarantined test, and nothing else is wrong with that attempt (no kill, no
failure outside the report, no first-run test missing), the whole command may
run again, up to 3 attempts in total. The run is green only when one of those
attempts is fully green. A quarantined test that fails all 3 is a **hard
break**, and the run is red. A test that is not quarantined keeps its one
break**, and the run is red. A quarantined test that is skipped on the final attempt is a **quarantined-fail**, and the run is red: a skip is not a run, so it never counts toward the clean streak. A test that is not quarantined keeps its one
rerun. A red attempt never turns green because its only reported failure is a
quarantined test, because the runner's exit status can come from things the
report does not show (a coverage threshold, a fail-on-flaky policy, a global
Expand Down
19 changes: 14 additions & 5 deletions lib/flaky-run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,12 @@ function mayRunAgain(again: Ran, first: JunitReport, quarantined: ReadonlySet<st
const history = (t: JunitTest, runs: Ran[]): Attempt[] =>
[...t.attempts, ...runs.flatMap(r => r.report?.tests.find(x => x.id === t.id)?.attempts ?? ['fail' as const])];

/** A quarantined test passes only when it never failed or an attempt with the runner's full verdict cleared it. */
/** A quarantined test passes only when its final attempt passed and it never failed, or an attempt with the runner's full verdict cleared it. A skip on the final attempt is not a run, so it fails; a test that failed every attempt is a hard break. */
const quarantinedStatus = (attempts: string[], cleared: boolean): 'quarantined-pass' | 'quarantined-fail' | 'hard-break' =>
cleared || !attempts.includes('fail') ? 'quarantined-pass'
: attempts.length >= QUARANTINE_ATTEMPTS && attempts.every(a => a === 'fail') ? 'hard-break' : 'quarantined-fail';
attempts.length >= QUARANTINE_ATTEMPTS && attempts.every(a => a === 'fail') ? 'hard-break'
: attempts.at(-1) !== 'pass' ? 'quarantined-fail'
: cleared || !attempts.includes('fail') ? 'quarantined-pass'
: 'quarantined-fail';

export async function runFlaky(opts: RunOptions): Promise<RunResult> {
const cwd = opts.cwd ?? process.cwd();
Expand Down Expand Up @@ -363,7 +365,8 @@ export async function runFlaky(opts: RunOptions): Promise<RunResult> {
if (why) settle(t, t.attempts, why);
else eligible.push(t);
}
const passedQ = report.tests.filter(t => isQ(t) && finalAttempt(t) !== 'fail' && t.attempts.some(a => a !== 'skip'));
// A quarantined test whose final attempt is a skip never ran on the attempt that decided the run; it is settled below as a quarantined-fail, not dropped silently.
const settledQ = report.tests.filter(t => isQ(t) && finalAttempt(t) !== 'fail');
const wholeRerun = !eligible.length ? undefined
: opts.rerunScope !== 'test' ? 'the default; --rerun-scope test reruns the failed test alone'
: incompleteRun(report.runner, first.output, first.exit, opts.command, runnerFlaky.length) ?? undefined;
Expand Down Expand Up @@ -409,7 +412,13 @@ export async function runFlaky(opts: RunOptions): Promise<RunResult> {
if (reruns) fs.writeFileSync(junit, firstXml);
}
const green = runs.length ? fullGreen(runs.at(-1)!, report) : reruns > 0 || first.exit === 0;
for (const t of passedQ) settleQ(t, history(t, runs), green, 'the attempt that decided the run was not a full green run', runs.length ? 'command' : undefined);
for (const t of settledQ) {
const attempts = history(t, runs);
const reason = attempts.at(-1) === 'skip'
? 'quarantined, and skipped on the final attempt instead of running'
: 'the attempt that decided the run was not a full green run';
settleQ(t, attempts, green, reason, runs.length ? 'command' : undefined);
}
const red = !green || outcomes.some(o => !PASSING.has(o.status));
const q = outcomes.filter(o => o.status === 'quarantined-pass' || o.status === 'quarantined-fail' || o.status === 'hard-break').length;
return { exitCode: red ? 1 : 0, tests: report.tests.length, commandExit: first.exit, outcomes, reruns, quarantined: q, ...(wholeRerun && reruns ? { wholeRerun } : {}), ledger };
Expand Down
9 changes: 9 additions & 0 deletions test/flaky-run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ function fakeRun(reports: (argv: string[], n: number) => string | null, exits: (
const suite = (cases: string) => `<testsuites name="bun test"><testsuite name="a.test.ts" file="a.test.ts">${cases}</testsuite></testsuites>`;
const pass = (name: string) => `<testcase name="${name}" classname="" file="a.test.ts"/>`;
const fail = (name: string, msg = 'boom') => `<testcase name="${name}" classname="" file="a.test.ts"><failure message="${msg}"/></testcase>`;
const skip = (name: string) => `<testcase name="${name}" classname="" file="a.test.ts"><skipped/></testcase>`;

describe('runFlaky', () => {
const base = (spawn: ReturnType<typeof fakeRun>, dir: string, extra: Partial<Parameters<typeof runFlaky>[0]> = {}) =>
Expand All @@ -131,6 +132,14 @@ describe('runFlaky', () => {
expect(f.calls).toHaveLength(1);
});

test('an ordinary skipped test still follows the runner verdict', async () => {
const dir = tmp();
const f = fakeRun(() => suite(skip('s') + pass('a')), () => 0);
const r = await base(f, dir);
expect(r).toMatchObject({ exitCode: 0, quarantined: 0, reruns: 0, ledger: [], outcomes: [] });
expect(f.calls).toHaveLength(1);
});

test('a coverage failure beside a flaky test is not cleared by a narrowed rerun', async () => {
const dir = tmp();
const cov = () => '----|---------|---------|\nAll files | % Funcs | % Lines |\nRan 2 tests across 1 file.\n';
Expand Down
29 changes: 29 additions & 0 deletions test/quarantine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ function fakeRun(reports: (n: number, argv: string[]) => string | null, exits: (
const suite = (cases: string, file = 'a.test.ts') => `<testsuites name="bun test"><testsuite name="${file}" file="${file}">${cases}</testsuite></testsuites>`;
const pass = (name: string) => `<testcase name="${name}" classname="" file="a.test.ts"/>`;
const fail = (name: string) => `<testcase name="${name}" classname="" file="a.test.ts"><failure message="boom"/></testcase>`;
const skip = (name: string) => `<testcase name="${name}" classname="" file="a.test.ts"><skipped/></testcase>`;
const Q = new Set(['a.test.ts::::q']);
const run = (f: ReturnType<typeof fakeRun>, extra: Partial<Parameters<typeof runFlaky>[0]> = {}) =>
runFlaky({ command: ['bun', 'test'], junit: 'r.xml', maxReruns: 3, cwd: tmp(), spawn: f.spawn, env: {}, now: () => NOW, quarantined: Q, ...extra });
Expand Down Expand Up @@ -307,6 +308,34 @@ describe('the quarantine attempt budget under the full green verdict', () => {
expect(renderResult(r, '/l').at(-1)).toContain('failed=0 flaky=0 quarantined=1');
});

test('a quarantined test that never runs is a quarantined-fail, not a silent pass', async () => {
const f = fakeRun(() => suite(skip('q') + pass('a')), () => 0);
const r = await run(f);
expect(f.calls).toHaveLength(1);
expect(r.exitCode).toBe(1);
expect(r.outcomes).toMatchObject([{ status: 'quarantined-fail', attempts: ['skip'], reason: 'quarantined, and skipped on the final attempt instead of running' }]);
expect(r.ledger).toMatchObject([{ kind: 'quarantined-fail', id: 'a.test.ts::::q', attempts: ['skip'] }]);
expect(renderResult(r, '/l').at(-1)).toBe('FLAKY_RESULT: tests=2 failed=1 flaky=0 quarantined=1 reruns=0 command_exit=0 verdict=fail ledger=/l');
});

test('a quarantined test skipped on the whole-command retry is not a clean pass', async () => {
const f = fakeRun(n => n === 1 ? suite(pass('q') + fail('a')) : suite(skip('q') + pass('a')), n => n === 1 ? 1 : 0);
const r = await run(f);
expect(f.calls).toHaveLength(2);
expect(r.exitCode).toBe(1);
expect(r.outcomes.find(o => o.test.id.endsWith('::q'))).toMatchObject({ status: 'quarantined-fail', attempts: ['pass', 'skip'] });
expect(r.ledger.find(l => l.id.endsWith('::q'))).toMatchObject({ kind: 'quarantined-fail', attempts: ['pass', 'skip'] });
expect(r.outcomes.find(o => o.test.id.endsWith('::a'))).toMatchObject({ status: 'flaky-pass', attempts: ['fail', 'pass'] });
});

test('a quarantined test skipped first but passing on the whole-command retry counts as a pass', async () => {
const f = fakeRun(n => n === 1 ? suite(skip('q') + fail('a')) : suite(pass('q') + pass('a')), n => n === 1 ? 1 : 0);
const r = await run(f);
expect(f.calls).toHaveLength(2);
expect(r.exitCode).toBe(0);
expect(r.outcomes.find(o => o.test.id.endsWith('::q'))).toMatchObject({ status: 'quarantined-pass', attempts: ['skip', 'pass'] });
});

test('quarantined failures do not count against --max-reruns', async () => {
const f = fakeRun(n => suite(pass('a') + (n === 1 ? fail('q') : pass('q'))), n => n === 1 ? 1 : 0);
expect((await run(f, { maxReruns: 0 })).exitCode).toBe(0);
Expand Down
Loading