forked from webdriverio/expect-webdriverio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsoftAssertions.test.ts
More file actions
506 lines (391 loc) · 19.8 KB
/
softAssertions.test.ts
File metadata and controls
506 lines (391 loc) · 19.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
import { describe, it, expect, beforeEach, vi } from 'vitest'
import { $ } from '@wdio/globals'
import { expect as expectWdio, SoftAssertionService, SoftAssertService } from '../src/index.js'
vi.mock('@wdio/globals')
describe('Soft Assertions', () => {
// Setup a mock element for testing
let el: ChainablePromiseElement
beforeEach(async () => {
el = $('sel')
// We need to mock getText() which is what the toHaveText matcher actually calls
el.getText = vi.fn().mockImplementation(() => 'Actual Text')
// Clear any soft assertion failures before each test
expectWdio.clearSoftFailures()
})
describe('expect.soft', () => {
it('should not throw immediately on failure', async () => {
const softService = SoftAssertService.getInstance()
softService.setCurrentTest('test-1', 'test name', 'test file')
await expectWdio.soft(el).toHaveText('Expected Text')
// Verify the failure was recorded
const failures = expectWdio.getSoftFailures()
expect(failures.length).toBe(1)
expect(failures[0].matcherName).toBe('toHaveText')
expect(failures[0].error.message).toContain('text')
})
it('should support chained assertions with .not', async () => {
// Setup a test ID for this test
const softService = SoftAssertService.getInstance()
softService.setCurrentTest('test-2', 'test name', 'test file')
// This should not throw even though it fails
await expectWdio.soft(el).not.toHaveText('Actual Text')
// Verify the failure was recorded
const failures = expectWdio.getSoftFailures()
expect(failures.length).toBe(1)
expect(failures[0].matcherName).toBe('not.toHaveText')
})
it('should support multiple soft failures in the same test', async () => {
// Setup a test ID for this test
const softService = SoftAssertService.getInstance()
softService.setCurrentTest('test-3', 'test name', 'test file')
// These should not throw even though they fail
await expectWdio.soft(el).toHaveText('First Expected')
await expectWdio.soft(el).toHaveText('Second Expected')
await expectWdio.soft(el).toHaveText('Third Expected')
// Verify all failures were recorded
const failures = expectWdio.getSoftFailures()
expect(failures.length).toBe(3)
expect(failures[0].matcherName).toBe('toHaveText')
expect(failures[1].matcherName).toBe('toHaveText')
expect(failures[2].matcherName).toBe('toHaveText')
})
it('should allow passing assertions', async () => {
// Set up a test ID for this test
const softService = SoftAssertService.getInstance()
softService.setCurrentTest('test-4', 'test name', 'test file')
// This should pass normally
await expectWdio.soft(el).toHaveText('Actual Text')
// Verify no failures were recorded
const failures = expectWdio.getSoftFailures()
expect(failures.length).toBe(0)
})
it('assertSoftFailures should throw if failures exist', async () => {
// Setup a test ID for this test
const softService = SoftAssertService.getInstance()
softService.setCurrentTest('test-5', 'test name', 'test file')
// Record a failure
await expectWdio.soft(el).toHaveText('Expected Text')
// Should throw when asserting failures
await expect(() => expectWdio.assertSoftFailures()).toThrow(/1 soft assertion failure/)
})
it('clearSoftFailures should remove all failures', async () => {
// Setup a test ID for this test
const softService = SoftAssertService.getInstance()
softService.setCurrentTest('test-6', 'test name', 'test file')
// Record failures
await expectWdio.soft(el).toHaveText('First Expected')
await expectWdio.soft(el).toHaveText('Second Expected')
// Verify failures were recorded
expect(expectWdio.getSoftFailures().length).toBe(2)
// Clear failures
expectWdio.clearSoftFailures()
// Should be no failures now
expect(expectWdio.getSoftFailures().length).toBe(0)
})
/**
* TODO: Skipped since soft assertions are currently not supporting basic matchers like toBe or toEqual. To fix one day!
* @see https://github.com/webdriverio/expect-webdriverio/issues/1887
*/
it.skip('should support basic text matching', async () => {
const softService = SoftAssertService.getInstance()
softService.setCurrentTest('test-7', 'test name', 'test file')
const text = await el.getText()
expectWdio.soft(text).toEqual('!Actual Text')
const failures = expectWdio.getSoftFailures()
expect(failures.length).toBe(1)
expect(failures[0].matcherName).toBe('toHaveText')
})
})
describe('SoftAssertService hooks', () => {
it('afterTest should throw if soft failures exist', () => {
const softService = SoftAssertService.getInstance()
softService.setCurrentTest('test-hooks-1', 'test hooks', 'test file')
const error = new Error('Test failure')
softService.addFailure(error, 'toBeDisplayed')
// Create mock test result object
const testResult = { passed: true, error: 'undefined' } as TestResult
// Create a mock service
const service = new SoftAssertionService()
// Call afterTest hook - this should update the result
service.afterTest({
file: 'test file', parent: '', title: 'test hooks',
fullName: '',
ctx: undefined,
type: '',
fullTitle: '',
pending: false
}, null, testResult)
// Verify the test result was updated
expect(testResult.passed).toBe(true)
expect(testResult.error).toBeDefined()
})
})
describe('Different Matcher Types', () => {
beforeEach(async () => {
el = $('sel')
// Mock different methods for different matchers
el.getText = vi.fn().mockImplementation(() => 'Actual Text')
el.isDisplayed = vi.fn().mockImplementation(() => false)
el.getAttribute = vi.fn().mockImplementation(() => 'actual-class')
el.isClickable = vi.fn().mockImplementation(() => false)
expectWdio.clearSoftFailures()
})
it('should handle boolean matchers', async () => {
const softService = SoftAssertService.getInstance()
softService.setCurrentTest('boolean-test', 'boolean test', 'test file')
// Test boolean matcher
await expectWdio.soft(el).toBeDisplayed()
await expectWdio.soft(el).toBeClickable()
const failures = expectWdio.getSoftFailures()
expect(failures.length).toBe(2)
expect(failures[0].matcherName).toBe('toBeDisplayed')
expect(failures[1].matcherName).toBe('toBeClickable')
})
it('should handle attribute matchers with multiple parameters', async () => {
const softService = SoftAssertService.getInstance()
softService.setCurrentTest('attribute-test', 'attribute test', 'test file')
await expectWdio.soft(el).toHaveAttribute('class', 'expected-class')
const failures = expectWdio.getSoftFailures()
expect(failures.length).toBe(1)
expect(failures[0].matcherName).toBe('toHaveAttribute')
})
it('should handle matchers with options', async () => {
const softService = SoftAssertService.getInstance()
softService.setCurrentTest('options-test', 'options test', 'test file')
await expectWdio.soft(el).toHaveText('Expected', { ignoreCase: true, wait: 1000 })
const failures = expectWdio.getSoftFailures()
expect(failures.length).toBe(1)
expect(failures[0].matcherName).toBe('toHaveText')
})
})
describe('Test Isolation', () => {
it('should isolate failures between different test contexts', async () => {
const softService = SoftAssertService.getInstance()
// Test 1
softService.setCurrentTest('isolation-test-1', 'test 1', 'file1')
await expectWdio.soft(el).toHaveText('Expected Text 1')
expect(expectWdio.getSoftFailures().length).toBe(1)
// Test 2 - should have separate failures
softService.setCurrentTest('isolation-test-2', 'test 2', 'file2')
await expectWdio.soft(el).toHaveText('Expected Text 2')
// Test 2 should only see its own failure
expect(expectWdio.getSoftFailures('isolation-test-2').length).toBe(1)
expect(expectWdio.getSoftFailures('isolation-test-1').length).toBe(1)
// Clear one test shouldn't affect the other
expectWdio.clearSoftFailures('isolation-test-1')
expect(expectWdio.getSoftFailures('isolation-test-1').length).toBe(0)
expect(expectWdio.getSoftFailures('isolation-test-2').length).toBe(1)
})
it('should handle calls without test context using global fallback ID', async () => {
const softService = SoftAssertService.getInstance()
softService.clearCurrentTest() // No test context
// Should NOT throw - instead should store under global fallback ID
await expectWdio.soft(el).toHaveText('Expected Text')
// Failures should be stored under the global ID
const failures = expectWdio.getSoftFailures(SoftAssertService.GLOBAL_TEST_ID)
expect(failures.length).toBe(1)
expect(failures[0].matcherName).toBe('toHaveText')
// Clean up
expectWdio.clearSoftFailures(SoftAssertService.GLOBAL_TEST_ID)
})
it('should handle rapid concurrent soft assertions', async () => {
const softService = SoftAssertService.getInstance()
softService.setCurrentTest('concurrent-test', 'concurrent', 'test file')
el.getText = vi.fn().mockImplementation(() => 'Actual Text')
el.isDisplayed = vi.fn().mockImplementation(() => false)
el.isClickable = vi.fn().mockImplementation(() => false)
// Fire multiple assertions rapidly
const promises = [
expectWdio.soft(el).toHaveText('Expected 1'),
expectWdio.soft(el).toBeDisplayed(),
expectWdio.soft(el).toBeClickable()
]
await Promise.all(promises)
const failures = expectWdio.getSoftFailures()
expect(failures.length).toBe(3)
// Verify all different matchers were recorded
const matcherNames = failures.map(f => f.matcherName)
expect(matcherNames).toContain('toHaveText')
expect(matcherNames).toContain('toBeDisplayed')
expect(matcherNames).toContain('toBeClickable')
})
})
describe('Edge Cases & Error Handling', () => {
it('should handle matcher that throws non-standard errors', async () => {
const softService = SoftAssertService.getInstance()
softService.setCurrentTest('error-test', 'error test', 'test file')
// Mock a matcher that throws a unique error
const originalMethod = el.getText
el.getText = vi.fn().mockImplementation(() => {
throw new TypeError('Weird browser error')
})
await expectWdio.soft(el).toHaveText('Expected Text')
const failures = expectWdio.getSoftFailures()
expect(failures.length).toBe(1)
expect(failures[0].error).toBeInstanceOf(Error)
expect(failures[0].error.message).toContain('Weird browser error')
// Restore
el.getText = originalMethod
})
it('should handle very long error messages', async () => {
const softService = SoftAssertService.getInstance()
softService.setCurrentTest('long-error-test', 'long error', 'test file')
const veryLongText = 'A'.repeat(10000)
await expectWdio.soft(el).toHaveText(veryLongText)
const failures = expectWdio.getSoftFailures()
expect(failures.length).toBe(1)
expect(failures[0].error.message.length).toBeGreaterThan(0)
})
it('should handle null/undefined values gracefully', async () => {
const softService = SoftAssertService.getInstance()
softService.setCurrentTest('null-test', 'null test', 'test file')
// Test with null/undefined values
await expectWdio.soft(el).toHaveText(null as any)
await expectWdio.soft(el).toHaveAttribute('class', undefined as any)
const failures = expectWdio.getSoftFailures()
expect(failures.length).toBe(2)
})
it('should capture error location information', async () => {
const softService = SoftAssertService.getInstance()
softService.setCurrentTest('location-test', 'location test', 'test file')
await expectWdio.soft(el).toHaveText('Expected Text')
const failures = expectWdio.getSoftFailures()
expect(failures.length).toBe(1)
// Should have location info (if implemented)
if (failures[0].location) {
expect(failures[0].location).toBeTruthy()
expect(typeof failures[0].location).toBe('string')
}
})
it('should handle maximum failure limits', async () => {
const softService = SoftAssertService.getInstance()
softService.setCurrentTest('limit-test', 'limit test', 'test file')
// Generate many failures
const promises = []
for (let i = 0; i < 150; i++) {
promises.push(expectWdio.soft(el).toHaveText(`Expected ${i}`))
}
await Promise.all(promises)
const failures = expectWdio.getSoftFailures()
// Should either limit failures or handle large numbers gracefully
expect(failures.length).toBeGreaterThan(0)
expect(failures.length).toBeLessThanOrEqual(150)
})
})
describe('SoftAssertionService Configuration', () => {
beforeEach(() => {
expectWdio.clearSoftFailures()
})
it('should auto-assert failures by default', () => {
const softService = SoftAssertService.getInstance()
const testId = 'test file::config default'
softService.setCurrentTest(testId, 'config default', 'test file')
const error = new Error('Test failure')
softService.addFailure(error, 'toBeDisplayed')
const service = new SoftAssertionService()
const testResult = { passed: true, error: undefined } as TestResult
service.afterTest({
file: 'test file',
parent: '',
title: 'config default',
fullName: '',
ctx: undefined,
type: '',
fullTitle: '',
pending: false
}, null, testResult)
expect(testResult.passed).toBe(false)
expect(testResult.error).toBeDefined()
})
it('should not auto-assert when autoAssertOnTestEnd is false', () => {
const softService = SoftAssertService.getInstance()
const testId = 'test file::config disabled'
softService.setCurrentTest(testId, 'config disabled', 'test file')
const error = new Error('Test failure')
softService.addFailure(error, 'toBeDisplayed')
const service = new SoftAssertionService({ autoAssertOnTestEnd: false })
// Create mock test result object
const testResult = { passed: true, error: undefined } as TestResult
// Call afterTest hook - should NOT update the result because auto-assert is disabled
service.afterTest({
file: 'test file',
parent: '',
title: 'config disabled',
fullName: '',
ctx: undefined,
type: '',
fullTitle: '',
pending: false
}, null, testResult)
expect(testResult.passed).toBe(true)
expect(testResult.error).toBeUndefined()
const failures = expectWdio.getSoftFailures(testId)
expect(failures.length).toBe(1)
})
it('should still auto-assert with explicit autoAssertOnTestEnd: true', () => {
const softService = SoftAssertService.getInstance()
const testId = 'test file::config explicit'
softService.setCurrentTest(testId, 'config explicit', 'test file')
const error = new Error('Test failure')
softService.addFailure(error, 'toBeDisplayed')
const service = new SoftAssertionService({ autoAssertOnTestEnd: true })
const testResult = { passed: true, error: undefined } as TestResult
service.afterTest({
file: 'test file',
parent: '',
title: 'config explicit',
fullName: '',
ctx: undefined,
type: '',
fullTitle: '',
pending: false
}, null, testResult)
expect(testResult.passed).toBe(false)
expect(testResult.error).toBeDefined()
})
it('should skip auto-assert if test already has an error', () => {
const softService = SoftAssertService.getInstance()
softService.setCurrentTest('config-existing-error-test', 'config existing error', 'test file')
const error = new Error('Soft assertion failure')
softService.addFailure(error, 'toBeDisplayed')
const service = new SoftAssertionService()
const existingError = new Error('Pre-existing test error')
const testResult = { passed: false, error: existingError } as TestResult
service.afterTest({
file: 'test file',
parent: '',
title: 'config existing error',
fullName: '',
ctx: undefined,
type: '',
fullTitle: '',
pending: false
}, null, testResult)
expect(testResult.passed).toBe(false)
expect(testResult.error).toBe(existingError)
expect(testResult.error?.message).toBe('Pre-existing test error')
})
it('should accept undefined options and use defaults', () => {
const service = new SoftAssertionService(undefined)
expect(service).toBeDefined()
const softService = SoftAssertService.getInstance()
const testId = 'test file::config undefined'
softService.setCurrentTest(testId, 'config undefined', 'test file')
const error = new Error('Test failure')
softService.addFailure(error, 'toBeDisplayed')
const testResult = { passed: true, error: undefined } as TestResult
service.afterTest({
file: 'test file',
parent: '',
title: 'config undefined',
fullName: '',
ctx: undefined,
type: '',
fullTitle: '',
pending: false
}, null, testResult)
expect(testResult.passed).toBe(false)
expect(testResult.error).toBeDefined()
})
})
})