From aad93a785352e0797494719d8bf17e9366d0cd6c Mon Sep 17 00:00:00 2001 From: Vladimir Date: Tue, 10 Sep 2024 21:40:46 +0200 Subject: [PATCH 1/3] fix: `expect.getState().testPath` always returns correct path --- packages/vitest/src/integrations/chai/index.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/packages/vitest/src/integrations/chai/index.ts b/packages/vitest/src/integrations/chai/index.ts index 72ce87d34c5c..fbf932ce35a4 100644 --- a/packages/vitest/src/integrations/chai/index.ts +++ b/packages/vitest/src/integrations/chai/index.ts @@ -39,7 +39,7 @@ export function createExpect(test?: TaskPopulated) { // @ts-expect-error global is not typed const globalState = getState(globalThis[GLOBAL_EXPECT]) || {} - const testPath = getTestFile(test) + const testPath = getTestFile() setState( { // this should also add "snapshotState" that is added conditionally @@ -111,10 +111,7 @@ export function createExpect(test?: TaskPopulated) { return expect } -function getTestFile(test?: TaskPopulated) { - if (test) { - return test.file.filepath - } +function getTestFile() { const state = getWorkerState() return state.filepath } From 58d84ee282578bb61bd7c994fc3c6b7cd59405b6 Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Wed, 11 Sep 2024 20:03:32 +0200 Subject: [PATCH 2/3] test: add test for testPath --- test/core/test/basic.test.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/core/test/basic.test.ts b/test/core/test/basic.test.ts index d775423c442b..80ef6a674e20 100644 --- a/test/core/test/basic.test.ts +++ b/test/core/test/basic.test.ts @@ -63,3 +63,8 @@ test('escaping', () => { expect(['\\123']).toEqual(['\\123']) expect('\\123').toEqual('\\123') }) + +const testPath = expect.getState().testPath +if (!testPath || !testPath.includes('basic.test.ts')) { + throw new Error(`testPath not correct: ${testPath}`) +} From 8d531407b8dd2b9326f7058d2c79ef922ebaece4 Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Wed, 11 Sep 2024 20:20:25 +0200 Subject: [PATCH 3/3] chore: cleanup --- packages/expect/src/state.ts | 8 ++++++-- packages/vitest/src/integrations/chai/index.ts | 10 +++------- test/core/test/basic.test.ts | 10 +++++----- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/packages/expect/src/state.ts b/packages/expect/src/state.ts index efa26c692c07..dade3622e001 100644 --- a/packages/expect/src/state.ts +++ b/packages/expect/src/state.ts @@ -39,6 +39,10 @@ export function setState( ): 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) } diff --git a/packages/vitest/src/integrations/chai/index.ts b/packages/vitest/src/integrations/chai/index.ts index fbf932ce35a4..2b2cfcbe912e 100644 --- a/packages/vitest/src/integrations/chai/index.ts +++ b/packages/vitest/src/integrations/chai/index.ts @@ -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() setState( { // this should also add "snapshotState" that is added conditionally @@ -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, @@ -111,11 +112,6 @@ export function createExpect(test?: TaskPopulated) { return expect } -function getTestFile() { - const state = getWorkerState() - return state.filepath -} - const globalExpect = createExpect() Object.defineProperty(globalThis, GLOBAL_EXPECT, { diff --git a/test/core/test/basic.test.ts b/test/core/test/basic.test.ts index 80ef6a674e20..9c6aac68f8eb 100644 --- a/test/core/test/basic.test.ts +++ b/test/core/test/basic.test.ts @@ -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) @@ -63,8 +68,3 @@ test('escaping', () => { expect(['\\123']).toEqual(['\\123']) expect('\\123').toEqual('\\123') }) - -const testPath = expect.getState().testPath -if (!testPath || !testPath.includes('basic.test.ts')) { - throw new Error(`testPath not correct: ${testPath}`) -}