Skip to content

Commit ca4d073

Browse files
committed
Separate Browser, mock and element matcher + better typing
1 parent 776fee5 commit ca4d073

File tree

3 files changed

+187
-109
lines changed

3 files changed

+187
-109
lines changed

test-types/jest/types-jest.test.ts

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,40 @@ describe('type assertions', () => {
55
const chainableElement = $('findMe')
66
const chainableArray = $$('ul>li')
77

8-
describe('toHaveUrl', () => {
8+
describe('Browser', () => {
99
const browser: WebdriverIO.Browser = {} as unknown as WebdriverIO.Browser
10-
it('should not have ts errors and be able to await the promise when actual is browser', async () => {
11-
const expectPromiseVoid: Promise<void> = expect(browser).toHaveUrl('https://example.com')
12-
await expectPromiseVoid
10+
describe('toHaveUrl', () => {
11+
it('should not have ts errors and be able to await the promise when actual is browser', async () => {
12+
const expectPromiseVoid: Promise<void> = expect(browser).toHaveUrl('https://example.com')
13+
await expectPromiseVoid
1314

14-
const expectNotPromiseVoid: Promise<void> = expect(browser).not.toHaveUrl('https://example.com')
15-
await expectNotPromiseVoid
16-
})
15+
const expectNotPromiseVoid: Promise<void> = expect(browser).not.toHaveUrl('https://example.com')
16+
await expectNotPromiseVoid
17+
})
1718

18-
it('should have ts errors and not need to await the promise when actual is browser', async () => {
19+
it('should have ts errors and not need to await the promise when actual is browser', async () => {
1920
// @ts-expect-error
20-
const expectVoid: void = expect(browser).toHaveUrl('https://example.com')
21-
// @ts-expect-error
22-
const expectNotVoid: void = expect(browser).not.toHaveUrl('https://example.com')
23-
})
21+
const expectVoid: void = expect(browser).toHaveUrl('https://example.com')
22+
// @ts-expect-error
23+
const expectNotVoid: void = expect(browser).not.toHaveUrl('https://example.com')
24+
})
2425

25-
it('should have ts errors when actual is an element', async () => {
26+
it('should have ts errors when actual is an element', async () => {
2627
// @ts-expect-error
27-
await expect(element).toHaveUrl('https://example.com')
28-
})
28+
await expect(element).toHaveUrl('https://example.com')
29+
})
2930

30-
it('should have ts errors when actual is an ChainableElement', async () => {
31+
it('should have ts errors when actual is an ChainableElement', async () => {
3132
// @ts-expect-error
32-
await expect(chainableElement).toHaveUrl('https://example.com')
33-
})
33+
await expect(chainableElement).toHaveUrl('https://example.com')
34+
})
3435

35-
it('should support stringContaining', async () => {
36-
const expectVoid1: Promise<void> = expect(browser).toHaveUrl(expect.stringContaining('WebdriverIO'))
36+
it('should support stringContaining', async () => {
37+
const expectVoid1: Promise<void> = expect(browser).toHaveUrl(expect.stringContaining('WebdriverIO'))
3738

38-
// @ts-expect-error
39-
const expectVoid2: void = expect(browser).toHaveUrl(expect.stringContaining('WebdriverIO'))
39+
// @ts-expect-error
40+
const expectVoid2: void = expect(browser).toHaveUrl(expect.stringContaining('WebdriverIO'))
41+
})
4042
})
4143
})
4244

@@ -227,7 +229,11 @@ describe('type assertions', () => {
227229
const expectPromise2: Promise<void> = expect(promiseNetworkMock).toBeRequestedTimes(2) // await expect(mock).toBeRequestedTimes({ eq: 2 })
228230
const expectPromise3: Promise<void> = expect(promiseNetworkMock).toBeRequestedTimes({ gte: 5, lte: 10 }) // request called at least 5 times but less than 11
229231

230-
const expectPromise4: Promise<void> = expect(promiseNetworkMock).toBeRequestedWith({
232+
const expectPromise4: Promise<void> = expect(promiseNetworkMock).not.toBeRequested()
233+
const expectPromise5: Promise<void> = expect(promiseNetworkMock).not.toBeRequestedTimes(2) // await expect(mock).toBeRequestedTimes({ eq: 2 })
234+
const expectPromise6: Promise<void> = expect(promiseNetworkMock).not.toBeRequestedTimes({ gte: 5, lte: 10 }) // request called at least 5 times but less than 11
235+
236+
const expectPromise7: Promise<void> = expect(promiseNetworkMock).toBeRequestedWith({
231237
url: 'http://localhost:8080/api/todo', // [optional] string | function | custom matcher
232238
method: 'POST', // [optional] string | array
233239
statusCode: 200, // [optional] number | array
@@ -236,6 +242,10 @@ describe('type assertions', () => {
236242
postData: { title: 'foo', description: 'bar' }, // [optional] object | function | custom matcher
237243
response: { success: true }, // [optional] object | function | custom matcher
238244
})
245+
246+
const expectPromise8: Promise<void> = expect(promiseNetworkMock).toBeRequestedWith(expect.objectContaining({
247+
response: { success: true }, // [optional] object | function | custom matcher
248+
}))
239249
})
240250

241251
it('should have ts errors when typing to void', async () => {
@@ -247,7 +257,14 @@ describe('type assertions', () => {
247257
const expectPromise3: void = expect(promiseNetworkMock).toBeRequestedTimes({ gte: 5, lte: 10 }) // request called at least 5 times but less than 11
248258

249259
// @ts-expect-error
250-
const expectPromise4: void = expect(promiseNetworkMock).toBeRequestedWith({
260+
const expectPromise4: void = expect(promiseNetworkMock).not.toBeRequested()
261+
// @ts-expect-error
262+
const expectPromise5: void = expect(promiseNetworkMock).not.toBeRequestedTimes(2) // await expect(mock).toBeRequestedTimes({ eq: 2 })
263+
// @ts-expect-error
264+
const expectPromise6: void = expect(promiseNetworkMock).not.toBeRequestedTimes({ gte: 5, lte: 10 }) // request called at least 5 times but less than 11
265+
266+
// @ts-expect-error
267+
const expectPromise7: void = expect(promiseNetworkMock).toBeRequestedWith({
251268
url: 'http://localhost:8080/api/todo', // [optional] string | function | custom matcher
252269
method: 'POST', // [optional] string | array
253270
statusCode: 200, // [optional] number | array
@@ -256,6 +273,11 @@ describe('type assertions', () => {
256273
postData: { title: 'foo', description: 'bar' }, // [optional] object | function | custom matcher
257274
response: { success: true }, // [optional] object | function | custom matcher
258275
})
276+
277+
// @ts-expect-error
278+
const expectPromise8: void = expect(promiseNetworkMock).toBeRequestedWith(expect.objectContaining({
279+
response: { success: true }, // [optional] object | function | custom matcher
280+
}))
259281
})
260282
})
261283

@@ -292,15 +314,15 @@ describe('type assertions', () => {
292314

293315
it('should have ts error when using await and actual is non-promise type', async () => {
294316
// @ts-expect-error
295-
const expectWdioMatcher: WdioMatchers<Promise<void>, string> = expect.soft(expectString)
317+
const expectWdioMatcher: jest.MatcherAndInverse<Promise<void>, string> = expect.soft(expectString)
296318

297319
// @ts-expect-error
298320
const expectVoid: Promise<void> = expect.soft(expectString).toBe('Test Page')
299321
})
300322

301323
it('should not have ts error and need to be awaited/be a promise if actual is a promise type', async () => {
302-
// @ts-expect-error
303-
const expectWdioMatcher: WdioMatchers<void, Promise<string>> = expect.soft(expectPromise)
324+
// @ts-expect-error
325+
const expectWdioMatcher: jest.MatcherAndInverse<void, Promise<string>> = expect.soft(expectPromise)
304326
// @ts-expect-error
305327
const expectVoid: void = expect.soft(expectPromise).toBe('Test Page')
306328
})

test-types/mocha/types-mocha.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,15 +301,15 @@ describe('type assertions', () => {
301301

302302
it('should have ts error when using await and actual is non-promise type', async () => {
303303
// @ts-expect-error
304-
const expectWdioMatcher: WdioMatchers<Promise<void>, string> = expect.soft(expectString)
304+
const expectWdioMatcher: ExpectWebdriverIO.MatchersAndInverse<Promise<void>, string> = expect.soft(expectString)
305305

306306
// @ts-expect-error
307307
const expectVoid: Promise<void> = expect.soft(expectString).toBe('Test Page')
308308
})
309309

310310
it('should not have ts error and need to be awaited/be a promise if actual is a promise type', async () => {
311-
// @ts-expect-error
312-
const expectWdioMatcher: WdioMatchers<void, Promise<string>> = expect.soft(expectPromise)
311+
// @ts-expect-error
312+
const expectWdioMatcher: ExpectWebdriverIO.MatchersAndInverse<void, Promise<string>> = expect.soft(expectPromise)
313313
// @ts-expect-error
314314
const expectVoid: void = expect.soft(expectPromise).toBe('Test Page')
315315
})

0 commit comments

Comments
 (0)