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

test: not to display obsoleted snapshots when test with -t #8122

Merged
merged 2 commits into from
Oct 16, 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
2 changes: 1 addition & 1 deletion packages/rspack-test-tools/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const config = {
"@rspack/test-tools/setup-env"
],
reporters: [
["default", null],
["../../scripts/test/ignore-snapshot-default-reporter.cjs", null],
"../../scripts/test/ignore-snapshot-summary-reporter.cjs"
],
testTimeout: process.env.CI ? 60000 : 30000,
Expand Down
84 changes: 84 additions & 0 deletions scripts/test/ignore-snapshot-default-reporter.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
const { DefaultReporter } = require("@jest/reporters");
const chalk = require.cache[require.resolve('@jest/reporters')].require("chalk");
const jestUtil = require.cache[require.resolve('@jest/reporters')].require("jest-util");

const ARROW = ' \u203A ';
const DOT = ' \u2022 ';
const FAIL_COLOR = chalk.bold.red;
const SNAPSHOT_ADDED = chalk.bold.green;
const SNAPSHOT_UPDATED = chalk.bold.green;
const SNAPSHOT_OUTDATED = chalk.bold.yellow;
function getSnapshotStatus(snapshot, afterUpdate) {
const statuses = [];
if (snapshot.added) {
statuses.push(
SNAPSHOT_ADDED(
`${ARROW + jestUtil().pluralize('snapshot', snapshot.added)
} written.`
)
);
}
if (snapshot.updated) {
statuses.push(
SNAPSHOT_UPDATED(
`${ARROW + jestUtil().pluralize('snapshot', snapshot.updated)
} updated.`
)
);
}
if (snapshot.unmatched) {
statuses.push(
FAIL_COLOR(
`${ARROW + jestUtil().pluralize('snapshot', snapshot.unmatched)
} failed.`
)
);
}
if (snapshot.unchecked) {
if (afterUpdate) {
statuses.push(
SNAPSHOT_UPDATED(
`${ARROW + jestUtil().pluralize('snapshot', snapshot.unchecked)
} removed.`
)
);
for (const key of snapshot.uncheckedKeys) {
statuses.push(` ${DOT}${key}`);
}
} else {
statuses.push(
`${SNAPSHOT_OUTDATED(
`${ARROW + jestUtil().pluralize('snapshot', snapshot.unchecked)
} obsolete`
)}.`
);
}
}
if (snapshot.fileDeleted) {
statuses.push(SNAPSHOT_UPDATED(`${ARROW}snapshot file removed.`));
}
return statuses;
}

const isUpdate = process.argv.includes("-u") || process.argv.includes("--updateSnapshot");
const isFiltering = process.argv.includes("-t") || process.argv.includes("--testNamePattern");
const isVerbose = process.argv.includes("--verbose");

if (!isVerbose && !isUpdate && isFiltering) {
class IgnoreSnapshotDefaultReporter extends DefaultReporter {
printTestFileFailureMessage(_testPath, _config, result) {
if (result.failureMessage) {
this.log(result.failureMessage);
}
const didUpdate = this._globalConfig.updateSnapshot === 'all';
const snapshotStatuses = getSnapshotStatus(
result.snapshot,
didUpdate
);
snapshotStatuses.forEach(this.log);
}
}
module.exports = IgnoreSnapshotDefaultReporter;
} else {
module.exports = DefaultReporter;
}
6 changes: 5 additions & 1 deletion scripts/test/ignore-snapshot-summary-reporter.cjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
const { SummaryReporter } = require("@jest/reporters");
const chalk = require.cache[require.resolve('@jest/reporters')].require("chalk");

if (!process.argv.includes("--verbose") && (process.argv.includes("-t") || process.argv.some(i => i.includes("--testNamePattern")))) {
const isUpdate = process.argv.includes("-u") || process.argv.includes("--updateSnapshot");
const isFiltering = process.argv.includes("-t") || process.argv.includes("--testNamePattern");
const isVerbose = process.argv.includes("--verbose");

if (!isVerbose && !isUpdate && isFiltering) {
class IgnoreSnapshotSummaryReporter extends SummaryReporter {
_printSnapshotSummary(snapshots, globalConfig) {
if (
Expand Down
6 changes: 5 additions & 1 deletion tests/webpack-test/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,5 +148,9 @@ module.exports = {
"snapshotFormat": {
"escapeString": true,
"printBasicPrototype": true
}
},
"reporters": [
["../../scripts/test/ignore-snapshot-default-reporter.cjs", null],
"../../scripts/test/ignore-snapshot-summary-reporter.cjs"
],
}
Loading