MCPcopy
hub / github.com/webpack/webpack / matchKindSnapshot

Function matchKindSnapshot

test/harness/snapshot/index.js:132–175  ·  view source on GitHub ↗
(caseDir, kind, received)

Source from the content-addressed store, hash-verified

130 * @param {EXPECTED_ANY} received The value to match against the snapshot
131 */
132const matchKindSnapshot = function (caseDir, kind, received) {
133 const snapshotPath = path.join(caseDir, "__snapshots__", `${kind}.snap`);
134 const parentState =
135 getActiveSnapshotState() || expect.getState().snapshotState;
136 const ps = /** @type {EXPECTED_ANY} */ (parentState);
137
138 const kindState = new SnapshotState(snapshotPath, {
139 updateSnapshot: ps._updateSnapshot === "all" ? "all" : "none",
140 snapshotFormat: parentState.snapshotFormat,
141 expand: parentState.expand,
142 prettierPath: ps._prettierPath,
143 rootDir: ps._rootDir || process.cwd()
144 });
145
146 // Call jest-snapshot's toMatchSnapshot directly with a controlled
147 // matcher context. Using `kind` as currentTestName produces the
148 // stable key "<kind> 1" (e.g. "errors 1"), independent of suite.
149 const { toMatchSnapshot } = require("jest-snapshot");
150 const context = /** @type {EXPECTED_ANY} */ ({
151 snapshotState: kindState,
152 currentTestName: kind,
153 isNot: false,
154 promise: "",
155 utils: require("jest-matcher-utils"),
156 expand: false
157 });
158
159 try {
160 const result = /** @type {import("expect").SyncExpectationResult} */ (
161 toMatchSnapshot.call(context, received)
162 );
163 if (!result.pass) {
164 throw new Error(result.message());
165 }
166 } finally {
167 kindState.save();
168
169 // Bubble counts up to the parent state so Jest reports them.
170 parentState.unmatched += kindState.unmatched;
171 parentState.matched += kindState.matched;
172 parentState.updated += kindState.updated;
173 parentState.added += kindState.added;
174 }
175};
176
177module.exports.getActiveSnapshotState = getActiveSnapshotState;
178module.exports.registerPerCaseSnapshotHooks = registerPerCaseSnapshotHooks;

Callers 1

Calls 4

getActiveSnapshotStateFunction · 0.85
messageMethod · 0.80
requireFunction · 0.50
callMethod · 0.45

Tested by

no test coverage detected