-
-
Notifications
You must be signed in to change notification settings - Fork 54
feat: add soft assertions feature #1836
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
christian-bromann
merged 7 commits into
webdriverio:main
from
JustasMonkev:feat/soft-assertion
May 29, 2025
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
27af273
feat: add soft assertions feature
JustasMonkev 13cf6ca
chore: implement suggestion
JustasMonkev 2547329
Update docs/API.md
christian-bromann cecd54a
Update src/index.ts
christian-bromann 8dbf1ff
chore: fix tests
JustasMonkev 11aced2
chore: adjust vitest threshold
JustasMonkev 06c5d39
Update docs/API.md
christian-bromann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import type { AssertionError } from 'node:assert' | ||
|
||
interface SoftFailure { | ||
error: AssertionError | Error; | ||
matcherName: string; | ||
location?: string; | ||
} | ||
|
||
interface TestIdentifier { | ||
id: string; | ||
name?: string; | ||
file?: string; | ||
} | ||
|
||
/** | ||
* Soft assertion service to collect failures without stopping test execution | ||
*/ | ||
export class SoftAssertService { | ||
private static instance: SoftAssertService | ||
private failureMap: Map<string, SoftFailure[]> = new Map() | ||
private currentTest: TestIdentifier | null = null | ||
|
||
private constructor() { } | ||
|
||
/** | ||
* Get singleton instance | ||
*/ | ||
public static getInstance(): SoftAssertService { | ||
if (!SoftAssertService.instance) { | ||
SoftAssertService.instance = new SoftAssertService() | ||
} | ||
return SoftAssertService.instance | ||
} | ||
|
||
/** | ||
* Set the current test context | ||
*/ | ||
public setCurrentTest(testId: string, testName?: string, testFile?: string): void { | ||
this.currentTest = { id: testId, name: testName, file: testFile } | ||
if (!this.failureMap.has(testId)) { | ||
this.failureMap.set(testId, []) | ||
} | ||
} | ||
|
||
/** | ||
* Clear the current test context | ||
*/ | ||
public clearCurrentTest(): void { | ||
this.currentTest = null | ||
} | ||
|
||
/** | ||
* Get current test ID | ||
*/ | ||
public getCurrentTestId(): string | null { | ||
return this.currentTest?.id || null | ||
} | ||
|
||
/** | ||
* Add a soft failure for the current test | ||
*/ | ||
public addFailure(error: Error, matcherName: string): void { | ||
const testId = this.getCurrentTestId() | ||
if (!testId) { | ||
throw error // If no test context, throw the error immediately | ||
} | ||
|
||
// Extract stack information to get file and line number | ||
const stackLines = error.stack?.split('\n') || [] | ||
let location = '' | ||
|
||
// Find the first non-expect-webdriverio line in the stack | ||
for (const line of stackLines) { | ||
if (line && !line.includes('expect-webdriverio') && !line.includes('node_modules')) { | ||
location = line.trim() | ||
break | ||
} | ||
} | ||
|
||
const failures = this.failureMap.get(testId) || [] | ||
failures.push({ error, matcherName, location }) | ||
this.failureMap.set(testId, failures) | ||
} | ||
|
||
/** | ||
* Get all failures for a specific test | ||
*/ | ||
public getFailures(testId?: string): SoftFailure[] { | ||
const id = testId || this.getCurrentTestId() | ||
if (!id) { | ||
return [] | ||
} | ||
return this.failureMap.get(id) || [] | ||
} | ||
|
||
/** | ||
* Clear failures for a specific test | ||
*/ | ||
public clearFailures(testId?: string): void { | ||
const id = testId || this.getCurrentTestId() | ||
if (id) { | ||
this.failureMap.delete(id) | ||
} | ||
} | ||
|
||
/** | ||
* Throw an aggregated error if there are failures for the current test | ||
*/ | ||
public assertNoFailures(testId?: string): void { | ||
const id = testId || this.getCurrentTestId() | ||
if (!id) { | ||
return | ||
} | ||
|
||
const failures = this.getFailures(id) | ||
if (failures.length === 0) { | ||
return | ||
} | ||
|
||
// Create a formatted error message with all failures | ||
let message = `${failures.length} soft assertion failure${failures.length > 1 ? 's' : ''}:\n\n` | ||
|
||
failures.forEach((failure, index) => { | ||
message += `${index + 1}) ${failure.matcherName}: ${failure.error.message}\n` | ||
if (failure.location) { | ||
message += ` at ${failure.location}\n` | ||
} | ||
message += '\n' | ||
}) | ||
|
||
// Clear failures for this test to prevent duplicate reporting | ||
this.clearFailures(id) | ||
|
||
// Throw an aggregated error | ||
const error = new Error(message) | ||
error.name = 'SoftAssertionsError' | ||
throw error | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.