Skip to content

Commit cb27d1c

Browse files
committed
add better failure message unit testings
1 parent 0996b9e commit cb27d1c

File tree

7 files changed

+106
-36
lines changed

7 files changed

+106
-36
lines changed

test/__fixtures__/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ export function matcherNameToString(matcherName: string) {
22
return matcherName.replace(/([A-Z])/g, ' $1').toLowerCase()
33
}
44

5+
export function matcherNameLastWord(matcherName: string) {
6+
return matcherName.replace(/([A-Z])/g, ' $1').toLowerCase().replace('to', '').replace('be ', '').trim()
7+
}
8+
59
export function getExpectMessage(msg: string) {
610
return msg.split('\n')[0]
711
}

test/matchers/beMatchers.test.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { vi, test, describe, expect } from 'vitest'
22
import { $ } from '@wdio/globals'
3-
import { getExpectMessage, matcherNameToString } from '../__fixtures__/utils.js'
3+
import { matcherNameToString, matcherNameLastWord } from '../__fixtures__/utils.js'
44
import * as Matchers from '../../src/matchers.js'
55

66
vi.mock('@wdio/globals')
@@ -97,8 +97,12 @@ describe('be* matchers', () => {
9797
test('not - failure', async () => {
9898
const result = await fn.call({ isNot: true }, $('sel'), { wait: 0 }) as ExpectWebdriverIO.AssertionResult
9999

100-
expect(result.message()).toContain('not')
101100
expect(result.pass).toBe(false)
101+
expect(result.message()).toEqual(`\
102+
Expect $(\`sel\`) not ${matcherNameToString(name)}
103+
104+
Expected: "not ${matcherNameLastWord(name)}"
105+
Received: "${matcherNameLastWord(name)}"`)
102106
})
103107

104108
test('not - success', async () => {
@@ -126,13 +130,26 @@ describe('be* matchers', () => {
126130
return false
127131
}
128132
const result = await fn.call({ isNot: true }, el, { wait: 1 }) as ExpectWebdriverIO.AssertionResult
133+
129134
expect(result.pass).toBe(true)
130135
})
131136

132137
test('message', async () => {
133-
const result = await fn.call({}, $('sel')) as ExpectWebdriverIO.AssertionResult
134-
expect(getExpectMessage(result.message()))
135-
.toContain(matcherNameToString(name))
138+
const el = await $('sel')
139+
// @ts-expect-error mock feature
140+
el._value = function (): boolean {
141+
return false
142+
}
143+
144+
const result = await fn.call({}, el, { wait: 0 }) as ExpectWebdriverIO.AssertionResult
145+
146+
expect(result.pass).toBe(false)
147+
expect(result.message()).toEqual(`\
148+
Expect $(\`sel\`) ${matcherNameToString(name)}
149+
150+
Expected: "${matcherNameLastWord(name)}"
151+
Received: "not ${matcherNameLastWord(name)}"`)
152+
136153
})
137154
})
138155
})

test/matchers/browserMatchers.test.ts

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { vi, test, describe, expect } from 'vitest'
22
import { browser } from '@wdio/globals'
33

4-
import { getExpectMessage, getReceived, matcherNameToString, getExpected } from '../__fixtures__/utils.js'
4+
import { matcherNameToString } from '../__fixtures__/utils.js'
55
import * as Matchers from '../../src/matchers.js'
66

77
vi.mock('@wdio/globals')
@@ -91,12 +91,19 @@ describe('browser matchers', () => {
9191
})
9292

9393
test('not - failure', async () => {
94-
const result = await fn.call({ isNot: true }, browser, validText, { wait: 0, trim: false }) as ExpectWebdriverIO.AssertionResult
94+
// @ts-expect-error mock feature
95+
browser._value = function (): string {
96+
return validText
97+
}
9598

96-
expect(getExpectMessage(result.message())).toContain('not')
97-
expect(getExpected(result.message())).toContain('not')
99+
const result = await fn.call({ isNot: true }, browser, validText, { wait: 0, trim: false }) as ExpectWebdriverIO.AssertionResult
98100

99101
expect(result.pass).toBe(false)
102+
expect(result.message()).toEqual(`\
103+
Expect window not ${matcherNameToString(name)}
104+
105+
Expected [not]: " Valid Text "
106+
Received : " Valid Text "`)
100107
})
101108

