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
60 changes: 47 additions & 13 deletions code/lib/cli-storybook/src/automigrate/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import type { JsPackageManager, PackageJson } from 'storybook/internal/common';

import { runFixes } from './index';
import * as mainConfigFile from './helpers/mainConfigFile';
import { doAutomigrate, runFixes } from './index';
import type { Fix } from './types';

const check1 = vi.fn();
const run1 = vi.fn();
const getModulePackageJSON = vi.fn();
const getStorybookData = vi.fn();
const prompt1Message = 'prompt1Message';

vi.spyOn(console, 'error').mockImplementation(console.log);
vi.spyOn(mainConfigFile, 'getStorybookData').mockImplementation(getStorybookData);

const fixes: Fix<any>[] = [
{
Expand Down Expand Up @@ -70,6 +73,20 @@ const mainConfigPath = '/path/to/mainConfig';
const beforeVersion = '6.5.15';
const isUpgrade = true;

const common = {
fixes,
dryRun,
yes,
mainConfig: { stories: [] },
rendererPackage,
skipInstall,
configDir,
packageManager: packageManager,
mainConfigPath,
isUpgrade,
storiesPaths: [],
};

const runFixWrapper = async ({
beforeVersion,
storybookVersion,
Expand All @@ -78,22 +95,30 @@ const runFixWrapper = async ({
storybookVersion: string;
}) => {
return runFixes({
fixes,
dryRun,
yes,
mainConfig: { stories: [] },
rendererPackage,
skipInstall,
configDir,
packageManager: packageManager,
mainConfigPath,
...common,
storybookVersion,
beforeVersion,
isUpgrade,
storiesPaths: [],
});
};

const runAutomigrateWrapper = async ({
beforeVersion,
storybookVersion,
}: {
beforeVersion: string;
storybookVersion: string;
}) => {
getStorybookData.mockImplementation(() => {
return {
...common,
beforeVersion,
storybookVersion,
isLatest: true,
};
});
return doAutomigrate({ configDir });
};

describe('runFixes', () => {
beforeEach(() => {
getModulePackageJSON.mockImplementation(() => {
Expand Down Expand Up @@ -129,7 +154,7 @@ describe('runFixes', () => {
);
});

it('should fail if an error is thrown', async () => {
it('should fail if an error is thrown by migration', async () => {
check1.mockRejectedValue(new Error('check1 error'));

const { fixResults } = await runFixWrapper({ beforeVersion, storybookVersion: '7.0.0' });
Expand All @@ -139,4 +164,13 @@ describe('runFixes', () => {
});
expect(run1).not.toHaveBeenCalled();
});

it('should throw error if an error is thrown my migration', async () => {
check1.mockRejectedValue(new Error('check1 error'));

const result = runAutomigrateWrapper({ beforeVersion, storybookVersion: '7.0.0' });

await expect(result).rejects.toThrow('Some migrations failed');
expect(run1).not.toHaveBeenCalled();
});
});
16 changes: 11 additions & 5 deletions code/lib/cli-storybook/src/automigrate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ const logAvailableMigrations = () => {
`);
};

const hasFailures = (fixResults: Record<string, FixStatus> | undefined): boolean => {
return Object.values(fixResults || {}).some(
(r) => r === FixStatus.FAILED || r === FixStatus.CHECK_FAILED
);
};

export const doAutomigrate = async (options: AutofixOptionsFromCLI) => {
logger.debug('Extracting storybook data...');
const {
Expand Down Expand Up @@ -82,6 +88,10 @@ export const doAutomigrate = async (options: AutofixOptionsFromCLI) => {
if (outcome && !options.skipDoctor) {
await doctor({ configDir, packageManager: options.packageManager });
}

if (hasFailures(outcome?.fixResults)) {
throw new Error('Some migrations failed');
}
};

export const automigrate = async ({
Expand Down Expand Up @@ -173,12 +183,8 @@ export const automigrate = async ({
storiesPaths,
});

const hasFailures = Object.values(fixResults).some(
(r) => r === FixStatus.FAILED || r === FixStatus.CHECK_FAILED
);

// if migration failed, display a log file in the users cwd
if (hasFailures) {
if (hasFailures(fixResults)) {
logTracker.enableLogWriting();
}

Expand Down