File tree Expand file tree Collapse file tree 2 files changed +30
-2
lines changed Expand file tree Collapse file tree 2 files changed +30
-2
lines changed Original file line number Diff line number Diff line change @@ -124,6 +124,26 @@ describe('.toHaveValue', () => {
124
124
` )
125
125
} )
126
126
127
+ test ( 'throws with type information when the expected text input value has loose equality with received value' , ( ) => {
128
+ const { container} = render ( `<input data-testid="one" value="8" />` )
129
+ const input = container . firstChild
130
+ let errorMessage
131
+ try {
132
+ expect ( input ) . toHaveValue ( 8 )
133
+ } catch ( error ) {
134
+ errorMessage = error . message
135
+ }
136
+
137
+ expect ( errorMessage ) . toMatchInlineSnapshot ( `
138
+ "<dim>expect(</><red>element</><dim>).toHaveValue(</><green>8</><dim>)</>
139
+
140
+ Expected the element to have value:
141
+ <green> 8 (number)</>
142
+ Received:
143
+ <red> 8 (string)</>"
144
+ ` )
145
+ } )
146
+
127
147
test ( 'throws when using not but the expected input value does match' , ( ) => {
128
148
const { container} = render ( `<input data-testid="one" value="foo" />` )
129
149
const input = container . firstChild
Original file line number Diff line number Diff line change @@ -21,6 +21,14 @@ export function toHaveValue(htmlElement, expectedValue) {
21
21
22
22
const receivedValue = getSingleElementValue ( htmlElement )
23
23
const expectsValue = expectedValue !== undefined
24
+
25
+ let expectedTypedValue = expectedValue
26
+ let receivedTypedValue = receivedValue
27
+ if ( expectedValue == receivedValue && expectedValue !== receivedValue ) {
28
+ expectedTypedValue = `${ expectedValue } (${ typeof expectedValue } )`
29
+ receivedTypedValue = `${ receivedValue } (${ typeof receivedValue } )`
30
+ }
31
+
24
32
return {
25
33
pass : expectsValue
26
34
? isEqualWith ( receivedValue , expectedValue , compareArraysAsSet )
@@ -35,9 +43,9 @@ export function toHaveValue(htmlElement, expectedValue) {
35
43
return getMessage (
36
44
matcher ,
37
45
`Expected the element ${ to } have value` ,
38
- expectsValue ? expectedValue : '(any)' ,
46
+ expectsValue ? expectedTypedValue : '(any)' ,
39
47
'Received' ,
40
- receivedValue ,
48
+ receivedTypedValue ,
41
49
)
42
50
} ,
43
51
}
You can’t perform that action at this time.
0 commit comments