102109
test('not - success', async () => {
@@ -106,10 +113,6 @@ describe('browser matchers', () => {
106113
}
107114
const result = await fn.call({ isNot: true }, browser, validText, { wait: 0 }) as ExpectWebdriverIO.AssertionResult
108115

109-
expect(getExpectMessage(result.message())).toContain('not')
110-
expect(getExpected(result.message())).toContain('Valid')
111-
expect(getReceived(result.message())).toContain('Wrong')
112-
113116
expect(result.pass).toBe(true)
114117
})
115118

@@ -118,10 +121,12 @@ describe('browser matchers', () => {
118121
delete browser._value
119122
const result = await fn.call({ isNot: true }, browser, validText, { wait: 1, trim: false }) as ExpectWebdriverIO.AssertionResult
120123

121-
expect(getExpectMessage(result.message())).toContain('not')
122-
expect(getExpected(result.message())).toContain('not')
123-
124124
expect(result.pass).toBe(false)
125+
expect(result.message()).toEqual(`\
126+
Expect window not ${matcherNameToString(name)}
127+
128+
Expected [not]: " Valid Text "
129+
Received : " Valid Text "`)
125130
})
126131

127132
test('not - success (with wait)', async () => {
@@ -131,16 +136,23 @@ describe('browser matchers', () => {
131136
}
132137
const result = await fn.call({ isNot: true }, browser, validText, { wait: 1 }) as ExpectWebdriverIO.AssertionResult
133138

134-
expect(getExpectMessage(result.message())).toContain('not')
135-
expect(getExpected(result.message())).toContain('Valid')
136-
expect(getReceived(result.message())).toContain('Wrong')
137-
138139
expect(result.pass).toBe(true)
139140
})
140141

141142
test('message', async () => {
143+
// @ts-expect-error mock feature
144+
browser._value = function (): string {
145+
return wrongText
146+
}
147+
142148
const result = await fn.call({}, browser) as ExpectWebdriverIO.AssertionResult
143-
expect(getExpectMessage(result.message())).toContain(matcherNameToString(name))
149+
150+
expect(result.pass).toBe(false)
151+
expect(result.message()).toEqual(`\
152+
Expect window ${matcherNameToString(name)}
153+
154+
Expected: undefined
155+
Received: " Wrong Text "`)
144156
})
145157
})
146158
})

test/matchers/element/toHaveComputedLabel.test.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { vi, test, describe, expect, beforeEach } from 'vitest'
22
import { $ } from '@wdio/globals'
33

4-
import { getExpectMessage, getReceived, getExpected } from '../../__fixtures__/utils.js'
4+
import { getExpectMessage } from '../../__fixtures__/utils.js'
55
import { toHaveComputedLabel } from '../../../src/matchers/element/toHaveComputedLabel.js'
66

77
vi.mock('@wdio/globals')
@@ -92,8 +92,12 @@ describe('toHaveComputedLabel', () => {
9292
}
9393
const result = await toHaveComputedLabel.call({ isNot: true }, el, 'WebdriverIO', { wait: 0 })
9494

95-
expect(result.message()).toContain('not')
9695
expect(result.pass).toBe(false)
96+
expect(result.message()).toEqual(`\
97+
Expect $(\`sel\`) not to have computed label
98+
99+
Expected [not]: "WebdriverIO"
100+
Received : "WebdriverIO"`)
97101
})
98102

99103
test("should return false if computed labels don't match", async () => {
@@ -342,18 +346,25 @@ describe('toHaveComputedLabel', () => {
342346

343347
test('failure if no match', async () => {
344348
const result = await toHaveComputedLabel.call({}, el, /Webdriver/i)
349+
345350
expect(result.pass).toBe(false)
346-
expect(getExpectMessage(result.message())).toContain('to have computed label')
347-
expect(getExpected(result.message())).toContain('/Webdriver/i')
348-
expect(getReceived(result.message())).toContain('This is example computed label')
351+
expect(result.message()).toEqual(`\
352+
Expect $(\`sel\`) to have computed label
353+
354+
Expected: /Webdriver/i
355+
Received: "This is example computed label"`)
349356
})
350357

351358
test('failure if array does not match with computed label', async () => {
352359
const result = await toHaveComputedLabel.call({}, el, ['div', /Webdriver/i])
360+
353361
expect(result.pass).toBe(false)
354-
expect(getExpectMessage(result.message())).toContain('to have computed label')
355-
expect(getExpected(result.message())).toContain('/Webdriver/i')
356-
expect(getExpected(result.message())).toContain('div')
362+
expect(result.message()).toEqual(`\
363+
Expect $(\`sel\`) to have computed label
364+
365+
Expected: ["div", /Webdriver/i]
366+
Received: "This is example computed label"`
367+
)
357368
})
358369
})
359370
})

