-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
166997d
commit d00dc29
Showing
4 changed files
with
157 additions
and
17 deletions.
There are no files selected for viewing
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
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,84 @@ | ||
import * as chai from 'chai'; | ||
import { GLOBAL_EXPECT, getState, setState } from '@vitest/expect'; | ||
import type { Assertion, ExpectStatic } from '@vitest/expect'; | ||
import type { MatcherState } from '@vitest/expect'; | ||
|
||
export function createExpect(test?: any) { | ||
const expect: ExpectStatic = chai.expect as ExpectStatic; | ||
|
||
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, | ||
testPath: test ? test.suite.file?.filepath : globalState.testPath, | ||
}, | ||
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 }; |
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