|
5 | 5 | * LICENSE file in the root directory of this source tree. |
6 | 6 | */ |
7 | 7 |
|
8 | | -import {expectError} from 'tsd-lite'; |
9 | | -import type * as expect from 'expect'; |
| 8 | +import {expectError, expectType} from 'tsd-lite'; |
| 9 | +import type {EqualsFunction, Tester} from '@jest/expect-utils'; |
| 10 | +import {type Matchers, expect} from 'expect'; |
| 11 | +import type * as jestMatcherUtils from 'jest-matcher-utils'; |
10 | 12 |
|
11 | | -type M = expect.Matchers<void, unknown>; |
12 | | -type N = expect.Matchers<void>; |
| 13 | +type M = Matchers<void, unknown>; |
| 14 | +type N = Matchers<void>; |
13 | 15 |
|
14 | 16 | expectError(() => { |
15 | | - type E = expect.Matchers; |
| 17 | + type E = Matchers; |
16 | 18 | }); |
| 19 | + |
| 20 | +// extend |
| 21 | + |
| 22 | +type MatcherUtils = typeof jestMatcherUtils & { |
| 23 | + iterableEquality: Tester; |
| 24 | + subsetEquality: Tester; |
| 25 | +}; |
| 26 | + |
| 27 | +expectType<void>( |
| 28 | + expect.extend({ |
| 29 | + toBeWithinRange(actual: number, floor: number, ceiling: number) { |
| 30 | + expectType<number>(this.assertionCalls); |
| 31 | + expectType<string | undefined>(this.currentTestName); |
| 32 | + expectType<(() => void) | undefined>(this.dontThrow); |
| 33 | + expectType<Error | undefined>(this.error); |
| 34 | + expectType<EqualsFunction>(this.equals); |
| 35 | + expectType<boolean | undefined>(this.expand); |
| 36 | + expectType<number | null | undefined>(this.expectedAssertionsNumber); |
| 37 | + expectType<Error | undefined>(this.expectedAssertionsNumberError); |
| 38 | + expectType<boolean | undefined>(this.isExpectingAssertions); |
| 39 | + expectType<Error | undefined>(this.isExpectingAssertionsError); |
| 40 | + expectType<boolean>(this.isNot); |
| 41 | + expectType<string>(this.promise); |
| 42 | + expectType<Array<Error>>(this.suppressedErrors); |
| 43 | + expectType<string | undefined>(this.testPath); |
| 44 | + expectType<MatcherUtils>(this.utils); |
| 45 | + |
| 46 | + const pass = actual >= floor && actual <= ceiling; |
| 47 | + if (pass) { |
| 48 | + return { |
| 49 | + message: () => |
| 50 | + `expected ${actual} not to be within range ${floor} - ${ceiling}`, |
| 51 | + pass: true, |
| 52 | + }; |
| 53 | + } else { |
| 54 | + return { |
| 55 | + message: () => |
| 56 | + `expected ${actual} to be within range ${floor} - ${ceiling}`, |
| 57 | + pass: false, |
| 58 | + }; |
| 59 | + } |
| 60 | + }, |
| 61 | + }), |
| 62 | +); |
| 63 | + |
| 64 | +declare module 'expect' { |
| 65 | + interface AsymmetricMatchers { |
| 66 | + toBeWithinRange(floor: number, ceiling: number): void; |
| 67 | + } |
| 68 | + interface Matchers<R> { |
| 69 | + toBeWithinRange(floor: number, ceiling: number): void; |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +expectType<void>(expect(100).toBeWithinRange(90, 110)); |
| 74 | +expectType<void>(expect(101).not.toBeWithinRange(0, 100)); |
| 75 | + |
| 76 | +expectType<void>( |
| 77 | + expect({apples: 6, bananas: 3}).toEqual({ |
| 78 | + apples: expect.toBeWithinRange(1, 10), |
| 79 | + bananas: expect.not.toBeWithinRange(11, 20), |
| 80 | + }), |
| 81 | +); |
0 commit comments