-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added jest expect support (#66)
Co-authored-by: Anthony Fu <[email protected]>
- Loading branch information
1 parent
f932bca
commit d9d2075
Showing
12 changed files
with
994 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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,83 @@ | ||
import chai, { util } from 'chai' | ||
|
||
import * as matcherUtils from './jest-matcher-utils' | ||
|
||
import { | ||
iterableEquality, | ||
subsetEquality, | ||
equals, | ||
} from './jest-utils' | ||
import type { | ||
ChaiPlugin, | ||
MatcherState, | ||
MatchersObject, | ||
SyncExpectationResult, | ||
} 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, | ||
} | ||
|
||
const matcherState: MatcherState = { | ||
isNot, | ||
utils: jestUtils, | ||
// global assertionCalls? needed for built-in jest function, but we don't use it | ||
assertionCalls: 0, | ||
promise: '', | ||
equals, | ||
// needed for built-in jest-snapshots, but we don't use it | ||
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 args wanting tuple | ||
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 args wanting tuple | ||
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)) | ||
}) | ||
} | ||
} |
Oops, something went wrong.