Skip to content

Commit e3848b9

Browse files
committed
Review typing, name matchers var more precisely
1 parent 047cdb4 commit e3848b9

8 files changed

Lines changed: 51 additions & 46 deletions

File tree

jasmine-wdio-expect-async.d.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
/// <reference types="./types/expect-webdriverio.d.ts"/>
22

3+
/**
4+
* Augment the Jasmine namespace to match the behavior of `@wdio/jasmine-framework`.
5+
* Only custom WDIO matchers are available under `expectAsync`, as well as Jasmine's built-in matchers.
6+
* `expectAsync` is forced into the `expect` global ambient, making all Jasmine sync-matchers asynchronous.
7+
*
8+
* When using `@wdio/jasmine-framework`, specify `expect-webdriverio/jasmine-wdio-expect-async` in the tsconfig.json's types.
9+
*/
10+
311
declare namespace jasmine {
412

513
/**
@@ -9,7 +17,6 @@ declare namespace jasmine {
917
* Both T,U must stay named as they are to override the default `AsyncMatchers` type from Jasmine.
1018
*
1119
* We force Matchers to return a `Promise<void>` since Jasmine's `expectAsync` expects a promise in all cases (different from Jest)
12-
* With Jasmine, only custom matchers are available under `expectAsync`, and not the one from Jest `expect` Library.
1320
*/
1421

1522
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- U is required to properly override Jasmine's AsyncMatchers
@@ -27,7 +34,7 @@ declare namespace ExpectWebdriverIO {
2734

2835
/**
2936
* Overrides the default WDIO expect specifically for Jasmine, since `expectAsync` is forced into `expect`, making all matchers fully asynchronous. This is not the case under Jest or Mocha.
30-
* Using `jasmine.AsyncMatchers` pull on WdioMatchers above but also allow to using Jasmine's built-in matchers and also `withContext` matcher.
37+
* Using `jasmine.AsyncMatchers` includes the WdioMatchers from above, but also allows using Jasmine's built-in matchers and the `withContext` matcher.
3138
*/
3239
interface JasmineExpect extends ExpectWebdriverIO.JasmineAsymmetricMatchers, ExpectLibInverse<ExpectWebdriverIO.JasmineAsymmetricMatchers> {
3340
/**
@@ -38,8 +45,6 @@ declare namespace ExpectWebdriverIO {
3845
* - T: the type of the actual value, e.g. any type, not just WebdriverIO.Browser or WebdriverIO.Element
3946
* - R: the type of the return value, e.g. Promise<void> or void
4047
*
41-
* Note: The function must stay here in the namespace to overwrite correctly the expect function from the expect library.
42-
*
4348
* @param actual The value to apply matchers against.
4449
*/
4550
<T = unknown>(actual: T): {
@@ -48,14 +53,14 @@ declare namespace ExpectWebdriverIO {
4853
}
4954
}
5055

51-
// @ts-expect-error: IDE might flags this one but just does be concerned by it. This way the `tsc:root-types` can pass!
56+
/**
57+
* Under `@wdio/jasmine-framework`, the global `expect` is overridden to use Jasmine's `expectAsync`.
58+
* It contains custom WebdriverIO matchers as well as Jasmine's built-in async & sync matchers but not the basic Jest's expect library matchers.
59+
*/
60+
// @ts-expect-error: IDE might flag this, but ignore it. This way the `tsc:root-types` can pass!
5261
declare const expect: ExpectWebdriverIO.JasmineExpect
5362
declare namespace NodeJS {
5463
interface Global {
55-
/**
56-
* Under `@wdio/jasmine-framework`, the global `expect` is overridden to use Jasmine's expectAsync.
57-
* It contains custom WebdriverIO matchers as well as Jasmine's async & sync matchers but not the basic Jest's expect library matchers.
58-
*/
5964
expect: ExpectWebdriverIO.JasmineExpect
6065
}
6166
}

jasmine.d.ts

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,20 @@
11
/// <reference types="./types/expect-webdriverio.d.ts"/>
22

3+
/**
4+
* Augment the Jasmine namespace to include the WDIO custom async matchers only.
5+
* When using the vanilla Jasmine Library, use `jasmine.addAsyncMatchers(matchers)` and specify `expect-webdriverio/jasmine` in the tsconfig.json's types.
6+
*/
7+
38
declare namespace jasmine {
49

510
/**
6-
* Async matchers for Jasmine to allow the typing of `expectAsync` with WebDriverIO matchers.
11+
* Async matchers for Jasmine to allow the typing of `expectAsync` with WebDriverIO custom matchers.
712
* T is the type of the actual value
813
* U is the type of the expected value
914
* Both T,U must stay named as they are to override the default `AsyncMatchers` type from Jasmine.
1015
*
11-
* We force Matchers to return a `Promise<void>` since Jasmine's `expectAsync` expects a promise in all cases (different from Jest)
12-
* With Jasmine, only custom matchers are available under `expectAsync`, and not the one from Jest `expect` Library.
16+
* We force Matchers to return a `Promise<void>` since under Jasmine's `expectAsync` everything is a promise.
1317
*/
1418
// eslint-disable-next-line @typescript-eslint/no-unused-vars
15-
interface AsyncMatchers<T, U> extends ExpectWebdriverIO.CustomMatchers<Promise<void>, T> {
16-
17-
/**
18-
* snapshot matcher
19-
* @param label optional snapshot label
20-
*/
21-
toMatchSnapshot(label?: string): Promise<void>
22-
23-
/**
24-
* inline snapshot matcher
25-
* @param snapshot snapshot string (autogenerated if not specified)
26-
* @param label optional snapshot label
27-
*/
28-
toMatchInlineSnapshot(snapshot?: string, label?: string): Promise<void>
29-
}
19+
interface AsyncMatchers<T, U> extends ExpectWebdriverIO.CustomMatchers<Promise<void>, T> {}
3020
}

jest.d.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
11
/// <reference types="./types/expect-webdriverio.d.ts"/>
22

33
/**
4-
* Augment the Jest namespace to include the matchers from expect-webdriverio.
5-
* When Jest Library is used, it specifies `expect-webdriverio/jest` for this file in the tsconfig.json's types.
4+
* Augment the Jest namespace to include the WebDriverIO custom matchers only.
5+
* When Jest Library is used, specify `expect-webdriverio/jest` for this file in the tsconfig.json's types.
66
*/
77

88
declare namespace jest {
99

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

1212
/**
1313
* Below are overloaded Jest's matchers not part of `expect` but of `jest-snapshot`.
1414
* @see https://github.com/jestjs/jest/blob/73dbef5d2d3195a1e55fb254c54cce70d3036252/packages/jest-snapshot/src/types.ts#L37
1515
*
16-
* Note: We need to define them below so that they are correctly typed overloaded.
16+
* Note: We need to define them below so that they are correctly overloaded.
1717
* Else even when extending `WdioJestOverloadedMatchers` we have typing errors.
1818
*/
1919

2020
/**
2121
* snapshot matcher
2222
* @param label optional snapshot label
2323
*/
24-
toMatchSnapshot(label?: string): T extends WdioPromiseLike ? Promise<void> : void;
24+
toMatchSnapshot(label?: string): T extends WdioElementOrPromiseLike ? Promise<void> : R;
2525

2626
/**
2727
* inline snapshot matcher
2828
* @param snapshot snapshot string (autogenerated if not specified)
2929
* @param label optional snapshot label
3030
*/
31-
toMatchInlineSnapshot(snapshot?: string, label?: string): T extends WdioPromiseLike ? Promise<void> : void;
31+
toMatchInlineSnapshot(snapshot?: string, label?: string): T extends WdioElementOrPromiseLike ? Promise<void> : R;
3232
}
3333

3434
interface Expect extends ExpectWebdriverIO.Expect {}
3535

3636
interface InverseAsymmetricMatchers extends ExpectWebdriverIO.AsymmetricMatchers {}
37-
}
37+
}

src/index.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@ import { DEFAULT_OPTIONS, defaultOptionsList } from './constants.js'
66
import createSoftExpect from './softExpect.js'
77
import { SoftAssertService } from './softAssert.js'
88

9-
export const matchers: WdioMatchersObject = new Map<string, RawMatcherFn>()
9+
/**
10+
* Contains only the custom WDIO matchers to be used with `expect.extend()`.
11+
*/
12+
export const wdioCustomMatchers: WdioMatchersObject = new Map<string, RawMatcherFn>()
13+
14+
// @deprecated use `wdioCustomMatchers` instead
15+
export const matchers = wdioCustomMatchers
16+
1017
const filteredMatchers = {}
1118
const extend = expectLib.extend
1219

@@ -22,7 +29,7 @@ expectLib.extend = (m) => {
2229
return
2330
}
2431

25-
Object.entries(m).forEach(([name, matcher]) => matchers.set(name, matcher))
32+
Object.entries(m).forEach(([name, matcher]) => wdioCustomMatchers.set(name, matcher))
2633
return extend(m)
2734
}
2835

