| 121 | } |
| 122 | |
| 123 | match(options: AssertOptions): MatchResult { |
| 124 | const { |
| 125 | filepath, |
| 126 | name, |
| 127 | testId = name, |
| 128 | message, |
| 129 | isInline = false, |
| 130 | properties, |
| 131 | inlineSnapshot, |
| 132 | error, |
| 133 | errorMessage, |
| 134 | rawSnapshot, |
| 135 | assertionName, |
| 136 | } = options |
| 137 | let { received } = options |
| 138 | |
| 139 | if (!filepath) { |
| 140 | throw new Error('Snapshot cannot be used outside of test') |
| 141 | } |
| 142 | |
| 143 | const snapshotState = this.getSnapshotState(filepath) |
| 144 | const testName = [name, ...(message ? [message] : [])].join(' > ') |
| 145 | |
| 146 | // Probe first so we can mark as checked even on early return |
| 147 | const expectedSnapshot = snapshotState.probeExpectedSnapshot({ |
| 148 | testName, |
| 149 | testId, |
| 150 | isInline, |
| 151 | inlineSnapshot, |
| 152 | }) |
| 153 | |
| 154 | if (typeof properties === 'object') { |
| 155 | if (typeof received !== 'object' || !received) { |
| 156 | expectedSnapshot.markAsChecked() |
| 157 | throw new Error( |
| 158 | 'Received value must be an object when the matcher has properties', |
| 159 | ) |
| 160 | } |
| 161 | |
| 162 | let propertiesPass: boolean |
| 163 | try { |
| 164 | propertiesPass = this.options.isEqual?.(received, properties) ?? false |
| 165 | } |
| 166 | catch (err) { |
| 167 | expectedSnapshot.markAsChecked() |
| 168 | throw err |
| 169 | } |
| 170 | if (!propertiesPass) { |
| 171 | expectedSnapshot.markAsChecked() |
| 172 | return { |
| 173 | pass: false, |
| 174 | message: () => errorMessage || 'Snapshot properties mismatched', |
| 175 | actual: received, |
| 176 | expected: properties, |
| 177 | } |
| 178 | } |
| 179 | received = deepMergeSnapshot(received, properties) |
| 180 | } |