Skip to content
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

tests(mocha): fix snapshot expectations in retries #15735

Merged
merged 3 commits into from
Jan 8, 2024
Merged
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
5 changes: 5 additions & 0 deletions core/test/scenarios/__snapshots__/api-test-pptr.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ Array [
"largest-contentful-paint",
"largest-contentful-paint-element",
"layout-shift-elements",
"layout-shifts",
Copy link
Member Author

@adamraine adamraine Jan 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These snapshot updates should have been caught in #15703

Confirmed they are caught here now:
https://github.com/GoogleChrome/lighthouse/actions/runs/7451657749/job/20273235889?pr=15735

"lcp-lazy-loaded",
"legacy-javascript",
"link-in-text-block",
Expand Down Expand Up @@ -454,6 +455,7 @@ Array [
"interaction-to-next-paint",
"is-on-https",
"layout-shift-elements",
"layout-shifts",
"legacy-javascript",
"long-tasks",
"main-thread-tasks",
Expand Down Expand Up @@ -492,6 +494,7 @@ Array [

exports[`Individual modes API startTimespan should compute ConsoleMessage results across a span of time 2`] = `
Array [
"layout-shifts",
"non-composited-animations",
"preload-fonts",
"third-party-summary",
Expand All @@ -515,6 +518,7 @@ Array [
"interaction-to-next-paint",
"is-on-https",
"layout-shift-elements",
"layout-shifts",
"legacy-javascript",
"long-tasks",
"main-thread-tasks",
Expand Down Expand Up @@ -556,6 +560,7 @@ Array [
"bootup-time",
"duplicated-javascript",
"efficient-animated-content",
"layout-shifts",
"legacy-javascript",
"modern-image-formats",
"network-rtt",
Expand Down
14 changes: 13 additions & 1 deletion core/test/test-env/mocha-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function getSnapshotState(testFile) {
const snapshotDir = path.join(path.dirname(testFile), '__snapshots__');
const snapshotFile = path.join(snapshotDir, path.basename(testFile) + '.snap');
snapshotState = new SnapshotState(snapshotFile, {
updateSnapshot: process.env.SNAPSHOT_UPDATE ? 'all' : 'new',
updateSnapshot: process.env.SNAPSHOT_UPDATE ? 'all' : 'none',
prettierPath: '',
snapshotFormat: {},
});
Expand Down Expand Up @@ -98,6 +98,7 @@ expect.extend({

const title = makeTestTitle(test);
const snapshotState = getSnapshotState(test.file);

const context = {snapshotState, currentTestName: title};
// @ts-expect-error - this is enough for snapshots to work.
const matcher = toMatchSnapshot.bind(context);
Expand Down Expand Up @@ -146,6 +147,17 @@ const rootHooks = {

// Needed so `expect` extension method can access information about the current test.
mochaCurrentTest = this.currentTest;

// If a test is retried the snapshot indices will start where the previous attempt left off.
// This can lead to several problems including the test passing where it should be failing.
//
// Jest itself clears the snapshot state on retries although they seem to execute retries after
// all tests finish and not immediately after the initial test failure.
// https://github.com/jestjs/jest/pull/8629
if (this.currentTest.retries() && this.currentTest.file) {
const snapshotState = getSnapshotState(this.currentTest.file);
snapshotState.clear();
}
},
/** @this {Mocha.Context} */
afterEach() {
Expand Down
Loading