Skip to content

Commit 9d6202d

Browse files
committed
feat(experimental): expose assertion as a public field
1 parent 203f07a commit 9d6202d

4 files changed

Lines changed: 12 additions & 9 deletions

File tree

docs/guide/extending-matchers.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ The name of the current [`environment`](/config/environment) (for example, `jsdo
155155

156156
Was assertion called as a [`soft`](/api/expect#soft) one. You don't need to respect it, Vitest will always catch the error.
157157

158+
## `assertion` <Advanced /> <Version type="experimental">4.1.4</Version> {#assertion}
159+
160+
The underlying [Chai assertion](https://www.chaijs.com/guide/plugins/) object. This is the same instance that Chai plugins receive, giving you access to Chai's flag system and chainable methods. This can be useful for building custom matchers that need to interact with Chai's internals.
161+
158162
::: tip
159163
These are not all of the available properties, only the most useful ones. The other state values are used by Vitest internally.
160164
:::

packages/expect/src/jest-extend.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function getMatcherState(
5353
suppressedErrors: [],
5454
soft: util.flag(assertion, 'soft') as boolean | undefined,
5555
poll: util.flag(assertion, 'poll') as boolean | undefined,
56-
__vitest_assertion__: assertion as any,
56+
assertion: assertion as any,
5757
}
5858
Object.assign(matcherState, { task })
5959

packages/expect/src/types.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,11 @@ export interface MatcherState {
8383
soft?: boolean
8484
poll?: boolean
8585
/**
86-
* this allows `expect.extend`-based custom matcher
87-
* to implement builtin vitest/chai assertion equivalent feature.
88-
* this used for custom snapshot matcher API.
86+
* The same assertion instance that chai plugins receive.
87+
* @experimental
88+
* @see {@link https://www.chaijs.com/guide/plugins/} Core Plugin Concepts
8989
*/
90-
/** @internal */
91-
__vitest_assertion__: Assertion
90+
readonly assertion: Assertion
9291
}
9392

9493
export interface SyncExpectationResult {

packages/vitest/src/integrations/snapshot/chai.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ export const Snapshots = {
301301
hint?: string,
302302
): SyncExpectationResult {
303303
return toMatchSnapshotImpl({
304-
assertion: this.__vitest_assertion__,
304+
assertion: this.assertion,
305305
received,
306306
...normalizeArguments(propertiesOrHint, hint),
307307
})
@@ -334,7 +334,7 @@ export const Snapshots = {
334334
hint?: string,
335335
): SyncExpectationResult {
336336
return toMatchSnapshotImpl({
337-
assertion: this.__vitest_assertion__,
337+
assertion: this.assertion,
338338
received,
339339
isInline: true,
340340
...normalizeInlineArguments(propertiesOrInlineSnapshot, inlineSnapshotOrHint, hint),
@@ -367,7 +367,7 @@ export const Snapshots = {
367367
hint?: string,
368368
): AsyncExpectationResult {
369369
return toMatchFileSnapshotImpl({
370-
assertion: this.__vitest_assertion__,
370+
assertion: this.assertion,
371371
received,
372372
filepath,
373373
hint,

0 commit comments

Comments
 (0)