Skip to content

Commit bcfe508

Browse files
committed
Document more findings around soft assertions
1 parent 7e58f77 commit bcfe508

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

docs/Framework.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,33 @@ beforeAll(async () => {
6868
expect.extend(matchers as Record<string, any>);
6969
});
7070
```
71-
For the soft assertion, it needs to expose the `createSoftExpect` first.
7271

72+
For the soft assertion, the `createSoftExpect` is currently not correctly exposed but the below works:
73+
```ts
74+
// @ts-ignore
75+
import * as createSoftExpect from "expect-webdriverio/lib/softExpect";
76+
77+
beforeAll(async () => {
78+
Object.defineProperty(expect, "soft", {
79+
value: <T = unknown>(actual: T) => createSoftExpect.default(actual),
80+
});
81+
82+
// Add soft assertions utility methods
83+
Object.defineProperty(expect, "getSoftFailures", {
84+
value: (testId?: string) => SoftAssertService.getInstance().getFailures(testId),
85+
});
86+
87+
Object.defineProperty(expect, "assertSoftFailures", {
88+
value: (testId?: string) => SoftAssertService.getInstance().assertNoFailures(testId),
89+
});
90+
91+
Object.defineProperty(expect, "clearSoftFailures", {
92+
value: (testId?: string) => SoftAssertService.getInstance().clearFailures(testId),
93+
});
94+
});
95+
```
7396

74-
As shown below, no imports are required and we can use WDIO matchers directly on Jest's `expect`:
97+
Then as shown below, no imports are required and we can use WDIO matchers directly on Jest's `expect`:
7598
```ts
7699
describe('My tests', async () => {
77100

test/softAssertions.test.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ vi.mock('@wdio/globals')
77

88
describe('Soft Assertions', () => {
99
// Setup a mock element for testing
10-
let el: any
10+
let el: ChainablePromiseElement
1111

1212
beforeEach(async () => {
1313
el = $('sel')
@@ -106,6 +106,20 @@ describe('Soft Assertions', () => {
106106
// Should be no failures now
107107
expect(expectWdio.getSoftFailures().length).toBe(0)
108108
})
109+
110+
// TODO: Soft are currently not supporting basic matchers like toBe or toEqual.To fix one day!
111+
it.skip('should support basic text matching', async () => {
112+
const softService = SoftAssertService.getInstance()
113+
softService.setCurrentTest('test-7', 'test name', 'test file')
114+
const text = await el.getText()
115+
116+
expectWdio.soft(text).toEqual('!Actual Text')
117+
118+
const failures = expectWdio.getSoftFailures()
119+
expect(failures.length).toBe(1)
120+
expect(failures[0].matcherName).toBe('toHaveText')
121+
})
122+
109123
})
110124

111125
describe('SoftAssertService hooks', () => {

0 commit comments

Comments
 (0)