Skip to content

Commit

Permalink
test: not to display obsoleted snapshots when test with -t (#8122)
Browse files Browse the repository at this point in the history
  • Loading branch information
LingyuCoder authored Oct 16, 2024
1 parent 4ea491e commit 5fe525b
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 3 deletions.
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"
],
}

2 comments on commit 5fe525b

@rspack-bot
Copy link

Choose a reason for hiding this comment

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

📝 Benchmark detail: Open

Name Base (2024-10-16 4ea491e) Current Change
10000_development-mode + exec 2.11 s ± 23 ms 2.11 s ± 38 ms +0.32 %
10000_development-mode_hmr + exec 662 ms ± 17 ms 656 ms ± 5.1 ms -0.86 %
10000_production-mode + exec 2.64 s ± 19 ms 2.64 s ± 27 ms +0.04 %
arco-pro_development-mode + exec 1.79 s ± 88 ms 1.77 s ± 60 ms -1.36 %
arco-pro_development-mode_hmr + exec 426 ms ± 1.5 ms 426 ms ± 2.5 ms +0.19 %
arco-pro_production-mode + exec 3.07 s ± 44 ms 3.06 s ± 69 ms -0.39 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.13 s ± 84 ms 3.18 s ± 61 ms +1.42 %
threejs_development-mode_10x + exec 1.61 s ± 22 ms 1.61 s ± 19 ms -0.50 %
threejs_development-mode_10x_hmr + exec 759 ms ± 9.2 ms 758 ms ± 5.1 ms -0.11 %
threejs_production-mode_10x + exec 4.9 s ± 23 ms 4.92 s ± 24 ms +0.55 %

@rspack-bot
Copy link

Choose a reason for hiding this comment

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

📝 Ran ecosystem CI: Open

suite result
modernjs ✅ success
_selftest ✅ success
rspress ✅ success
rslib ✅ success
rsbuild ✅ success
examples ✅ success
devserver ❌ failure

Please sign in to comment.