Skip to content

Commit

Permalink
Fix for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinpalkovic committed Jan 9, 2025
1 parent 03093e1 commit 090276b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { formatFileContent } from '@storybook/core/common';

import { formatConfig, loadConfig } from '@storybook/core/csf-tools';

import { existsSync, readFileSync, writeFileSync } from 'fs';
Expand Down Expand Up @@ -558,7 +560,7 @@ describe('addonA11yAddonTest', () => {
});

describe('transformPreviewFile', () => {
it('should add a new tags property if it does not exist', () => {
it('should add a new tags property if it does not exist', async () => {
const source = dedent`
import type { Preview } from '@storybook/react';
Expand All @@ -576,7 +578,7 @@ describe('addonA11yAddonTest', () => {
export default preview;
`;

const transformed = transformPreviewFile(source);
const transformed = await transformPreviewFile(source, process.cwd());
const expected = dedent`
import type { Preview } from '@storybook/react';
Expand All @@ -598,10 +600,10 @@ describe('addonA11yAddonTest', () => {
export default preview;
`;

expect(transformed).toBe(formatConfig(loadConfig(expected).parse()));
expect(transformed).toBe(await formatFileContent(process.cwd(), expected));

Check failure on line 603 in code/lib/cli-storybook/src/automigrate/fixes/addon-a11y-addon-test.test.ts

View workflow job for this annotation

GitHub Actions / Core Unit Tests, windows-latest

src/automigrate/fixes/addon-a11y-addon-test.test.ts > addonA11yAddonTest > transformPreviewFile > should add a new tags property if it does not exist

AssertionError: expected ' import type { Preview } from \'…' to be ' import type { Preview } from \'…' // Object.is equality - Expected + Received - import type { Preview } from '@storybook/react'; - + import type { Preview } from '@storybook/react'; + - const preview: Preview = { + const preview: Preview = { - parameters: { + parameters: { - controls: { + controls: { - matchers: { + matchers: { - color: /(background|color)$/i, + color: /(background|color)$/i, - date: /Date$/i, + date: /Date$/i, - }, + }, - }, + }, - }, - + }, + // a11y-test tag controls whether accessibility tests are run as part of a standalone Vitest test run // For more information please visit: https://storybook.js.org/docs/writing-tests/accessibility-testing - tags: [/*'a11y-test'*/] + tags: [/*'a11y-test'*/] - }; - + }; + export default preview; ❯ src/automigrate/fixes/addon-a11y-addon-test.test.ts:603:27
});

it('should add a new tags property if it does not exist and a default export does not exist', () => {
it('should add a new tags property if it does not exist and a default export does not exist', async () => {
const source = dedent`
export const parameters = {
controls: {
Expand All @@ -613,7 +615,7 @@ describe('addonA11yAddonTest', () => {
}
`;

const transformed = transformPreviewFile(source);
const transformed = await transformPreviewFile(source, process.cwd());
const expected = dedent`
export const parameters = {
controls: {
Expand All @@ -626,10 +628,10 @@ describe('addonA11yAddonTest', () => {
export const tags = ["a11y-test"];
`;

expect(transformed).toBe(formatConfig(loadConfig(expected).parse()));
expect(transformed).toBe(await formatFileContent(process.cwd(), expected));

Check failure on line 631 in code/lib/cli-storybook/src/automigrate/fixes/addon-a11y-addon-test.test.ts

View workflow job for this annotation

GitHub Actions / Core Unit Tests, windows-latest

src/automigrate/fixes/addon-a11y-addon-test.test.ts > addonA11yAddonTest > transformPreviewFile > should add a new tags property if it does not exist and a default export does not exist

AssertionError: expected 'export const parameters = {\r\n cont…' to be 'export const parameters = {\n contro…' // Object.is equality - Expected + Received - export const parameters = { + export const parameters = { - controls: { + controls: { - matchers: { + matchers: { - color: /(background|color)$/i, + color: /(background|color)$/i, - date: /Date$/i, + date: /Date$/i, - }, + }, - }, + }, - } + } export const tags = ["a11y-test"]; ❯ src/automigrate/fixes/addon-a11y-addon-test.test.ts:631:27
});

it('should extend the existing tags property', () => {
it('should extend the existing tags property', async () => {
const source = dedent`
import type { Preview } from "@storybook/react";
Expand All @@ -648,7 +650,7 @@ describe('addonA11yAddonTest', () => {
export default preview;
`;

const transformed = transformPreviewFile(source);
const transformed = await transformPreviewFile(source, process.cwd());
const expected = dedent`
import type { Preview } from "@storybook/react";
Expand All @@ -669,10 +671,10 @@ describe('addonA11yAddonTest', () => {
export default preview;
`;

expect(transformed).toBe(formatConfig(loadConfig(expected).parse()));
expect(transformed).toBe(await formatFileContent(process.cwd(), expected));

Check failure on line 674 in code/lib/cli-storybook/src/automigrate/fixes/addon-a11y-addon-test.test.ts

View workflow job for this annotation

GitHub Actions / Core Unit Tests, windows-latest

src/automigrate/fixes/addon-a11y-addon-test.test.ts > addonA11yAddonTest > transformPreviewFile > should extend the existing tags property

AssertionError: expected ' import type { Preview } from "@…' to be ' import type { Preview } from "@…' // Object.is equality - Expected + Received - import type { Preview } from "@storybook/react"; - + import type { Preview } from "@storybook/react"; + - const preview: Preview = { + const preview: Preview = { // a11y-test tag controls whether accessibility tests are run as part of a standalone Vitest test run // For more information please visit: https://storybook.js.org/docs/writing-tests/accessibility-testing - tags: ["existingTag"/*, "a11y-test"*/], + tags: ["existingTag"/*, "a11y-test"*/], - parameters: { + parameters: { - controls: { + controls: { - matchers: { + matchers: { - color: /(background|color)$/i, + color: /(background|color)$/i, - date: /Date$/i, + date: /Date$/i, - }, + }, - }, + }, - }, + }, - }; - + }; + export default preview; ❯ src/automigrate/fixes/addon-a11y-addon-test.test.ts:674:27
});

it('should not add a11y-test if it already exists in the tags property', () => {
it('should not add a11y-test if it already exists in the tags property', async () => {
const expected = dedent`
import type { Preview } from "@storybook/react";
Expand All @@ -691,12 +693,12 @@ describe('addonA11yAddonTest', () => {
export default preview;
`;

const transformed = transformPreviewFile(expected);
const transformed = await transformPreviewFile(expected, process.cwd());

expect(transformed).toBe(expected);
expect(transformed).toBe(await formatFileContent(process.cwd(), expected));
});

it('should handle the default export without type annotations', () => {
it('should handle the default export without type annotations', async () => {
const source = dedent`
export default {
parameters: {
Expand All @@ -710,7 +712,7 @@ describe('addonA11yAddonTest', () => {
};
`;

const transformed = transformPreviewFile(source);
const transformed = await transformPreviewFile(source, process.cwd());
const expected = dedent`
export default {
parameters: {
Expand All @@ -728,10 +730,10 @@ describe('addonA11yAddonTest', () => {
};
`;

expect(transformed).toBe(formatConfig(loadConfig(expected).parse()));
expect(transformed).toBe(await formatFileContent(process.cwd(), expected));

Check failure on line 733 in code/lib/cli-storybook/src/automigrate/fixes/addon-a11y-addon-test.test.ts

View workflow job for this annotation

GitHub Actions / Core Unit Tests, windows-latest

src/automigrate/fixes/addon-a11y-addon-test.test.ts > addonA11yAddonTest > transformPreviewFile > should handle the default export without type annotations

AssertionError: expected 'export default {\r\n parameters: {\r…' to be 'export default {\n parameters: {\n …' // Object.is equality - Expected + Received - export default { + export default { - parameters: { + parameters: { - controls: { + controls: { - matchers: { + matchers: { - color: /(background|color)$/i, + color: /(background|color)$/i, - date: /Date$/i, + date: /Date$/i, - }, + }, - }, + }, - }, - + }, + // a11y-test tag controls whether accessibility tests are run as part of a standalone Vitest test run // For more information please visit: https://storybook.js.org/docs/writing-tests/accessibility-testing - tags: [/*"a11y-test"*/] + tags: [/*"a11y-test"*/] }; ❯ src/automigrate/fixes/addon-a11y-addon-test.test.ts:733:27
});

it('should extend the existing tags property without type annotations', () => {
it('should extend the existing tags property without type annotations', async () => {
const source = dedent`
export default {
tags: ["existingTag"],
Expand All @@ -746,7 +748,7 @@ describe('addonA11yAddonTest', () => {
};
`;

const transformed = transformPreviewFile(source);
const transformed = await transformPreviewFile(source, process.cwd());
const expected = dedent`
export default {
// a11y-test tag controls whether accessibility tests are run as part of a standalone Vitest test run
Expand All @@ -763,7 +765,7 @@ describe('addonA11yAddonTest', () => {
};
`;

expect(transformed).toBe(formatConfig(loadConfig(expected).parse()));
expect(transformed).toBe(await formatFileContent(process.cwd(), expected));

Check failure on line 768 in code/lib/cli-storybook/src/automigrate/fixes/addon-a11y-addon-test.test.ts

View workflow job for this annotation

GitHub Actions / Core Unit Tests, windows-latest

src/automigrate/fixes/addon-a11y-addon-test.test.ts > addonA11yAddonTest > transformPreviewFile > should extend the existing tags property without type annotations

AssertionError: expected 'export default {\r\n // a11y-test ta…' to be 'export default {\n // a11y-test tag …' // Object.is equality - Expected + Received - export default { + export default { // a11y-test tag controls whether accessibility tests are run as part of a standalone Vitest test run // For more information please visit: https://storybook.js.org/docs/writing-tests/accessibility-testing - tags: ["existingTag"/*, "a11y-test"*/], + tags: ["existingTag"/*, "a11y-test"*/], - parameters: { + parameters: { - controls: { + controls: { - matchers: { + matchers: { - color: /(background|color)$/i, + color: /(background|color)$/i, - date: /Date$/i, + date: /Date$/i, - }, + }, - }, + }, - }, + }, }; ❯ src/automigrate/fixes/addon-a11y-addon-test.test.ts:768:27
});
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { EOL } from 'node:os';

import { rendererPackages } from 'storybook/internal/common';
import { formatFileContent, rendererPackages } from 'storybook/internal/common';
import { formatConfig, loadConfig } from 'storybook/internal/csf-tools';

import { type ArrayExpression } from '@babel/types';
Expand Down Expand Up @@ -123,14 +121,14 @@ export const addonA11yAddonTest: Fix<AddonA11yAddonTestOptions> = {
}
};

const getTransformedPreviewCode = () => {
const getTransformedPreviewCode = async () => {
if (!previewFile || skipPreviewTransformation) {
return null;
}

try {
const previewSetupSource = readFileSync(previewFile, 'utf8');
return transformPreviewFile(previewSetupSource);
return await transformPreviewFile(previewSetupSource, previewFile);
} catch (e) {
return null;
}
Expand All @@ -140,7 +138,7 @@ export const addonA11yAddonTest: Fix<AddonA11yAddonTestOptions> = {
setupFile: vitestSetupFile,
previewFile: previewFile,
transformedSetupCode: getTransformedSetupCode(),
transformedPreviewCode: getTransformedPreviewCode(),
transformedPreviewCode: await getTransformedPreviewCode(),
skipVitestSetupTransformation,
skipPreviewTransformation,
};
Expand Down Expand Up @@ -273,7 +271,7 @@ export function transformSetupFile(source: string) {
return root.toSource();
}

export function transformPreviewFile(source: string) {
export async function transformPreviewFile(source: string, filePath: string) {
const previewConfig = loadConfig(source).parse();
const tags = previewConfig.getFieldNode(['tags']);
const tagsNode = previewConfig.getFieldNode(['tags']) as ArrayExpression;
Expand Down Expand Up @@ -327,5 +325,5 @@ export function transformPreviewFile(source: string) {
const comment = `${indentation}// a11y-test tag controls whether accessibility tests are run as part of a standalone Vitest test run\n${indentation}// For more information please visit: https://storybook.js.org/docs/writing-tests/accessibility-testing`;
lines.splice(tagsLineIndex, 0, comment);

return lines.join(EOL);
return formatFileContent(filePath, lines.join('\n'));
}

0 comments on commit 090276b

Please sign in to comment.