-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: report results correctly when the browser crashes mid-test (#27786)
* report correct number of failures if browser crashes in the middle of a test suite * report failure correctly when browser crashes during test * refactor crash handling * correct exit option check, clean up debug * exit on success if exit option !== false * use default stats when reporter stats are unavailable * fix error messaging * move reporter types to an intermediate .ts file, to be combined with reporter.js when migrated * debug tab close test in ci * move debug env from pkg to ci yml * set debug env in spec * fix pckg * adds some logging to cri-client * remove event emit logging from project-base * revert snapshot for tab close system test * fixes console output for no exit on success * changelog * changelog wsp * cleanup * clean up tests * refactor to more straightforward control flow * rm export for unused type * correct tab close snapshot for ci * new system test for mid-test config crash * update snapshots
- Loading branch information
1 parent
92cc6da
commit 1a6f879
Showing
11 changed files
with
1,790 additions
and
1,506 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,48 @@ | ||
interface ReporterTestAttempt { | ||
state: 'skipped' | 'failed' | 'passed' | ||
error: any | ||
timings: any | ||
failedFromHookId: any | ||
wallClockStartedAt: Date | ||
wallClockDuration: number | ||
videoTimestamp: any | ||
} | ||
interface ReporterTest { | ||
testId: string | ||
title: string[] | ||
state: 'skipped' | 'passed' | 'failed' | ||
body: string | ||
displayError: any | ||
attempts: ReporterTestAttempt[] | ||
} | ||
|
||
export interface BaseReporterResults { | ||
error?: string | ||
stats: { | ||
failures: number | ||
tests: number | ||
passes: number | ||
pending: number | ||
suites: number | ||
skipped: number | ||
wallClockDuration: number | ||
wallClockStartedAt: string | ||
wallClockEndedAt: string | ||
} | ||
} | ||
|
||
export interface ReporterResults extends BaseReporterResults { | ||
reporter: string | ||
reporterStats: { | ||
suites: number | ||
tests: number | ||
passes: number | ||
pending: number | ||
failures: number | ||
start: string | ||
end: string | ||
duration: number | ||
} | ||
hooks: any[] | ||
tests: ReporterTest[] | ||
} |
This file contains 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,115 @@ | ||
import type { ProjectBase } from '../project-base' | ||
import type { BaseReporterResults, ReporterResults } from '../types/reporter' | ||
import * as errors from '../errors' | ||
import Debug from 'debug' | ||
import pDefer, { DeferredPromise } from 'p-defer' | ||
|
||
const debug = Debug('cypress:util:crash_handling') | ||
|
||
const patchRunResultsAfterCrash = (error: Error, reporterResults: ReporterResults, mostRecentRunnable: any): ReporterResults => { | ||
const endTime: number = reporterResults?.stats?.wallClockEndedAt ? Date.parse(reporterResults?.stats?.wallClockEndedAt) : new Date().getTime() | ||
const wallClockDuration = reporterResults?.stats?.wallClockStartedAt ? | ||
endTime - Date.parse(reporterResults.stats.wallClockStartedAt) : 0 | ||
const endTimeStamp = new Date(endTime).toJSON() | ||
|
||
// in crash situations, the most recent report will not have the triggering test | ||
// so the results are manually patched, which produces the expected exit=1 and | ||
// terminal output indicating the failed test | ||
return { | ||
...reporterResults, | ||
stats: { | ||
...reporterResults?.stats, | ||
wallClockEndedAt: endTimeStamp, | ||
wallClockDuration, | ||
failures: (reporterResults?.stats?.failures ?? 0) + 1, | ||
skipped: (reporterResults?.stats?.skipped ?? 1) - 1, | ||
}, | ||
reporterStats: { | ||
...reporterResults?.reporterStats, | ||
tests: (reporterResults?.reporterStats?.tests ?? 0) + 1, // crashed test does not increment this value | ||
end: reporterResults?.reporterStats?.end || endTimeStamp, | ||
duration: wallClockDuration, | ||
failures: (reporterResults?.reporterStats?.failures ?? 0) + 1, | ||
}, | ||
tests: (reporterResults?.tests || []).map((test) => { | ||
if (test.testId === mostRecentRunnable.id) { | ||
return { | ||
...test, | ||
state: 'failed', | ||
attempts: [ | ||
...test.attempts.slice(0, -1), | ||
{ | ||
...test.attempts[test.attempts.length - 1], | ||
state: 'failed', | ||
}, | ||
], | ||
} | ||
} | ||
|
||
return test | ||
}), | ||
error: errors.stripAnsi(error.message), | ||
} | ||
} | ||
|
||
const defaultStats = (error: Error): BaseReporterResults => { | ||
return { | ||
error: errors.stripAnsi(error.message), | ||
stats: { | ||
failures: 1, | ||
tests: 0, | ||
passes: 0, | ||
pending: 0, | ||
suites: 0, | ||
skipped: 0, | ||
wallClockDuration: 0, | ||
wallClockStartedAt: new Date().toJSON(), | ||
wallClockEndedAt: new Date().toJSON(), | ||
}, | ||
} | ||
} | ||
|
||
export class EarlyExitTerminator { | ||
private terminator: DeferredPromise<BaseReporterResults> | ||
|
||
private pendingRunnable: any | ||
private intermediateStats: ReporterResults | undefined | ||
|
||
constructor () { | ||
this.terminator = pDefer<BaseReporterResults>() | ||
} | ||
|
||
waitForEarlyExit (project: ProjectBase, exit?: boolean) { | ||
debug('waiting for early exit') | ||
|
||
project.on('test:before:run', ({ | ||
runnable, | ||
previousResults, | ||
}) => { | ||
debug('preparing to run test, previous stats reported as %O', previousResults) | ||
|
||
this.intermediateStats = previousResults | ||
this.pendingRunnable = runnable | ||
}) | ||
|
||
return this.terminator.promise | ||
} | ||
|
||
exitEarly (error) { | ||
if (error.isFatalApiErr) { | ||
this.terminator.reject(error) | ||
|
||
return | ||
} | ||
|
||
// eslint-disable-next-line no-console | ||
console.log('') | ||
errors.log(error) | ||
|
||
const runResults: BaseReporterResults = (this.intermediateStats && this.pendingRunnable) ? | ||
patchRunResultsAfterCrash(error, this.intermediateStats, this.pendingRunnable) : | ||
defaultStats(error) | ||
|
||
this.terminator.resolve(runResults) | ||
} | ||
} |
Oops, something went wrong.
1a6f879
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Circle has built the
linux x64
version of the Test Runner.Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version
Run this command to install the pre-release locally:
1a6f879
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Circle has built the
linux arm64
version of the Test Runner.Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version
Run this command to install the pre-release locally:
1a6f879
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Circle has built the
darwin x64
version of the Test Runner.Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version
Run this command to install the pre-release locally:
1a6f879
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Circle has built the
darwin arm64
version of the Test Runner.Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version
Run this command to install the pre-release locally:
1a6f879
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Circle has built the
win32 x64
version of the Test Runner.Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version
Run this command to install the pre-release locally: