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

feat: added jest expect support #66

Merged
merged 15 commits into from
Dec 12, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,19 @@
"cli-truncate": "^3.1.0",
"diff": "^5.0.0",
"elegant-spinner": "^3.0.0",
"expect": "^27.4.2",
sheremet-va marked this conversation as resolved.
Show resolved Hide resolved
"fast-glob": "^3.2.7",
"figures": "^4.0.0",
"find-up": "^6.2.0",
"happy-dom": "^2.24.5",
"jest-matcher-utils": "^27.4.2",
"jest-snapshot": "^27.4.2",
"jest-util": "^27.4.2",
"jsdom": "^19.0.0",
"nanoid": "^3.1.30",
"log-symbols": "^4.1.0",
"log-update": "^5.0.0",
"mlly": "^0.3.15",
"nanoid": "^3.1.30",
"picocolors": "^1.0.0",
"piscina": "^3.1.0",
"sade": "^1.7.4",
Expand Down
119 changes: 94 additions & 25 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { MatchersObject } from 'expect/build/types'
import { UserOptions } from './types'

export * from './types'
Expand All @@ -16,6 +17,10 @@ declare module 'vite' {

declare global {
namespace Chai {
interface ExpectStatic {
extend(expects: MatchersObject): void
}

interface Assertion {
// Snapshot
toMatchSnapshot(message?: string): Assertion
Expand Down
78 changes: 78 additions & 0 deletions src/integrations/chai/jest-extend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import chai, { util } from 'chai'

import { MatcherState } from 'expect'
import { MatchersObject, SyncExpectationResult } from 'expect/build/types'

import {
iterableEquality,
subsetEquality,
} from 'expect/build/utils'
import * as matcherUtils from 'jest-matcher-utils'
import { ChaiPlugin } from './types'

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

const getMatcherState = (assertion: Chai.AssertionStatic & Chai.Assertion) => {
const actual = assertion._obj
const isNot = util.flag(assertion, 'negate') as boolean
const jestUtils = {
...matcherUtils,
iterableEquality,
subsetEquality,
}

// TODO add to ctx - { error, promise, equals, ...getState() }
const matcherState: MatcherState = {
isNot,
utils: jestUtils,
assertionCalls: 0,
promise: '',
equals: () => true,
suppressedErrors: [],
}

return {
state: matcherState,
isNot,
actual,
}
}

function JestExtendPlugin(expects: MatchersObject): ChaiPlugin {
return (c, utils) => {
Object.entries(expects).forEach(([expectAssertionName, expectAssertion]) => {
function expectSyncWrapper(this: Chai.AssertionStatic & Chai.Assertion, ...args: any[]) {
const { state, isNot, actual } = getMatcherState(this)

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

if ((pass && isNot) || (!pass && !isNot))
c.expect.fail(message())
}

async function expectAsyncWrapper(this: Chai.AssertionStatic & Chai.Assertion, ...args: any[]) {
const { state, isNot, actual } = getMatcherState(this)

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

if ((pass && isNot) || (!pass && !isNot))
c.expect.fail(message())
}

const expectAssertionWrapper = isAsyncFunction(expectAssertion) ? expectAsyncWrapper : expectSyncWrapper

utils.addMethod(chai.Assertion.prototype, expectAssertionName, expectAssertionWrapper)
})
}
}

export function JestExtend(): ChaiPlugin {
return (chai, utils) => {
utils.addMethod(chai.expect, 'extend', (expects: MatchersObject) => {
chai.use(JestExtendPlugin(expects))
})
}
}
Loading