Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: expect.getState().testPath always returns correct path #6472

Merged
merged 3 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/expect/src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export function setState<State extends MatcherState = MatcherState>(
): void {
const map = (globalThis as any)[MATCHERS_OBJECT]
const current = map.get(expect) || {}
Object.assign(current, state)
map.set(expect, current)
// so it keeps getters from `testPath`
const results = Object.defineProperties(current, {
...Object.getOwnPropertyDescriptors(current),
...Object.getOwnPropertyDescriptors(state),
})
map.set(expect, results)
}
13 changes: 3 additions & 10 deletions packages/vitest/src/integrations/chai/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ export function createExpect(test?: TaskPopulated) {
// @ts-expect-error global is not typed
const globalState = getState(globalThis[GLOBAL_EXPECT]) || {}

const testPath = getTestFile(test)
setState<MatcherState>(
{
// this should also add "snapshotState" that is added conditionally
Expand All @@ -50,7 +49,9 @@ export function createExpect(test?: TaskPopulated) {
expectedAssertionsNumber: null,
expectedAssertionsNumberErrorGen: null,
environment: getCurrentEnvironment(),
testPath,
get testPath() {
return getWorkerState().filepath
},
currentTestName: test
? getTestName(test as Test)
: globalState.currentTestName,
Expand Down Expand Up @@ -111,14 +112,6 @@ export function createExpect(test?: TaskPopulated) {
return expect
}

function getTestFile(test?: TaskPopulated) {
if (test) {
return test.file.filepath
}
const state = getWorkerState()
return state.filepath
}

const globalExpect = createExpect()

Object.defineProperty(globalThis, GLOBAL_EXPECT, {
Expand Down
5 changes: 5 additions & 0 deletions test/core/test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { assert, expect, it, suite, test } from 'vitest'
import { two } from '../src/submodule'
import { timeout } from '../src/timeout'

const testPath = expect.getState().testPath
if (!testPath || !testPath.includes('basic.test.ts')) {
throw new Error(`testPath is not correct: ${testPath}`)
}

test('Math.sqrt()', async () => {
assert.equal(Math.sqrt(4), two)
assert.equal(Math.sqrt(2), Math.SQRT2)
Expand Down
Loading