(caseDir, kind, received)
| 130 | * @param {EXPECTED_ANY} received The value to match against the snapshot |
| 131 | */ |
| 132 | const 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 | |
| 177 | module.exports.getActiveSnapshotState = getActiveSnapshotState; |
| 178 | module.exports.registerPerCaseSnapshotHooks = registerPerCaseSnapshotHooks; |
no test coverage detected