test/matchers/element/toHaveHeight.test.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { vi, test, describe, expect } from 'vitest'
22
import { $ } from '@wdio/globals'
3-
4-
import { getExpectMessage } from '../../__fixtures__/utils.js'
53
import { toHaveHeight } from '../../../src/matchers/element/toHaveHeight.js'
64

75
vi.mock('@wdio/globals')
@@ -67,7 +65,7 @@ describe('toHaveHeight', () => {
6765
}
6866

6967
const result = await toHaveHeight.call({}, el, 32, {})
70-
expect(result.message()).toEqual('Expect $(`sel`) to have height\n\nExpected: 32\nReceived: serializes to the same string')
68+
7169
expect(result.pass).toBe(true)
7270
expect(el._attempts).toBe(1)
7371
})
@@ -144,7 +142,11 @@ describe('toHaveHeight', () => {
144142
}
145143
const result = await toHaveHeight.call({ isNot: true }, el, 32, { wait: 0 })
146144

147-
expect(result.message()).toContain('not')
145+
expect(result.message()).toEqual(`\
146+
Expect $(\`sel\`) not to have height
147+
148+
Expected [not]: 32
149+
Received : 32`)
148150
expect(result.pass).toBe(false)
149151
})
150152

@@ -186,6 +188,11 @@ describe('toHaveHeight', () => {
186188
return null
187189
}
188190
const result = await toHaveHeight.call({}, el, 50)
189-
expect(getExpectMessage(result.message())).toContain('to have height')
191+
192+
expect(result.message()).toEqual(`\
193+
Expect $(\`sel\`) to have height
194+
195+
Expected: 50
196+
Received: null`)
190197
})
191198
})

test/matchers/element/toHaveStyle.test.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,12 @@ describe('toHaveStyle', () => {
123123
})
124124
const result = await toHaveStyle.call({ isNot: true }, el, mockStyle, { wait: 0 })
125125

126-
expect(result.message()).toContain('not')
127126
expect(result.pass).toBe(false)
127+
expect(result.message()).toEqual(`\
128+
Expect $(\`sel\`) not to have style
129+
130+
Expected [not]: {"color": "#000", "font-family": "Faktum", "font-size": "26px"}
131+
Received : {"color": "#000", "font-family": "Faktum", "font-size": "26px"}`)
128132
})
129133

130134
test('should return true if styles dont match when isNot is true', async () => {
@@ -156,7 +160,22 @@ describe('toHaveStyle', () => {
156160
}
157161

158162
const result = await toHaveStyle.bind({ })(el, wrongStyle, { wait: 1 })
163+
159164
expect(result.pass).toBe(false)
165+
expect(result.message()).toEqual(`\
166+
Expect $(\`sel\`) to have style
167+
168+
- Expected - 3
169+
+ Received + 3
170+
171+
Object {
172+
- "color": "#fff",
173+
- "font-family": "Incorrect Font",
174+
- "font-size": "100px",
175+
+ "color": "#000",
176+
+ "font-family": "Faktum",
177+
+ "font-size": "26px",
178+
}`)
160179
})
161180

162181
test('should return true if styles match', async () => {

test/matchers/element/toHaveWidth.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ describe('toHaveWidth', () => {
6767
}
6868

6969
const result = await toHaveWidth.call({}, el, 50, {})
70-
expect(result.message()).toEqual('Expect $(`sel`) to have width\n\nExpected: 50\nReceived: serializes to the same string')
70+
7171
expect(result.pass).toBe(true)
7272
expect(el._attempts).toBe(1)
7373
})

0 commit comments

Comments
 (0)