From 5e4d9ae99eed06f54014deb801ffd3d7f6510a1f Mon Sep 17 00:00:00 2001 From: Yann Braga Date: Wed, 28 Aug 2024 13:31:27 +0200 Subject: [PATCH 1/2] Addon Vitest: Fix tests potentially not existing in non-isolate mode --- .../vitest-plugin/transformer.test.ts | 24 +++++++++---------- .../csf-tools/vitest-plugin/transformer.ts | 4 +++- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/code/core/src/csf-tools/vitest-plugin/transformer.test.ts b/code/core/src/csf-tools/vitest-plugin/transformer.test.ts index a92af8c4cc2f..a96833accaf4 100644 --- a/code/core/src/csf-tools/vitest-plugin/transformer.test.ts +++ b/code/core/src/csf-tools/vitest-plugin/transformer.test.ts @@ -80,7 +80,7 @@ describe('transformer', () => { }; export default _meta; export const Story = {}; - const _isRunningFromThisFile = import.meta.url.includes(_expect.getState().testPath ?? globalThis.__vitest_worker__.filepath); + const _isRunningFromThisFile = import.meta.url.includes(globalThis.__vitest_worker__.filepath ?? _expect.getState().testPath); if (_isRunningFromThisFile) { _test("Story", _testStory("Story", Story, _meta, [])); } @@ -109,7 +109,7 @@ describe('transformer', () => { }; export default _meta; export const Story = {}; - const _isRunningFromThisFile = import.meta.url.includes(_expect.getState().testPath ?? globalThis.__vitest_worker__.filepath); + const _isRunningFromThisFile = import.meta.url.includes(globalThis.__vitest_worker__.filepath ?? _expect.getState().testPath); if (_isRunningFromThisFile) { _test("Story", _testStory("Story", Story, _meta, [])); } @@ -139,7 +139,7 @@ describe('transformer', () => { }; export default meta; export const Story = {}; - const _isRunningFromThisFile = import.meta.url.includes(_expect.getState().testPath ?? globalThis.__vitest_worker__.filepath); + const _isRunningFromThisFile = import.meta.url.includes(globalThis.__vitest_worker__.filepath ?? _expect.getState().testPath); if (_isRunningFromThisFile) { _test("Story", _testStory("Story", Story, meta, [])); } @@ -170,7 +170,7 @@ describe('transformer', () => { }; export default meta; export const Story = {}; - const _isRunningFromThisFile = import.meta.url.includes(_expect.getState().testPath ?? globalThis.__vitest_worker__.filepath); + const _isRunningFromThisFile = import.meta.url.includes(globalThis.__vitest_worker__.filepath ?? _expect.getState().testPath); if (_isRunningFromThisFile) { _test("Story", _testStory("Story", Story, meta, [])); } @@ -206,7 +206,7 @@ describe('transformer', () => { label: 'Primary Button' } }; - const _isRunningFromThisFile = import.meta.url.includes(_expect.getState().testPath ?? globalThis.__vitest_worker__.filepath); + const _isRunningFromThisFile = import.meta.url.includes(globalThis.__vitest_worker__.filepath ?? _expect.getState().testPath); if (_isRunningFromThisFile) { _test("Primary", _testStory("Primary", Primary, _meta, [])); } @@ -240,7 +240,7 @@ describe('transformer', () => { } }; export { Primary }; - const _isRunningFromThisFile = import.meta.url.includes(_expect.getState().testPath ?? globalThis.__vitest_worker__.filepath); + const _isRunningFromThisFile = import.meta.url.includes(globalThis.__vitest_worker__.filepath ?? _expect.getState().testPath); if (_isRunningFromThisFile) { _test("Primary", _testStory("Primary", Primary, _meta, [])); } @@ -276,7 +276,7 @@ describe('transformer', () => { }; export const Secondary = {}; export { Primary }; - const _isRunningFromThisFile = import.meta.url.includes(_expect.getState().testPath ?? globalThis.__vitest_worker__.filepath); + const _isRunningFromThisFile = import.meta.url.includes(globalThis.__vitest_worker__.filepath ?? _expect.getState().testPath); if (_isRunningFromThisFile) { _test("Secondary", _testStory("Secondary", Secondary, _meta, [])); _test("Primary", _testStory("Primary", Primary, _meta, [])); @@ -308,7 +308,7 @@ describe('transformer', () => { export default _meta; export const Story = {}; export const nonStory = 123; - const _isRunningFromThisFile = import.meta.url.includes(_expect.getState().testPath ?? globalThis.__vitest_worker__.filepath); + const _isRunningFromThisFile = import.meta.url.includes(globalThis.__vitest_worker__.filepath ?? _expect.getState().testPath); if (_isRunningFromThisFile) { _test("Story", _testStory("Story", Story, _meta, [])); } @@ -365,7 +365,7 @@ describe('transformer', () => { tags: ['include-me'] }; export const NotIncluded = {}; - const _isRunningFromThisFile = import.meta.url.includes(_expect.getState().testPath ?? globalThis.__vitest_worker__.filepath); + const _isRunningFromThisFile = import.meta.url.includes(globalThis.__vitest_worker__.filepath ?? _expect.getState().testPath); if (_isRunningFromThisFile) { _test("Included", _testStory("Included", Included, _meta, [])); } @@ -396,7 +396,7 @@ describe('transformer', () => { export const NotIncluded = { tags: ['exclude-me'] }; - const _isRunningFromThisFile = import.meta.url.includes(_expect.getState().testPath ?? globalThis.__vitest_worker__.filepath); + const _isRunningFromThisFile = import.meta.url.includes(globalThis.__vitest_worker__.filepath ?? _expect.getState().testPath); if (_isRunningFromThisFile) { _test("Included", _testStory("Included", Included, _meta, [])); } @@ -424,7 +424,7 @@ describe('transformer', () => { export const Skipped = { tags: ['skip-me'] }; - const _isRunningFromThisFile = import.meta.url.includes(_expect.getState().testPath ?? globalThis.__vitest_worker__.filepath); + const _isRunningFromThisFile = import.meta.url.includes(globalThis.__vitest_worker__.filepath ?? _expect.getState().testPath); if (_isRunningFromThisFile) { _test("Skipped", _testStory("Skipped", Skipped, _meta, ["skip-me"])); } @@ -456,7 +456,7 @@ describe('transformer', () => { }; export default meta; export const Primary = {}; - const _isRunningFromThisFile = import.meta.url.includes(_expect.getState().testPath ?? globalThis.__vitest_worker__.filepath); + const _isRunningFromThisFile = import.meta.url.includes(globalThis.__vitest_worker__.filepath ?? _expect.getState().testPath); if (_isRunningFromThisFile) { _test("Primary", _testStory("Primary", Primary, meta, [])); } diff --git a/code/core/src/csf-tools/vitest-plugin/transformer.ts b/code/core/src/csf-tools/vitest-plugin/transformer.ts index b4020ee4efcb..9c98d00a8bf8 100644 --- a/code/core/src/csf-tools/vitest-plugin/transformer.ts +++ b/code/core/src/csf-tools/vitest-plugin/transformer.ts @@ -172,8 +172,10 @@ export async function vitestTransform({ // Combine testPath and filepath using the ?? operator const nullishCoalescingExpression = t.logicalExpression( '??', + // TODO: switch order of testPathProperty and filePathProperty when the bug is fixed + // https://github.com/vitest-dev/vitest/issues/6367 (or probably just use testPathProperty) + filePathProperty, testPathProperty, - filePathProperty ); // Create the final expression: import.meta.url.includes(...) From 7d7acc4e4e731f662e918a2ef5f341557c5d4413 Mon Sep 17 00:00:00 2001 From: Yann Braga Date: Wed, 28 Aug 2024 22:10:54 +0200 Subject: [PATCH 2/2] fix lint --- code/core/src/csf-tools/vitest-plugin/transformer.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/core/src/csf-tools/vitest-plugin/transformer.ts b/code/core/src/csf-tools/vitest-plugin/transformer.ts index 9c98d00a8bf8..b2c66693b228 100644 --- a/code/core/src/csf-tools/vitest-plugin/transformer.ts +++ b/code/core/src/csf-tools/vitest-plugin/transformer.ts @@ -175,7 +175,7 @@ export async function vitestTransform({ // TODO: switch order of testPathProperty and filePathProperty when the bug is fixed // https://github.com/vitest-dev/vitest/issues/6367 (or probably just use testPathProperty) filePathProperty, - testPathProperty, + testPathProperty ); // Create the final expression: import.meta.url.includes(...)