|
| 1 | +// This is adapted version of https://github.com/vitest-dev/vitest/blob/main/packages/vitest/src/integrations/chai/index.ts |
| 2 | +// Credits to Vitest team for the original implementation. |
| 3 | + |
| 4 | +import type { Assertion, ExpectStatic, MatcherState } from '@vitest/expect'; |
| 5 | +import { |
| 6 | + addCustomEqualityTesters, |
| 7 | + ASYMMETRIC_MATCHERS_OBJECT, |
| 8 | + customMatchers, |
| 9 | + getState, |
| 10 | + GLOBAL_EXPECT, |
| 11 | + setState, |
| 12 | +} from '@vitest/expect'; |
| 13 | +import * as chai from 'chai'; |
| 14 | + |
| 15 | +// Setup additional matchers |
| 16 | +import './setup.js'; |
| 17 | +import { toMatchImageSnapshot } from './matchers/toMatchImageSnapshot.js'; |
| 18 | + |
| 19 | +export function createExpect(): ExpectStatic { |
| 20 | + const expect = ((value: unknown, message?: string): Assertion => { |
| 21 | + const { assertionCalls } = getState(expect); |
| 22 | + setState({ assertionCalls: assertionCalls + 1 }, expect); |
| 23 | + return chai.expect(value, message) as unknown as Assertion; |
| 24 | + }) as ExpectStatic; |
| 25 | + Object.assign(expect, chai.expect); |
| 26 | + Object.assign( |
| 27 | + expect, |
| 28 | + globalThis[ASYMMETRIC_MATCHERS_OBJECT as unknown as keyof typeof globalThis] |
| 29 | + ); |
| 30 | + |
| 31 | + expect.getState = () => getState<MatcherState>(expect); |
| 32 | + expect.setState = (state) => setState(state as Partial<MatcherState>, expect); |
| 33 | + |
| 34 | + // @ts-expect-error global is not typed |
| 35 | + const globalState = getState(globalThis[GLOBAL_EXPECT]) || {}; |
| 36 | + |
| 37 | + setState<MatcherState>( |
| 38 | + { |
| 39 | + // this should also add "snapshotState" that is added conditionally |
| 40 | + ...globalState, |
| 41 | + assertionCalls: 0, |
| 42 | + isExpectingAssertions: false, |
| 43 | + isExpectingAssertionsError: null, |
| 44 | + expectedAssertionsNumber: null, |
| 45 | + expectedAssertionsNumberErrorGen: null, |
| 46 | + }, |
| 47 | + expect |
| 48 | + ); |
| 49 | + |
| 50 | + // @ts-expect-error untyped |
| 51 | + expect.extend = (matchers) => chai.expect.extend(expect, matchers); |
| 52 | + // @ts-expect-error untyped |
| 53 | + expect.addEqualityTesters = (customTesters) => |
| 54 | + addCustomEqualityTesters(customTesters); |
| 55 | + |
| 56 | + // @ts-expect-error untyped |
| 57 | + expect.soft = (...args) => { |
| 58 | + // @ts-expect-error private soft access |
| 59 | + return expect(...args).withContext({ soft: true }) as Assertion; |
| 60 | + }; |
| 61 | + |
| 62 | + // @ts-expect-error untyped |
| 63 | + expect.unreachable = (message?: string) => { |
| 64 | + chai.assert.fail( |
| 65 | + `expected${message ? ` "${message}" ` : ' '}not to be reached` |
| 66 | + ); |
| 67 | + }; |
| 68 | + |
| 69 | + function assertions(expected: number) { |
| 70 | + const errorGen = () => |
| 71 | + new Error( |
| 72 | + `expected number of assertions to be ${expected}, but got ${ |
| 73 | + expect.getState().assertionCalls |
| 74 | + }` |
| 75 | + ); |
| 76 | + if (Error.captureStackTrace) { |
| 77 | + Error.captureStackTrace(errorGen(), assertions); |
| 78 | + } |
| 79 | + |
| 80 | + expect.setState({ |
| 81 | + expectedAssertionsNumber: expected, |
| 82 | + expectedAssertionsNumberErrorGen: errorGen, |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + function hasAssertions() { |
| 87 | + const error = new Error('expected any number of assertion, but got none'); |
| 88 | + if (Error.captureStackTrace) { |
| 89 | + Error.captureStackTrace(error, hasAssertions); |
| 90 | + } |
| 91 | + |
| 92 | + expect.setState({ |
| 93 | + isExpectingAssertions: true, |
| 94 | + isExpectingAssertionsError: error, |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + chai.util.addMethod(expect, 'assertions', assertions); |
| 99 | + chai.util.addMethod(expect, 'hasAssertions', hasAssertions); |
| 100 | + |
| 101 | + expect.extend(customMatchers); |
| 102 | + expect.extend({ |
| 103 | + toMatchImageSnapshot, |
| 104 | + }); |
| 105 | + |
| 106 | + return expect; |
| 107 | +} |
| 108 | + |
| 109 | +const globalExpect: ExpectStatic = createExpect(); |
| 110 | + |
| 111 | +Object.defineProperty(globalThis, GLOBAL_EXPECT, { |
| 112 | + value: globalExpect, |
| 113 | + writable: true, |
| 114 | + configurable: true, |
| 115 | +}); |
| 116 | + |
| 117 | +export { assert, should } from 'chai'; |
| 118 | +export { chai, globalExpect as expect }; |
| 119 | + |
| 120 | +export type { |
| 121 | + Assertion, |
| 122 | + AsymmetricMatchersContaining, |
| 123 | + DeeplyAllowMatchers, |
| 124 | + ExpectStatic, |
| 125 | + JestAssertion, |
| 126 | + Matchers, |
| 127 | +} from '@vitest/expect'; |
0 commit comments