/
index.ts
103 lines (84 loc) · 3.26 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// CI failes only for this file, but it works locally
import * as chai from 'chai'
import './setup'
import type { Test } from '@vitest/runner'
import { getCurrentTest } from '@vitest/runner'
import { GLOBAL_EXPECT, getState, setState } from '@vitest/expect'
import type { Assertion, ExpectStatic } from '@vitest/expect'
import type { MatcherState } from '../../types/chai'
import { getCurrentEnvironment, getFullName } from '../../utils'
export function createExpect(test?: Test) {
const expect = ((value: any, message?: string): Assertion => {
const { assertionCalls } = getState(expect)
setState({ assertionCalls: assertionCalls + 1, soft: false }, expect)
const assert = chai.expect(value, message) as unknown as Assertion
const _test = test || getCurrentTest()
if (_test)
// @ts-expect-error internal
return assert.withTest(_test) as Assertion
else
return assert
}) as ExpectStatic
Object.assign(expect, chai.expect)
expect.getState = () => getState<MatcherState>(expect)
expect.setState = state => setState(state as Partial<MatcherState>, expect)
// @ts-expect-error global is not typed
const globalState = getState(globalThis[GLOBAL_EXPECT]) || {}
setState<MatcherState>({
// this should also add "snapshotState" that is added conditionally
...globalState,
assertionCalls: 0,
isExpectingAssertions: false,
isExpectingAssertionsError: null,
expectedAssertionsNumber: null,
expectedAssertionsNumberErrorGen: null,
environment: getCurrentEnvironment(),
testPath: test ? test.suite.file?.filepath : globalState.testPath,
currentTestName: test ? getFullName(test) : globalState.currentTestName,
}, expect)
// @ts-expect-error untyped
expect.extend = matchers => chai.expect.extend(expect, matchers)
expect.soft = (...args) => {
const assert = expect(...args)
expect.setState({
soft: true,
})
return assert
}
expect.unreachable = (message?: string) => {
chai.assert.fail(`expected${message ? ` "${message}" ` : ' '}not to be reached`)
}
function assertions(expected: number) {
const errorGen = () => new Error(`expected number of assertions to be ${expected}, but got ${expect.getState().assertionCalls}`)
if (Error.captureStackTrace)
Error.captureStackTrace(errorGen(), assertions)
expect.setState({
expectedAssertionsNumber: expected,
expectedAssertionsNumberErrorGen: errorGen,
})
}
function hasAssertions() {
const error = new Error('expected any number of assertion, but got none')
if (Error.captureStackTrace)
Error.captureStackTrace(error, hasAssertions)
expect.setState({
isExpectingAssertions: true,
isExpectingAssertionsError: error,
})
}
chai.util.addMethod(expect, 'assertions', assertions)
chai.util.addMethod(expect, 'hasAssertions', hasAssertions)
return expect
}
const globalExpect = createExpect()
Object.defineProperty(globalThis, GLOBAL_EXPECT, {
value: globalExpect,
writable: true,
configurable: true,
})
export { assert, should } from 'chai'
export { chai, globalExpect as expect }
export function setupChaiConfig(config: ChaiConfig) {
Object.assign(chai.config, config)
}
export type ChaiConfig = Omit<Partial<typeof chai.config>, 'useProxy' | 'proxyExcludedKeys'>