Skip to content
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

fix: allow custom async matchers #2707

Merged
merged 1 commit into from Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 11 additions & 17 deletions packages/expect/src/jest-extend.ts
Expand Up @@ -17,9 +17,6 @@ import {
subsetEquality,
} from './jest-utils'

const isAsyncFunction = (fn: unknown) =>
typeof fn === 'function' && (fn as any)[Symbol.toStringTag] === 'AsyncFunction'

const getMatcherState = (assertion: Chai.AssertionStatic & Chai.Assertion, expect: Vi.ExpectStatic) => {
const obj = assertion._obj
const isNot = util.flag(assertion, 'negate') as boolean
Expand Down Expand Up @@ -56,30 +53,27 @@ class JestExtendError extends Error {
function JestExtendPlugin(expect: Vi.ExpectStatic, matchers: MatchersObject): ChaiPlugin {
return (c, utils) => {
Object.entries(matchers).forEach(([expectAssertionName, expectAssertion]) => {
function expectSyncWrapper(this: Chai.AssertionStatic & Chai.Assertion, ...args: any[]) {
function expectWrapper(this: Chai.AssertionStatic & Chai.Assertion, ...args: any[]) {
const { state, isNot, obj } = getMatcherState(this, expect)

// @ts-expect-error args wanting tuple
const { pass, message, actual, expected } = expectAssertion.call(state, obj, ...args) as SyncExpectationResult

if ((pass && isNot) || (!pass && !isNot))
throw new JestExtendError(message(), actual, expected)
}
const result = expectAssertion.call(state, obj, ...args)

async function expectAsyncWrapper(this: Chai.AssertionStatic & Chai.Assertion, ...args: any[]) {
const { state, isNot, obj } = getMatcherState(this, expect)
if (result && typeof result === 'object' && result instanceof Promise) {
return result.then(({ pass, message, actual, expected }) => {
if ((pass && isNot) || (!pass && !isNot))
throw new JestExtendError(message(), actual, expected)
})
}

// @ts-expect-error args wanting tuple
const { pass, message, actual, expected } = await expectAssertion.call(state, obj, ...args) as SyncExpectationResult
const { pass, message, actual, expected } = result

if ((pass && isNot) || (!pass && !isNot))
throw new JestExtendError(message(), actual, expected)
}

const expectAssertionWrapper = isAsyncFunction(expectAssertion) ? expectAsyncWrapper : expectSyncWrapper

utils.addMethod((globalThis as any)[JEST_MATCHERS_OBJECT].matchers, expectAssertionName, expectAssertionWrapper)
utils.addMethod(c.Assertion.prototype, expectAssertionName, expectAssertionWrapper)
utils.addMethod((globalThis as any)[JEST_MATCHERS_OBJECT].matchers, expectAssertionName, expectWrapper)
utils.addMethod(c.Assertion.prototype, expectAssertionName, expectWrapper)

class CustomMatcher extends AsymmetricMatcher<[unknown, ...unknown[]]> {
constructor(inverse = false, ...sample: [unknown, ...unknown[]]) {
Expand Down
28 changes: 27 additions & 1 deletion test/core/test/jest-expect.test.ts
Expand Up @@ -8,6 +8,9 @@ class TestError extends Error {}
// For expect.extend
interface CustomMatchers<R = unknown> {
toBeDividedBy(divisor: number): R
toBeTestedAsync(): Promise<R>
toBeTestedSync(): R
toBeTestedPromise(): R
}
declare global {
namespace Vi {
Expand Down Expand Up @@ -142,7 +145,7 @@ describe('jest-expect', () => {
expect(['Bob', 'Eve']).toEqual(expect.not.arrayContaining(['Steve']))
})

it('expect.extend', () => {
it('expect.extend', async () => {
expect.extend({
toBeDividedBy(received, divisor) {
const pass = received % divisor === 0
Expand All @@ -161,6 +164,24 @@ describe('jest-expect', () => {
}
}
},
async toBeTestedAsync() {
return {
pass: false,
message: () => 'toBeTestedAsync',
}
},
toBeTestedSync() {
return {
pass: false,
message: () => 'toBeTestedSync',
}
},
toBeTestedPromise() {
return Promise.resolve({
pass: false,
message: () => 'toBeTestedPromise',
})
},
})

expect(5).toBeDividedBy(5)
Expand All @@ -169,6 +190,11 @@ describe('jest-expect', () => {
one: expect.toBeDividedBy(1),
two: expect.not.toBeDividedBy(5),
})
expect(() => expect(2).toBeDividedBy(5)).toThrowError()

expect(() => expect(null).toBeTestedSync()).toThrowError('toBeTestedSync')
await expect(async () => await expect(null).toBeTestedAsync()).rejects.toThrowError('toBeTestedAsync')
await expect(async () => await expect(null).toBeTestedPromise()).rejects.toThrowError('toBeTestedPromise')
})

it('object', () => {
Expand Down