src/softExpect.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expect, matchers } from './index.js'
1+
import { expect, wdioCustomMatchers } from './index.js'
22
import { SoftAssertService } from './softAssert.js'
33

44
/**
@@ -24,7 +24,7 @@ const createSoftExpect = <T = unknown>(actual: T): ExpectWebdriverIO.Matchers<Pr
2424
}
2525

2626
// Handle matchers
27-
if (matchers.has(propName)) {
27+
if (wdioCustomMatchers.has(propName)) {
2828
return createSoftMatcher(actual, propName, softService)
2929
}
3030

@@ -41,7 +41,7 @@ const createSoftNotProxy = <T>(actual: T, softService: SoftAssertService) => {
4141
return new Proxy({} as ExpectWebdriverIO.Matchers<Promise<void>, T>, {
4242
get(target, prop) {
4343
const propName = String(prop)
44-
if (matchers.has(propName)) {
44+
if (wdioCustomMatchers.has(propName)) {
4545
return createSoftMatcher(actual, propName, softService, 'not')
4646
}
4747
return undefined
@@ -56,7 +56,7 @@ const createSoftChainProxy = <T>(actual: T, chainType: string, softService: Soft
5656
return new Proxy({} as ExpectWebdriverIO.Matchers<Promise<void>, T>, {
5757
get(target, prop) {
5858
const propName = String(prop)
59-
if (matchers.has(propName)) {
59+
if (wdioCustomMatchers.has(propName)) {
6060
return createSoftMatcher(actual, propName, softService, chainType)
6161
}
6262
return undefined

test/index.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { test, expect } from 'vitest'
2-
import { setOptions, expect as expectExport, matchers, utils } from '../src/index.js'
2+
import { setOptions, expect as expectExport, matchers, utils, wdioCustomMatchers } from '../src/index.js'
33

44
test('index', () => {
55
expect(setOptions.name).toBe('setDefaultOptions')
66
expect(expectExport).toBeDefined()
77
expect(utils.compareText).toBeDefined()
8-
expect(matchers.size).toEqual(42)
8+
expect(wdioCustomMatchers.size).toEqual(41)
9+
expect(matchers.size).toEqual(41)
910
})

test/matchers.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { test, expect, vi, describe } from 'vitest'
2-
import { matchers, expect as expectLib } from '../src/index.js'
2+
import { matchers, wdioCustomMatchers, expect as expectLib } from '../src/index.js'
33
import { $ } from '@wdio/globals'
44

55
vi.mock('@wdio/globals')
@@ -60,6 +60,7 @@ const ALL_MATCHERS = [
6060

6161
test('matchers', () => {
6262
expect([...matchers.keys()]).toEqual(ALL_MATCHERS)
63+
expect(wdioCustomMatchers.size).toEqual(ALL_MATCHERS.length)
6364
})
6465

6566
test('allows to add matcher', () => {
@@ -69,6 +70,7 @@ test('allows to add matcher', () => {
6970
// @ts-expect-error not in types
7071
expectLib('foo').toBeCustom('foo')
7172
expect(matchers.keys()).toContain('toBeCustom')
73+
expect(wdioCustomMatchers.keys()).toContain('toBeCustom')
7274
})
7375

7476
test('Generic asymmetric matchers from Expect library should work', () => {

types/expect-webdriverio.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -397,18 +397,18 @@ interface WdioElementArrayOnlyMatchers<_R, ActualT = unknown> {
397397
* Therefore, they also need to be redefined in the jest.d.ts file so correctly overload the matchers from the Jest namespace.
398398
* @see jest.d.ts
399399
*/
400-
interface WdioJestOverloadedMatchers<_R, ActualT> {
400+
interface WdioJestOverloadedMatchers<R, ActualT> {
401401
/**
402402
* snapshot matcher
403403
* @param label optional snapshot label
404404
*/
405-
toMatchSnapshot(label?: string): ActualT extends WdioElementOrPromiseLike ? Promise<void> : void
405+
toMatchSnapshot(label?: string): ActualT extends WdioElementOrPromiseLike ? Promise<void> : R
406406
/**
407407
* inline snapshot matcher
408408
* @param snapshot snapshot string (autogenerated if not specified)
409409
* @param label optional snapshot label
410410
*/
411-
toMatchInlineSnapshot(snapshot?: string, label?: string): ActualT extends WdioElementOrPromiseLike ? Promise<void> : void
411+
toMatchInlineSnapshot(snapshot?: string, label?: string): ActualT extends WdioElementOrPromiseLike ? Promise<void> : R
412412
}
413413

414414
/**

0 commit comments

Comments
 (0)