Skip to content

Commit dc96981

Browse files
committed
Fix jest global + review customs matchers
1 parent 3c8ac43 commit dc96981

12 files changed

Lines changed: 1135 additions & 1424 deletions

jest.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
declare namespace jest {
99

10-
interface Matchers<R extends void | Promise<void>, T> extends ExpectWebdriverIO.CustomMatchers<R, T> {
10+
interface Matchers<R extends void | Promise<void>, T> extends ExpectWebdriverIO.Matchers<R, T> {
1111

1212
/**
1313
* Below are overloaded Jest's matchers not part of `expect` but of `jest-snapshot`.

test-types/jasmine/customMatchers/customMatchers-namespace.d.ts renamed to test-types/jasmine/customMatchers.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
declare namespace ExpectWebdriverIO {
66
// eslint-disable-next-line @typescript-eslint/no-unused-vars
77
interface Matchers<R, T> {
8-
toBeCustom(): Promise<void>;
8+
toBeCustomWdio(): Promise<void>;
99
}
1010
}
1111

test-types/jasmine/jasmine.test-d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,8 @@ describe('Jasmine type agumentations', () => {
232232
describe('Custom matchers', () => {
233233
describe('using `ExpectWebdriverIO` namespace augmentation', () => {
234234
it('should return Promise<void> for a non-promise custom matcher', async () => {
235-
expectTypeOf(expectAsync('test').toBeCustom()).toEqualTypeOf<Promise<void>>()
236-
expectTypeOf(expectAsync('test').not.toBeCustom()).toEqualTypeOf<Promise<void>>()
235+
expectTypeOf(expectAsync('test').toBeCustomWdio()).toEqualTypeOf<Promise<void>>()
236+
expectTypeOf(expectAsync('test').not.toBeCustomWdio()).toEqualTypeOf<Promise<void>>()
237237

238238
expectTypeOf(expectAsync('test').toBeCustomJasmine()).toEqualTypeOf<Promise<void>>()
239239
expectTypeOf(expectAsync('test').not.toBeCustomJasmine()).toEqualTypeOf<Promise<void>>()
@@ -352,7 +352,7 @@ describe('Jasmine type agumentations', () => {
352352
})
353353

354354
it('should support custom matchers', async () => {
355-
expectTypeOf(wdioExpect('test').toBeCustom()).toEqualTypeOf<Promise<void>>()
355+
expectTypeOf(wdioExpect('test').toBeCustomWdio()).toEqualTypeOf<Promise<void>>()
356356
})
357357

358358
describe('Support soft Assertions on wdioExpect only (not supported on expect global since its Jasmine)', async () => {

test-types/jest-@jest_global/types-jest.test.ts

Lines changed: 311 additions & 443 deletions
Large diffs are not rendered by default.

test-types/jest-@types_jest/customMatchers/customMatchers-module-expect.d.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,25 @@ import 'expect'
22

33
/**
44
* Custom matchers under the `expect` module.
5+
*
6+
* LIMITATION: This augmentation is specifically required for `expect.soft(...)` support.
7+
* It does NOT affect the global `expect(...)` in Jest which uses `namespace jest`.
8+
*
9+
* For universal support, prefer augmenting `ExpectWebdriverIO` namespace.
10+
*
511
* @see {@link https://jestjs.io/docs/expect#expectextendmatchers}
612
*/
713
declare module 'expect' {
814
interface AsymmetricMatchers {
9-
toBeWithinRange(floor: number, ceiling: number): void
10-
toHaveSimpleCustomProperty(string: string): string
11-
toHaveCustomProperty(element: ChainablePromiseElement | WebdriverIO.Element): Promise<ExpectWebdriverIO.PartialMatcher<string>>
15+
toBeWithinRangeExpect(floor: number, ceiling: number): void
16+
toHaveSimpleCustomPropertyExpect(string: string): string
17+
toHaveCustomPropertyExpect(element: ChainablePromiseElement | WebdriverIO.Element): Promise<ExpectWebdriverIO.PartialMatcher<string>>
1218
}
1319

1420
interface Matchers<R, T> {
15-
toBeWithinRange(floor: number, ceiling: number): R
16-
toHaveSimpleCustomProperty(string: string | ExpectWebdriverIO.PartialMatcher<string>): Promise<R>
17-
toHaveCustomProperty:
21+
toBeWithinRangeExpect(floor: number, ceiling: number): R
22+
toHaveSimpleCustomPropertyExpect(string: string | ExpectWebdriverIO.PartialMatcher<string>): Promise<R>
23+
toHaveCustomPropertyExpect:
1824
// Useful to typecheck the custom matcher so it is only used on elements
1925
T extends ChainablePromiseElement | WebdriverIO.Element ?
2026
(test: string | ExpectWebdriverIO.PartialMatcher<string> |
@@ -23,4 +29,4 @@ declare module 'expect' {
2329
// Using `never` blocks the call on non-element types
2430
=> Promise<R> : never;
2531
}
26-
}
32+
}
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
/**
22
* Custom matchers under the `ExpectWebdriverIO` namespace.
3+
*
4+
* RECOMMENDED: This augmentation works universally.
5+
* It is supported by both the global `expect` in Jest/Jasmine AND `expect.soft`.
6+
*
37
* @see {@link https://webdriver.io/docs/custommatchers/#typescript-support}
48
*/
59
declare namespace ExpectWebdriverIO {
610
interface AsymmetricMatchers {
7-
toBeCustom(): ExpectWebdriverIO.PartialMatcher<string>;
8-
toBeCustomPromise(chainableElement: ChainablePromiseElement): Promise<ExpectWebdriverIO.PartialMatcher<string>>;
11+
toBeCustomWdio(): ExpectWebdriverIO.PartialMatcher<string>;
12+
toBeCustomPromiseWdio(chainableElement: ChainablePromiseElement): Promise<ExpectWebdriverIO.PartialMatcher<string>>;
913
}
1014
interface Matchers<R, T> {
11-
toBeCustom(): R;
12-
toBeCustomPromise: T extends ChainablePromiseElement ? (expected?: string | ExpectWebdriverIO.PartialMatcher<string> | Promise<ExpectWebdriverIO.PartialMatcher<string>>) => Promise<R> : never;
15+
toBeCustomWdio(): R;
16+
toBeCustomPromiseWdio: T extends ChainablePromiseElement ? (expected?: string | ExpectWebdriverIO.PartialMatcher<string> | Promise<ExpectWebdriverIO.PartialMatcher<string>>) => Promise<R> : never;
1317
}
14-
}
18+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
declare namespace jest {
2+
/**
3+
* Custom matchers under the `jest` namespace.
4+
*
5+
* LIMITATION: This augmentation only works for the global `expect(...)` in the Jest environment.
6+
* It does NOT support `expect.soft(...)` which uses the standalone `expect` types.
7+
*
8+
* For universal support, prefer augmenting `ExpectWebdriverIO` namespace.
9+
*/
10+
interface Expect {
11+
toBeWithinRangeJest(floor: number, ceiling: number): void
12+
toHaveSimpleCustomPropertyJest(string: string): string
13+
toHaveCustomPropertyJest(element: ChainablePromiseElement | WebdriverIO.Element): Promise<ExpectWebdriverIO.PartialMatcher<string>>
14+
}
15+
16+
interface InverseAsymmetricMatchers {
17+
toBeWithinRangeJest(floor: number, ceiling: number): void
18+
toHaveSimpleCustomPropertyJest(string: string): string
19+
toHaveCustomPropertyJest(element: ChainablePromiseElement | WebdriverIO.Element): Promise<ExpectWebdriverIO.PartialMatcher<string>>
20+
}
21+
22+
interface Matchers<R, T> {
23+
toBeWithinRangeJest(floor: number, ceiling: number): R
24+
toHaveSimpleCustomPropertyJest(string: string | ExpectWebdriverIO.PartialMatcher<string>): Promise<R>
25+
toHaveCustomPropertyJest:
26+
// Useful to typecheck the custom matcher so it is only used on elements
27+
T extends ChainablePromiseElement | WebdriverIO.Element ?
28+
(test: string | ExpectWebdriverIO.PartialMatcher<string> |
29+
// Needed for the custom asymmetric matcher defined above to be typed correctly
30+
Promise<ExpectWebdriverIO.PartialMatcher<string>>)
31+
// Using `never` blocks the call on non-element types
32+
=> Promise<R> : never;
33+
}
34+
}

0 commit comments

Comments
 (0)