|
| 1 | +/* eslint-disable no-lone-blocks */ |
| 2 | + |
| 3 | +import { expect, test } from 'vitest' |
| 4 | + |
| 5 | +test('expect.* allows asymmetrict mattchers with different types', () => { |
| 6 | + // types.ts examples: stringContaining |
| 7 | + expect('I have an apple').toEqual(expect.stringContaining('apple')) |
| 8 | + expect('I have an apple').toEqual<string>(expect.stringContaining('apple')) |
| 9 | + |
| 10 | + expect({ a: 'test string' }).toEqual({ a: expect.stringContaining('test') }) |
| 11 | + expect({ a: 'test string' }).toEqual<{ a: string }>({ a: expect.stringContaining('test') }) |
| 12 | + |
| 13 | + // types.ts examples: objectContaining |
| 14 | + expect({ a: '1', b: 2 }).toEqual(expect.objectContaining({ a: '1' })) |
| 15 | + expect({ a: '1', b: 2 }).toEqual<{ a: string; b: string }>(expect.objectContaining({ a: '1' })) |
| 16 | + |
| 17 | + // types.ts examples: arrayContaining |
| 18 | + expect(['a', 'b', 'c']).toEqual(expect.arrayContaining(['b', 'a'])) |
| 19 | + expect(['a', 'b', 'c']).toEqual<string[]>(expect.arrayContaining(['b', 'a'])) |
| 20 | + |
| 21 | + // types.ts examples: stringMatching |
| 22 | + expect('hello world').toEqual(expect.stringMatching(/^hello/)) |
| 23 | + expect('hello world').toEqual<string>(expect.stringMatching(/^hello/)) |
| 24 | + |
| 25 | + expect('hello world').toEqual(expect.stringMatching('hello')) |
| 26 | + expect('hello world').toEqual<string>(expect.stringMatching('hello')) |
| 27 | + |
| 28 | + // types.ts examples: closeTo |
| 29 | + expect(10.45).toEqual(expect.closeTo(10.5, 1)) |
| 30 | + expect(10.45).toEqual<number>(expect.closeTo(10.5, 1)) |
| 31 | + |
| 32 | + expect(5.11).toEqual(expect.closeTo(5.12)) |
| 33 | + expect(5.11).toEqual<number>(expect.closeTo(5.12)) |
| 34 | + |
| 35 | + // expect.any(String) |
| 36 | + // https://github.com/vitest-dev/vitest/pull/7016#issuecomment-2517674066 |
| 37 | + { |
| 38 | + const obj = { |
| 39 | + id: '', |
| 40 | + name: '', |
| 41 | + } |
| 42 | + |
| 43 | + expect(obj).toEqual({ |
| 44 | + id: expect.any(String), |
| 45 | + name: 'Amelia', |
| 46 | + }) |
| 47 | + |
| 48 | + expect(obj).toEqual<{ |
| 49 | + id: string |
| 50 | + name: string |
| 51 | + }>({ |
| 52 | + id: expect.any(String), |
| 53 | + name: 'Amelia', |
| 54 | + }) |
| 55 | + } |
| 56 | + |
| 57 | + // expect.any(Date) |
| 58 | + // https://github.com/vitest-dev/vitest/issues/4543#issuecomment-1817960296 |
| 59 | + // https://github.com/vitest-dev/vitest/issues/4543#issuecomment-1817967628 |
| 60 | + // https://github.com/DefinitelyTyped/DefinitelyTyped/pull/62831#issue-1418959169 |
| 61 | + { |
| 62 | + const actual = {} as { |
| 63 | + foo: string |
| 64 | + bar: string |
| 65 | + createdAt: Date |
| 66 | + } |
| 67 | + |
| 68 | + expect(actual).toEqual({ |
| 69 | + foo: 'foo', |
| 70 | + bar: 'bar', |
| 71 | + createdAt: expect.any(Date), |
| 72 | + }) |
| 73 | + |
| 74 | + expect(actual).toEqual<{ |
| 75 | + foo: string |
| 76 | + bar: string |
| 77 | + createdAt: Date |
| 78 | + }>({ |
| 79 | + foo: 'foo', |
| 80 | + bar: 'bar', |
| 81 | + createdAt: expect.any(Date), |
| 82 | + }) |
| 83 | + |
| 84 | + expect(actual).toEqual<{ |
| 85 | + foo: string |
| 86 | + bar: string |
| 87 | + createdAt: Date |
| 88 | + }[]>([ |
| 89 | + { |
| 90 | + foo: 'foo', |
| 91 | + bar: 'bar', |
| 92 | + createdAt: expect.any(Date), |
| 93 | + }, |
| 94 | + ]) |
| 95 | + } |
| 96 | + |
| 97 | + // expect.arrayContaining |
| 98 | + // https://github.com/jestjs/jest/issues/13812#issue-1555843276 |
| 99 | + { |
| 100 | + expect([1, 2, 3]).toEqual(expect.arrayContaining(['a'])) |
| 101 | + expect([1, 2, 3]).toEqual<number[]>(expect.arrayContaining(['a'])) |
| 102 | + |
| 103 | + expect([1, 2, 3]).toEqual(expect.arrayContaining([expect.any(Number)])) |
| 104 | + expect([1, 2, 3]).toEqual<number[]>(expect.arrayContaining([expect.any(Number)])) |
| 105 | + |
| 106 | + expect([1, 2, 3]).toEqual(expect.arrayContaining([expect.anything()])) |
| 107 | + expect([1, 2, 3]).toEqual<number[]>(expect.arrayContaining([expect.anything()])) |
| 108 | + } |
| 109 | + |
| 110 | + // expect.any(Array) |
| 111 | + // https://github.com/DefinitelyTyped/DefinitelyTyped/pull/62831/files#diff-ff7b882e4a29e7fe0e348a6bdf8b11774d606eaa221009b166b01389576d921fR1237 |
| 112 | + expect({ list: [1, 2, 3] }).toMatchObject({ list: expect.any(Array) }) |
| 113 | + expect({ list: [1, 2, 3] }).toMatchObject<{ list: number[] }>({ list: expect.any(Array) }) |
| 114 | +}) |
0 commit comments