Skip to content

Commit 0996b9e

Browse files
committed
fix failure msg with enhanceErrorBe + code refactoring simplification
- Fix double negation in `Expected` with `isNot` when using `enhanceErrorBe` - Fix incorrect Received value with `isNot` when using `enhanceErrorBe` - Code refactoring simplify to better understand the code
1 parent da0b63e commit 0996b9e

File tree

2 files changed

+223
-129
lines changed

2 files changed

+223
-129
lines changed

src/util/formatMessage.ts

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@ import { equals } from '../jasmineUtils.js'
33
import type { WdioElements } from '../types.js'
44
import { isElementArray } from './elementsUtil.js'
55

6-
const EXPECTED_LABEL = 'Expected'
7-
const RECEIVED_LABEL = 'Received'
8-
const NOT_SUFFIX = ' [not]'
9-
const NOT_EXPECTED_LABEL = EXPECTED_LABEL + NOT_SUFFIX
10-
116
export const getSelector = (el: WebdriverIO.Element | WebdriverIO.ElementArray) => {
127
let result = typeof el.selector === 'string' ? el.selector : '<fn>'
138
if (Array.isArray(el) && (el as WebdriverIO.ElementArray).props.length > 0) {
@@ -39,22 +34,20 @@ export const getSelectors = (el: WebdriverIO.Element | WdioElements) => {
3934
return selectors.reverse().join('.')
4035
}
4136

42-
export const not = (isNot: boolean): string => {
43-
return `${isNot ? 'not ' : ''}`
44-
}
37+
const not = (isNot: boolean): string => `${isNot ? 'not ' : ''}`
4538

4639
export const enhanceError = (
4740
subject: string | WebdriverIO.Element | WdioElements,
4841
expected: unknown,
4942
actual: unknown,
50-
context: { isNot: boolean },
43+
context: { isNot: boolean, useNotInLabel?: boolean },
5144
verb: string,
5245
expectation: string,
53-
arg2 = '', {
46+
expectedValueArgument2 = '', {
5447
message = '',
5548
containing = false
56-
}): string => {
57-
const { isNot = false } = context
49+
} = {}): string => {
50+
const { isNot = false, useNotInLabel = true } = context
5851

5952
subject = typeof subject === 'string' ? subject : getSelectors(subject)
6053

@@ -67,37 +60,43 @@ export const enhanceError = (
6760
verb += ' '
6861
}
6962

70-
let diffString = isNot && equals(actual, expected)
71-
? `${EXPECTED_LABEL}: ${printExpected(expected)}\n${RECEIVED_LABEL}: ${printReceived(actual)}`
72-
: printDiffOrStringify(expected, actual, EXPECTED_LABEL, RECEIVED_LABEL, true)
73-
74-
if (isNot) {
75-
diffString = diffString
76-
.replace(EXPECTED_LABEL, NOT_EXPECTED_LABEL)
77-
.replace(RECEIVED_LABEL, RECEIVED_LABEL + ' '.repeat(NOT_SUFFIX.length))
63+
const label = {
64+
expected: isNot && useNotInLabel ? 'Expected [not]' : 'Expected',
65+
received: isNot && useNotInLabel ? 'Received ' : 'Received'
7866
}
7967

68+
// Using `printDiffOrStringify()` with equals values output `Received: serializes to the same string`, so we need to tweak.
69+
const diffString = equals(actual, expected) ?`\
70+
${label.expected}: ${printExpected(expected)}
71+
${label.received}: ${printReceived(actual)}`
72+
: printDiffOrStringify(expected, actual, label.expected, label.received, true)
73+
8074
if (message) {
8175
message += '\n'
8276
}
8377

84-
if (arg2) {
85-
arg2 = ` ${arg2}`
78+
if (expectedValueArgument2) {
79+
expectedValueArgument2 = ` ${expectedValueArgument2}`
8680
}
8781

88-
const msg = `${message}Expect ${subject} ${not(isNot)}to ${verb}${expectation}${arg2}${contain}\n\n${diffString}`
82+
const msg = `\
83+
${message}Expect ${subject} ${not(isNot)}to ${verb}${expectation}${expectedValueArgument2}${contain}
84+
85+
${diffString}`
86+
8987
return msg
9088
}
9189

9290
export const enhanceErrorBe = (
9391
subject: string | WebdriverIO.Element | WebdriverIO.ElementArray,
94-
pass: boolean,
92+
_pass: boolean,
9593
context: { isNot: boolean },
9694
verb: string,
9795
expectation: string,
9896
options: ExpectWebdriverIO.CommandOptions
9997
) => {
100-
return enhanceError(subject, not(context.isNot) + expectation, not(!pass) + expectation, context, verb, expectation, '', options)
98+
const { isNot } = context
99+
return enhanceError(subject, `${not(isNot)}${expectation}`, `${not(!isNot)}${expectation}`, { ...context, useNotInLabel: false }, verb, expectation, '', options)
101100
}
102101

103102
export const numberError = (options: ExpectWebdriverIO.NumberOptions = {}): string | number => {

0 commit comments

Comments
 (0)