()
| 120 | } |
| 121 | |
| 122 | get snap(): snapProperty<unknown, AssertionKind> { |
| 123 | // Use variadic args to distinguish undefined being passed explicitly from no args |
| 124 | const inline = (...args: unknown[]) => { |
| 125 | const snapName = this.ctx.lastSnapName ?? "snap" |
| 126 | const expectedSerialized = snapshot(args[0]) |
| 127 | if (!args.length || this.ctx.cfg.updateSnapshots) { |
| 128 | const position = caller() |
| 129 | if (this.ctx.cfg.failOnMissingSnapshots) { |
| 130 | throw new MissingSnapshotError( |
| 131 | `.${snapName}() at ${positionToString(position)} must be populated.` |
| 132 | ) |
| 133 | } |
| 134 | if (this.snapRequiresUpdate(expectedSerialized)) { |
| 135 | const snapshotArgs: SnapshotArgs = { |
| 136 | position, |
| 137 | serializedValue: this.serializedActual, |
| 138 | snapFunctionName: snapName |
| 139 | } |
| 140 | queueSnapshotUpdate(snapshotArgs) |
| 141 | } |
| 142 | } else { |
| 143 | // compare as strings, but if match fails, compare again as objects |
| 144 | // to give a clearer error message. This avoid problems with objects |
| 145 | // like subtypes of array that do not pass node's deep equality test |
| 146 | // but serialize to the same value. |
| 147 | if (printable(args[0]) !== printable(this.unversionedActual)) |
| 148 | assertEquals(expectedSerialized, this.serializedActual, this.ctx) |
| 149 | } |
| 150 | return this |
| 151 | } |
| 152 | const toFile = (id: string, opts?: ExternalSnapshotOptions) => { |
| 153 | const expectedSnapshot = getSnapshotByName( |
| 154 | this.ctx.position.file, |
| 155 | id, |
| 156 | opts?.path |
| 157 | ) |
| 158 | if (!expectedSnapshot || this.ctx.cfg.updateSnapshots) { |
| 159 | if (this.snapRequiresUpdate(expectedSnapshot)) { |
| 160 | updateExternalSnapshot({ |
| 161 | serializedValue: this.serializedActual, |
| 162 | position: caller(), |
| 163 | name: id, |
| 164 | customPath: opts?.path |
| 165 | }) |
| 166 | } |
| 167 | } else assertEquals(expectedSnapshot, this.serializedActual, this.ctx) |
| 168 | |
| 169 | return this |
| 170 | } |
| 171 | return Object.assign(inline, { |
| 172 | toFile, |
| 173 | unwrap: this.unwrap.bind(this) |
| 174 | }) |
| 175 | } |
| 176 | |
| 177 | private immediateOrChained() { |
| 178 | const immediateAssertion = (...args: [expected: unknown]) => { |
no outgoing calls
no test coverage detected