11import { describe , it , expect , beforeEach , vi } from 'vitest'
2- import { $ } from '@wdio/globals'
2+ import { $ , $$ } from '@wdio/globals'
3+ import expectLib from 'expect'
34import { expect as expectWdio , SoftAssertionService , SoftAssertService } from '../src/index.js'
45
56vi . mock ( '@wdio/globals' )
@@ -19,6 +20,77 @@ describe('Soft Assertions', () => {
1920 } )
2021
2122 describe ( 'expect.soft' , ( ) => {
23+
24+ it ( 'should handle promises properly and return a promise when matchers are used with Promises or Elements' , async ( ) => {
25+ const softService = SoftAssertService . getInstance ( )
26+ softService . setCurrentTest ( 'promise-0' , 'test name' , 'test file' )
27+
28+ expect ( expectLib ( Promise . resolve ( true ) ) . resolves . toBe ( true ) ) . toBeInstanceOf ( Promise )
29+ expect ( expectWdio ( Promise . resolve ( true ) ) . resolves . toBe ( true ) ) . toBeInstanceOf ( Promise )
30+ expect ( expectWdio ( Promise . resolve ( true ) ) . resolves . not . toBe ( false ) ) . toBeInstanceOf ( Promise )
31+ expect ( expectWdio . soft ( Promise . resolve ( true ) ) . resolves . toBe ( true ) ) . toBeInstanceOf ( Promise )
32+
33+ const elementToHaveText = expectWdio ( $ ( 'element1' ) ) . toHaveText ( 'Valid Text' )
34+ expect ( elementToHaveText ) . toBeInstanceOf ( Promise )
35+
36+ // TODO remove await once $$() support is merged
37+ const elementsToHaveText = expectWdio ( await $$ ( 'elements2' ) ) . toHaveText ( 'Valid Text' )
38+ expect ( elementsToHaveText ) . toBeInstanceOf ( Promise )
39+
40+ const elementsNotToHaveText = expectWdio ( await $$ ( 'elements3' ) ) . not . toHaveText ( 'Not Valid Text' )
41+ expect ( elementsNotToHaveText ) . toBeInstanceOf ( Promise )
42+
43+ await Promise . all ( [ elementToHaveText , elementsToHaveText , elementsNotToHaveText ] )
44+
45+ const elementSoftToHaveText = expectWdio . soft ( $ ( 'element4' ) ) . toHaveText ( 'Valid Text' )
46+ expect ( elementSoftToHaveText ) . toBeInstanceOf ( Promise )
47+
48+ const elementsSoftToHaveText = expectWdio . soft ( await $$ ( 'elements5' ) ) . toHaveText ( 'Valid Text' )
49+ expect ( elementsSoftToHaveText ) . toBeInstanceOf ( Promise )
50+
51+ const elementsSoftNotToHaveText = expectWdio . soft ( await $$ ( 'elements6' ) ) . not . toHaveText ( 'Not Valid Text' )
52+ expect ( elementsSoftNotToHaveText ) . toBeInstanceOf ( Promise )
53+
54+ // Ensure all assertions are awaited to avoid conflicts in other tests
55+ await Promise . all ( [ elementSoftToHaveText , elementsSoftToHaveText , elementsSoftNotToHaveText ] )
56+ } )
57+
58+ it ( 'should handle non-promises properly' , ( ) => {
59+ const softService = SoftAssertService . getInstance ( )
60+ softService . setCurrentTest ( 'non-promise-1' , 'test name' , 'test file' )
61+
62+ expect ( expectLib ( true ) . toBe ( true ) ) . toBeUndefined ( )
63+ expect ( expectLib ( true ) . toBe ) . toBeInstanceOf ( Function )
64+ expect ( expectLib ( true ) . toBe ( true ) ) . not . toBeInstanceOf ( Promise )
65+ expect ( expectLib ( true ) . not . toBe ( false ) ) . not . toBeInstanceOf ( Promise )
66+
67+ expect ( expectWdio ( true ) . toBe ( true ) ) . toBeUndefined ( )
68+ expect ( expectWdio ( true ) . toBe ) . toBeInstanceOf ( Function )
69+ expect ( expectWdio ( true ) . toBe ( true ) ) . not . toBeInstanceOf ( Promise )
70+ expect ( expectWdio ( true ) . not . toBe ( false ) ) . not . toBeInstanceOf ( Promise )
71+
72+ expect ( expectWdio . soft ( true ) . toBe ( true ) ) . toBeUndefined ( )
73+ expect ( expectWdio . soft ( true ) . toBe ) . toBeInstanceOf ( Function )
74+ expect ( expectWdio . soft ( true ) . toBe ) . not . toBeInstanceOf ( Promise )
75+ expect ( expectWdio . soft ( true ) . not . toBe ( false ) ) . not . toBeInstanceOf ( Promise )
76+ } )
77+
78+ it . for ( [
79+ '' ,
80+ 2 ,
81+ [ ] ,
82+ ] ) ( 'should handle non-promises and return a non-promise target to have the correct runtime type' , ( actualPromise ) => {
83+ const softService = SoftAssertService . getInstance ( )
84+ softService . setCurrentTest ( 'non-promise-2' , 'test name' , 'test file' )
85+
86+ const wdioExpect = expectWdio ( actualPromise )
87+ expect ( wdioExpect ) . not . toBeInstanceOf ( Promise )
88+
89+ const softExpect = expectWdio . soft ( actualPromise )
90+ expect ( softExpect ) . toBeInstanceOf ( Object )
91+ expect ( softExpect ) . not . toBeInstanceOf ( Promise )
92+ } )
93+
2294 it ( 'should not throw immediately on failure' , async ( ) => {
2395 const softService = SoftAssertService . getInstance ( )
2496 softService . setCurrentTest ( 'test-1' , 'test name' , 'test file' )
@@ -108,20 +180,48 @@ describe('Soft Assertions', () => {
108180 expect ( expectWdio . getSoftFailures ( ) . length ) . toBe ( 0 )
109181 } )
110182
111- /**
112- * TODO: Skipped since soft assertions are currently not supporting basic matchers like toBe or toEqual. To fix one day!
113- * @see https://github.com/webdriverio/expect-webdriverio/issues/1887
114- */
115- it . skip ( 'should support basic text matching' , async ( ) => {
116- const softService = SoftAssertService . getInstance ( )
117- softService . setCurrentTest ( 'test-7' , 'test name' , 'test file' )
118- const text = await el . getText ( )
183+ describe ( 'Basic Matchers Support' , ( ) => {
184+ it ( 'should support basic matchers failure without await' , ( ) => {
185+ const softService = SoftAssertService . getInstance ( )
186+ softService . setCurrentTest ( 'test-7' , 'test name' , 'test file' )
119187
120- expectWdio . soft ( text ) . toEqual ( '!Actual Text' )
188+ expectWdio . soft ( 'Actual Text' ) . toEqual ( '!Actual Text' )
121189
122- const failures = expectWdio . getSoftFailures ( )
123- expect ( failures . length ) . toBe ( 1 )
124- expect ( failures [ 0 ] . matcherName ) . toBe ( 'toHaveText' )
190+ const failures = expectWdio . getSoftFailures ( )
191+ expect ( failures . length ) . toBe ( 1 )
192+ expect ( failures [ 0 ] . matcherName ) . toBe ( 'toEqual' )
193+ } )
194+
195+ it ( 'should support basic matchers success' , async ( ) => {
196+ const softService = SoftAssertService . getInstance ( )
197+ softService . setCurrentTest ( 'test-8' , 'test name' , 'test file' )
198+
199+ expectWdio . soft ( 'Actual Text' ) . toEqual ( 'Actual Text' )
200+
201+ const failures = expectWdio . getSoftFailures ( )
202+ expect ( failures . length ) . toBe ( 0 )
203+ } )
204+
205+ it ( 'not - should support basic matchers failure without await' , async ( ) => {
206+ const softService = SoftAssertService . getInstance ( )
207+ softService . setCurrentTest ( 'test-9' , 'test name' , 'test file' )
208+
209+ expectWdio . soft ( 'Actual Text' ) . not . toEqual ( 'Actual Text' )
210+
211+ const failures = expectWdio . getSoftFailures ( )
212+ expect ( failures . length ) . toBe ( 1 )
213+ expect ( failures [ 0 ] . matcherName ) . toBe ( 'not.toEqual' )
214+ } )
215+
216+ it . skip ( 'not - should support basic matcher success' , async ( ) => {
217+ const softService = SoftAssertService . getInstance ( )
218+ softService . setCurrentTest ( 'test-10' , 'test name' , 'test file' )
219+
220+ expectWdio . soft ( 'Actual Text' ) . not . toEqual ( 'Not Actual Text' )
221+
222+ const failures = expectWdio . getSoftFailures ( )
223+ expect ( failures . length ) . toBe ( 0 )
224+ } )
125225 } )
126226
127227 } )
0 commit comments