@@ -11,24 +11,46 @@ import { enhanceError, enhanceErrorBe, numberError } from './util/formatMessage.
1111
1212const sleep = ( ms : number ) => new Promise ( ( resolve ) => setTimeout ( resolve , ms ) )
1313
14- const asymmetricMatcher =
15- typeof Symbol === 'function' && Symbol . for
16- ? Symbol . for ( 'jest.asymmetricMatcher' )
17- : 0x13_57_a5
14+ export function isWdioAsymmetricMatcher < T > ( expected : unknown ) : expected is WdioAsymmetricMatcher < T > {
15+ return isAsymmetricMatcher ( expected ) && 'sample' in expected
16+ }
17+
18+ export function isJasmineAsymmetricMatcher < T > ( expected : unknown ) : expected is JasmineAsymmetricMatcher < T > {
19+ return isAsymmetricMatcher ( expected ) && 'expected' in expected
20+ }
1821
19- export function isAsymmetricMatcher ( expected : unknown ) : expected is WdioAsymmetricMatcher < unknown > {
22+ export function isAsymmetricMatcher ( expected : unknown ) : expected is WdioAsymmetricMatcher < unknown > | JasmineAsymmetricMatcher < unknown > {
2023 return (
2124 typeof expected === 'object' &&
22- expected &&
23- '$$typeof' in expected &&
25+ ! ! expected &&
2426 'asymmetricMatch' in expected &&
25- expected . $$typeof === asymmetricMatcher &&
2627 Boolean ( expected . asymmetricMatch )
27- ) as boolean
28+ )
29+ }
30+
31+ export function isStringContainingMatcher ( expected : unknown ) : expected is WdioAsymmetricMatcher < unknown > | JasmineAsymmetricMatcher < unknown > {
32+ return ! ! expected && expected . constructor . name === 'StringContaining'
2833}
2934
30- function isStringContainingMatcher ( expected : unknown ) : expected is WdioAsymmetricMatcher < unknown > {
31- return isAsymmetricMatcher ( expected ) && [ 'StringContaining' , 'StringNotContaining' ] . includes ( expected . toString ( ) )
35+ export function isStrictStringContainingMatcher ( expected : unknown ) : expected is WdioAsymmetricMatcher < unknown > | JasmineAsymmetricMatcher < unknown > {
36+ if ( isStringContainingMatcher ( expected ) ) {
37+ if ( isWdioAsymmetricMatcher ( expected ) ) {
38+ return expected . toString ( ) . includes ( 'StringContaining' )
39+ }
40+ return true
41+ }
42+ return false
43+ }
44+
45+ export function getValueFromAsymmetricMatcher < T > (
46+ expected : WdioAsymmetricMatcher < T > | JasmineAsymmetricMatcher < T >
47+ ) : T {
48+ if ( 'expected' in expected ) {
49+ return expected . expected
50+ } else if ( 'sample' in expected ) {
51+ return expected . sample
52+ }
53+ throw new Error ( 'Could not extract value from asymmetric matcher: ' + expected )
3254}
3355
3456/**
@@ -141,7 +163,7 @@ const compareNumbers = (actual: number, options: ExpectWebdriverIO.NumberOptions
141163
142164export const compareText = (
143165 actual : string ,
144- expected : string | RegExp | WdioAsymmetricMatcher < string > ,
166+ expected : string | RegExp | WdioAsymmetricMatcher < string > | JasmineAsymmetricMatcher < string > ,
145167 {
146168 ignoreCase = false ,
147169 trim = true ,
@@ -170,9 +192,10 @@ export const compareText = (
170192 if ( typeof expected === 'string' ) {
171193 expected = expected . toLowerCase ( )
172194 } else if ( isStringContainingMatcher ( expected ) ) {
173- expected = ( expected . toString ( ) === 'StringContaining'
174- ? expect . stringContaining ( expected . sample ?. toString ( ) . toLowerCase ( ) )
175- : expect . not . stringContaining ( expected . sample ?. toString ( ) . toLowerCase ( ) ) ) as WdioAsymmetricMatcher < string >
195+ const sample = getValueFromAsymmetricMatcher < string > ( expected ) . toString ( ) . toLocaleLowerCase ( )
196+ expected = ( isStrictStringContainingMatcher ( expected )
197+ ? expect . stringContaining ( sample )
198+ : expect . not . stringContaining ( sample ) ) as WdioAsymmetricMatcher < string >
176199 }
177200 }
178201
0 commit comments