Skip to content

Commit

Permalink
Merge pull request #28993 from storybookjs/yann/fix-isolate-false-issue
Browse files Browse the repository at this point in the history
Addon Vitest: Fix tests potentially not existing in non-isolate mode
  • Loading branch information
yannbf committed Aug 28, 2024
2 parents e870b9d + 7d7acc4 commit c0f25c1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
24 changes: 12 additions & 12 deletions code/core/src/csf-tools/vitest-plugin/transformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, []));
}
Expand Down Expand Up @@ -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, []));
}
Expand Down Expand Up @@ -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, []));
}
Expand Down Expand Up @@ -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, []));
}
Expand Down Expand Up @@ -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, []));
}
Expand Down Expand Up @@ -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, []));
}
Expand Down Expand Up @@ -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, []));
Expand Down Expand Up @@ -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, []));
}
Expand Down Expand Up @@ -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, []));
}
Expand Down Expand Up @@ -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, []));
}
Expand Down Expand Up @@ -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"]));
}
Expand Down Expand Up @@ -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, []));
}
Expand Down
6 changes: 4 additions & 2 deletions code/core/src/csf-tools/vitest-plugin/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,10 @@ export async function vitestTransform({
// Combine testPath and filepath using the ?? operator
const nullishCoalescingExpression = t.logicalExpression(
'??',
testPathProperty,
filePathProperty
// 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
);

// Create the final expression: import.meta.url.includes(...)
Expand Down

0 comments on commit c0f25c1

Please sign in to comment.