Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ describe('addonA11yAddonTest', () => {
});
});

it.skip('should return previewFile and transformedPreviewCode if preview file exists', async () => {
it('should return previewFile and transformedPreviewCode if preview file exists', async () => {
vi.mocked(getAddonNames).mockReturnValue([
'@storybook/addon-a11y',
'@storybook/addon-vitest',
Expand Down Expand Up @@ -191,8 +191,45 @@ describe('addonA11yAddonTest', () => {
transformedPreviewCode: expect.any(String),
transformedSetupCode: null,
skipPreviewTransformation: false,
skipVitestSetupTransformation: false,
skipVitestSetupTransformation: true,
});
});

it('should return null if vitest.setup file does not exist and preview file has the necessary transformations', async () => {
vi.mocked(getAddonNames).mockReturnValue([
'@storybook/addon-a11y',
'@storybook/addon-vitest',
]);
vi.mocked(existsSync).mockImplementation((p) => {
if (p.toString().includes('preview')) {
return true;
} else {
return false;
}
});
vi.mocked(readFileSync).mockImplementation((p) => {
if (p.toString().includes('preview')) {
return `
export default {
parameters: {
a11y: {
test: 'todo'
}
}
}
`;
} else {
return '';
}
});

const result = await addonA11yAddonTest.check({
mainConfig: {
framework: '@storybook/react-vite',
},
configDir,
} as any);
expect(result).toBeNull();
Comment thread
Sidnioulz marked this conversation as resolved.
});

it('should return setupFile and null transformedSetupCode if transformation fails', async () => {
Expand Down Expand Up @@ -265,7 +302,7 @@ describe('addonA11yAddonTest', () => {
transformedPreviewCode: null,
transformedSetupCode: null,
skipPreviewTransformation: false,
skipVitestSetupTransformation: false,
skipVitestSetupTransformation: true,
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,33 @@ export const addonA11yAddonTest: Fix<AddonA11yAddonTestOptions> = {
.map((ext) => path.join(configDir, `preview${ext}`))
.find((filePath) => existsSync(filePath)) ?? null;

let skipVitestSetupTransformation = hasCsfFactoryPreview;
// Without a setup file there is nothing to transform: since Storybook 10.3
// the vitest plugin auto-provisions preview annotations (including addon-a11y's).
let skipVitestSetupTransformation = hasCsfFactoryPreview || !vitestSetupFile;
let skipPreviewTransformation = false;

if (vitestSetupFile && previewFile) {
const vitestSetupSource = readFileSync(vitestSetupFile, 'utf8');
const previewSetupSource = readFileSync(previewFile, 'utf8');

skipVitestSetupTransformation ||= vitestSetupSource.includes('@storybook/addon-a11y');
skipPreviewTransformation ||= !shouldPreviewFileBeTransformed(previewSetupSource);
if (vitestSetupFile && !skipVitestSetupTransformation) {
try {
const vitestSetupSource = readFileSync(vitestSetupFile, 'utf8');
skipVitestSetupTransformation = vitestSetupSource.includes('@storybook/addon-a11y');
} catch {
// leave the flag as-is; getTransformedSetupCode handles unreadable files
}
}

if (skipVitestSetupTransformation && skipPreviewTransformation) {
return null;
if (previewFile) {
try {
const previewSetupSource = readFileSync(previewFile, 'utf8');
skipPreviewTransformation = !shouldPreviewFileBeTransformed(previewSetupSource);
} catch {
// leave the flag as-is; getTransformedPreviewCode handles unreadable files
}
}

if (skipVitestSetupTransformation && skipPreviewTransformation) {
return null;
}
Comment thread
Sidnioulz marked this conversation as resolved.

const getTransformedSetupCode = () => {
if (!vitestSetupFile || skipVitestSetupTransformation) {
return null;
Expand All @@ -95,7 +107,7 @@ export const addonA11yAddonTest: Fix<AddonA11yAddonTestOptions> = {
try {
const vitestSetupSource = readFileSync(vitestSetupFile, 'utf8');
return transformSetupFile(vitestSetupSource);
} catch (e) {
} catch {
return null;
}
};
Expand All @@ -108,7 +120,7 @@ export const addonA11yAddonTest: Fix<AddonA11yAddonTestOptions> = {
try {
const previewSetupSource = readFileSync(previewFile, 'utf8');
return transformPreviewFile(previewSetupSource, previewFile);
} catch (e) {
} catch {
return null;
}
};
Expand Down
Loading