|
| 1 | +import type { Tester } from '@vitest/expect' |
1 | 2 | import { getCurrentTest } from '@vitest/runner'
|
2 |
| -import { describe, expect, expectTypeOf, test } from 'vitest' |
| 3 | +import { describe, expect, expectTypeOf, test, vi } from 'vitest' |
3 | 4 |
|
4 | 5 | describe('expect.soft', () => {
|
5 | 6 | test('types', () => {
|
@@ -40,3 +41,233 @@ describe('expect.soft', () => {
|
40 | 41 | expect.soft('test3').toBe('test res')
|
41 | 42 | })
|
42 | 43 | })
|
| 44 | + |
| 45 | +describe('expect.addEqualityTesters', () => { |
| 46 | + class AnagramComparator { |
| 47 | + public word: string |
| 48 | + |
| 49 | + constructor(word: string) { |
| 50 | + this.word = word |
| 51 | + } |
| 52 | + |
| 53 | + equals(other: AnagramComparator): boolean { |
| 54 | + const cleanStr1 = this.word.replace(/ /g, '').toLowerCase() |
| 55 | + const cleanStr2 = other.word.replace(/ /g, '').toLowerCase() |
| 56 | + |
| 57 | + const sortedStr1 = cleanStr1.split('').sort().join('') |
| 58 | + const sortedStr2 = cleanStr2.split('').sort().join('') |
| 59 | + |
| 60 | + return sortedStr1 === sortedStr2 |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + function createAnagramComparator(word: string) { |
| 65 | + return new AnagramComparator(word) |
| 66 | + } |
| 67 | + |
| 68 | + function isAnagramComparator(a: unknown): a is AnagramComparator { |
| 69 | + return a instanceof AnagramComparator |
| 70 | + } |
| 71 | + |
| 72 | + const areObjectsEqual: Tester = ( |
| 73 | + a: unknown, |
| 74 | + b: unknown, |
| 75 | + ): boolean | undefined => { |
| 76 | + const isAAnagramComparator = isAnagramComparator(a) |
| 77 | + const isBAnagramComparator = isAnagramComparator(b) |
| 78 | + |
| 79 | + if (isAAnagramComparator && isBAnagramComparator) |
| 80 | + return a.equals(b) |
| 81 | + |
| 82 | + else if (isAAnagramComparator === isBAnagramComparator) |
| 83 | + return undefined |
| 84 | + |
| 85 | + else |
| 86 | + return false |
| 87 | + } |
| 88 | + |
| 89 | + function* toIterator<T>(array: Array<T>): Iterator<T> { |
| 90 | + for (const obj of array) |
| 91 | + yield obj |
| 92 | + } |
| 93 | + |
| 94 | + const customObject1 = createAnagramComparator('listen') |
| 95 | + const customObject2 = createAnagramComparator('silent') |
| 96 | + |
| 97 | + expect.addEqualityTesters([areObjectsEqual]) |
| 98 | + |
| 99 | + test('AnagramComparator objects are unique and not contained within arrays of AnagramComparator objects', () => { |
| 100 | + expect(customObject1).not.toBe(customObject2) |
| 101 | + expect([customObject1]).not.toContain(customObject2) |
| 102 | + }) |
| 103 | + |
| 104 | + test('basic matchers pass different AnagramComparator objects', () => { |
| 105 | + expect(customObject1).toEqual(customObject2) |
| 106 | + expect([customObject1, customObject2]).toEqual([customObject2, customObject1]) |
| 107 | + expect(new Map([['key', customObject1]])).toEqual(new Map([['key', customObject2]])) |
| 108 | + expect(new Set([customObject1])).toEqual(new Set([customObject2])) |
| 109 | + expect(toIterator([customObject1, customObject2])).toEqual( |
| 110 | + toIterator([customObject2, customObject1]), |
| 111 | + ) |
| 112 | + expect([customObject1]).toContainEqual(customObject2) |
| 113 | + expect({ a: customObject1 }).toHaveProperty('a', customObject2) |
| 114 | + expect({ a: customObject2, b: undefined }).toStrictEqual({ |
| 115 | + a: customObject1, |
| 116 | + b: undefined, |
| 117 | + }) |
| 118 | + expect({ a: 1, b: { c: customObject1 } }).toMatchObject({ |
| 119 | + a: 1, |
| 120 | + b: { c: customObject2 }, |
| 121 | + }) |
| 122 | + }) |
| 123 | + |
| 124 | + test('asymmetric matchers pass different AnagramComparator objects', () => { |
| 125 | + expect([customObject1]).toEqual(expect.arrayContaining([customObject1])) |
| 126 | + expect({ a: 1, b: { c: customObject1 } }).toEqual( |
| 127 | + expect.objectContaining({ b: { c: customObject2 } }), |
| 128 | + ) |
| 129 | + }) |
| 130 | + |
| 131 | + test('toBe recommends toStrictEqual even with different objects', () => { |
| 132 | + expect(() => expect(customObject1).toBe(customObject2)).toThrow('toStrictEqual') |
| 133 | + }) |
| 134 | + |
| 135 | + test('toBe recommends toEqual even with different AnagramComparator objects', () => { |
| 136 | + expect(() => expect({ a: undefined, b: customObject1 }).toBe({ b: customObject2 })).toThrow( |
| 137 | + 'toEqual', |
| 138 | + ) |
| 139 | + }) |
| 140 | + |
| 141 | + test('iterableEquality still properly detects cycles', () => { |
| 142 | + const a = new Set() |
| 143 | + a.add(customObject1) |
| 144 | + a.add(a) |
| 145 | + |
| 146 | + const b = new Set() |
| 147 | + b.add(customObject2) |
| 148 | + b.add(b) |
| 149 | + |
| 150 | + expect(a).toEqual(b) |
| 151 | + }) |
| 152 | +}) |
| 153 | + |
| 154 | +describe('recursive custom equality tester', () => { |
| 155 | + let personId = 0 |
| 156 | + |
| 157 | + class Address { |
| 158 | + public address: string |
| 159 | + |
| 160 | + constructor(address: string) { |
| 161 | + this.address = address |
| 162 | + } |
| 163 | + } |
| 164 | + class Person { |
| 165 | + public name: string |
| 166 | + public address: Address |
| 167 | + public personId: string |
| 168 | + |
| 169 | + constructor(name: string, address: Address) { |
| 170 | + this.name = name |
| 171 | + this.address = address |
| 172 | + this.personId = `${personId++}` |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + const arePersonsEqual: Tester = function (a, b, customTesters) { |
| 177 | + const isAPerson = a instanceof Person |
| 178 | + const isBPerson = b instanceof Person |
| 179 | + |
| 180 | + if (isAPerson && isBPerson) |
| 181 | + return a.name === b.name && this.equals(a.address, b.address, customTesters) |
| 182 | + |
| 183 | + else if (isAPerson === isBPerson) |
| 184 | + return undefined |
| 185 | + |
| 186 | + else |
| 187 | + return false |
| 188 | + } |
| 189 | + |
| 190 | + const areAddressesEqual: Tester = (a, b) => { |
| 191 | + const isAAddress = a instanceof Address |
| 192 | + const isBAddress = b instanceof Address |
| 193 | + |
| 194 | + if (isAAddress && isBAddress) |
| 195 | + return a.address === b.address |
| 196 | + |
| 197 | + else if (isAAddress === isBAddress) |
| 198 | + return undefined |
| 199 | + |
| 200 | + else |
| 201 | + return false |
| 202 | + } |
| 203 | + |
| 204 | + const person1 = new Person('Luke Skywalker', new Address('Tatooine')) |
| 205 | + const person2 = new Person('Luke Skywalker', new Address('Tatooine')) |
| 206 | + |
| 207 | + expect.addEqualityTesters([areAddressesEqual, arePersonsEqual]) |
| 208 | + |
| 209 | + test('basic matchers pass different Address objects', () => { |
| 210 | + expect(person1).not.toBe(person2) |
| 211 | + expect([person1]).not.toContain(person2) |
| 212 | + expect(person1).toEqual(person1) |
| 213 | + expect(person1).toEqual(person2) |
| 214 | + expect([person1, person2]).toEqual([person2, person1]) |
| 215 | + expect(new Map([['key', person1]])).toEqual(new Map([['key', person2]])) |
| 216 | + expect(new Set([person1])).toEqual(new Set([person2])) |
| 217 | + expect([person1]).toContainEqual(person2) |
| 218 | + expect({ a: person1 }).toHaveProperty('a', person2) |
| 219 | + expect({ a: person1, b: undefined }).toStrictEqual({ |
| 220 | + a: person2, |
| 221 | + b: undefined, |
| 222 | + }) |
| 223 | + expect({ a: 1, b: { c: person1 } }).toMatchObject({ |
| 224 | + a: 1, |
| 225 | + b: { c: person2 }, |
| 226 | + }) |
| 227 | + }) |
| 228 | + |
| 229 | + test('asymmetric matchers pass different Address objects', () => { |
| 230 | + expect([person1]).toEqual(expect.arrayContaining([person2])) |
| 231 | + expect({ a: 1, b: { c: person1 } }).toEqual( |
| 232 | + expect.objectContaining({ b: { c: person2 } }), |
| 233 | + ) |
| 234 | + }) |
| 235 | + |
| 236 | + test('toBe recommends toStrictEqual even with different Address objects', () => { |
| 237 | + expect(() => expect(person1).toBe(person2)).toThrow('toStrictEqual') |
| 238 | + }) |
| 239 | + |
| 240 | + test('toBe recommends toEqual even with different Address objects', () => { |
| 241 | + expect(() => expect({ a: undefined, b: person1 }).toBe({ b: person2 })).toThrow( |
| 242 | + 'toEqual', |
| 243 | + ) |
| 244 | + }) |
| 245 | + |
| 246 | + test('iterableEquality still properly detects cycles', () => { |
| 247 | + const a = new Set() |
| 248 | + a.add(person1) |
| 249 | + a.add(a) |
| 250 | + |
| 251 | + const b = new Set() |
| 252 | + b.add(person2) |
| 253 | + b.add(b) |
| 254 | + |
| 255 | + expect(a).toEqual(b) |
| 256 | + }) |
| 257 | + |
| 258 | + test('spy matchers pass different Person objects', () => { |
| 259 | + const mockFn = vi.fn( |
| 260 | + (person: Person) => [person, person2], |
| 261 | + ) |
| 262 | + mockFn(person1) |
| 263 | + |
| 264 | + expect(mockFn).toHaveBeenCalledWith(person1) |
| 265 | + expect(mockFn).toHaveBeenCalledWith(person1) |
| 266 | + expect(mockFn).toHaveBeenLastCalledWith(person1) |
| 267 | + expect(mockFn).toHaveBeenNthCalledWith(1, person1) |
| 268 | + |
| 269 | + expect(mockFn).toHaveReturnedWith([person1, person2]) |
| 270 | + expect(mockFn).toHaveLastReturnedWith([person1, person2]) |
| 271 | + expect(mockFn).toHaveNthReturnedWith(1, [person1, person2]) |
| 272 | + }) |
| 273 | +}) |
0 commit comments