histoire
Version:
A fast story book powered by Vite
74 lines (73 loc) • 2.95 kB
JavaScript
const SHARED_EXPECT_KEY = Symbol.for('expect-global');
/** Identifies the story facade on the iframe and marks Histoire-owned test tasks. */
export const STORY_EXPECT_SYMBOL = 'histoire:story-expect';
export const STORY_EXPECT_KEY = Symbol.for(STORY_EXPECT_SYMBOL);
/** Reads the shared expect object installed by Vitest (or the preview shim). */
export function getSharedExpect() {
const globals = globalThis;
return globals[STORY_EXPECT_KEY] ?? globals[SHARED_EXPECT_KEY];
}
/** Selects Vitest's context-local expect when a test requested it. */
function getTaskExpect(context) {
return context?._local ? context.expect : getSharedExpect();
}
/**
* Resets the global expect bookkeeping (`assertionCalls`, `expect.assertions(n)`,
* `expect.hasAssertions()`) so they don't leak between consecutive test handlers
* inside the same iframe runtime.
*/
export function resetSharedExpectState() {
const sharedExpect = getSharedExpect();
if (typeof sharedExpect?.setState !== 'function') {
return;
}
sharedExpect.setState({
assertionCalls: 0,
isExpectingAssertions: false,
isExpectingAssertionsError: null,
expectedAssertionsNumber: null,
expectedAssertionsNumberErrorGen: null,
});
}
/**
* Mirrors Vitest's end-of-test assertion bookkeeping checks for the lightweight
* preview expect shim.
*/
export function assertSharedExpectState(context) {
const taskExpect = getTaskExpect(context);
if (typeof taskExpect?.getState !== 'function') {
return;
}
const state = taskExpect.getState();
const assertionCalls = state?.assertionCalls ?? 0;
if (typeof state?.expectedAssertionsNumber === 'number'
&& assertionCalls !== state.expectedAssertionsNumber) {
throw typeof state.expectedAssertionsNumberErrorGen === 'function'
? state.expectedAssertionsNumberErrorGen()
: new Error(`expected number of assertions to be ${state.expectedAssertionsNumber}, but got ${assertionCalls}`);
}
if (state?.isExpectingAssertions && assertionCalls === 0) {
throw state.isExpectingAssertionsError instanceof Error
? state.isExpectingAssertionsError
: new Error('expected any number of assertion, but got none');
}
}
/**
* Completes Vitest's body assertion check before teardown hooks can make more
* assertions. The real runner performs this check before `afterEach`; clearing
* the expectation also prevents its outer generated test from checking again.
*/
export function assertAndClearSharedExpectState(context) {
const taskExpect = getTaskExpect(context);
try {
assertSharedExpectState(context);
}
finally {
taskExpect?.setState?.({
isExpectingAssertions: false,
isExpectingAssertionsError: null,
expectedAssertionsNumber: null,
expectedAssertionsNumberErrorGen: null,
});
}
}