Skip to content

Commit

Permalink
Doctor: Add dynamic check for incompatible storybook packages
Browse files Browse the repository at this point in the history
  • Loading branch information
yannbf committed Feb 28, 2024
1 parent 9ba16bf commit 4865e2f
Show file tree
Hide file tree
Showing 9 changed files with 338 additions and 199 deletions.
106 changes: 0 additions & 106 deletions code/lib/cli/src/automigrate/fixes/incompatible-addons.test.ts

This file was deleted.

34 changes: 0 additions & 34 deletions code/lib/cli/src/automigrate/fixes/incompatible-addons.ts

This file was deleted.

10 changes: 0 additions & 10 deletions code/lib/cli/src/automigrate/helpers/getMigrationSummary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import dedent from 'ts-dedent';
import type { InstallationMetadata } from '@storybook/core-common';
import type { FixSummary } from '../types';
import { FixStatus } from '../types';
import { getDuplicatedDepsWarnings } from '../../doctor/getDuplicatedDepsWarnings';

export const messageDivider = '\n\n';
const segmentDivider = '\n\n─────────────────────────────────────────────────\n\n';
Expand Down Expand Up @@ -53,7 +52,6 @@ export function getMigrationSummary({
fixResults,
fixSummary,
logFile,
installationMetadata,
}: {
fixResults: Record<string, FixStatus>;
fixSummary: FixSummary;
Expand All @@ -75,14 +73,6 @@ export function getMigrationSummary({
And reach out on Discord if you need help: ${chalk.yellow('https://discord.gg/storybook')}
`);

const duplicatedDepsMessage = installationMetadata
? getDuplicatedDepsWarnings(installationMetadata)
: getDuplicatedDepsWarnings();

if (duplicatedDepsMessage) {
messages.push(duplicatedDepsMessage.join(messageDivider));
}

const hasNoFixes = Object.values(fixResults).every((r) => r === FixStatus.UNNECESSARY);
const hasFailures = Object.values(fixResults).some(
(r) => r === FixStatus.FAILED || r === FixStatus.CHECK_FAILED
Expand Down
5 changes: 4 additions & 1 deletion code/lib/cli/src/automigrate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { FixStatus, allFixes } from './fixes';
import { cleanLog } from './helpers/cleanLog';
import { getMigrationSummary } from './helpers/getMigrationSummary';
import { getStorybookData } from './helpers/mainConfigFile';
import { doctor } from '../doctor';

const logger = console;
const LOG_FILE_NAME = 'migration-storybook.log';
Expand Down Expand Up @@ -83,7 +84,7 @@ export const doAutomigrate = async (options: AutofixOptionsFromCLI) => {
throw new Error('Could not determine main config path');
}

return automigrate({
await automigrate({
...options,
packageManager,
storybookVersion,
Expand All @@ -92,6 +93,8 @@ export const doAutomigrate = async (options: AutofixOptionsFromCLI) => {
configDir,
isUpgrade: false,
});

await doctor({ configDir, packageManager: options.packageManager });
};

export const automigrate = async ({
Expand Down
122 changes: 122 additions & 0 deletions code/lib/cli/src/doctor/getIncompatibleStorybookPackages.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import {
getIncompatibleStorybookPackages,
getIncompatiblePackagesSummary,
} from './getIncompatibleStorybookPackages';
import pkgUp from 'read-pkg-up';
import type { JsPackageManager } from '@storybook/core-common';

vi.mock('chalk', () => {
return {
default: {
yellow: (str: string) => str,
cyan: (str: string) => str,
},
};
});

vi.mock('read-pkg-up', () => ({
default: vi.fn(),
}));

const packageManagerMock = {
getAllDependencies: () =>
Promise.resolve({
'@storybook/addon-essentials': '7.0.0',
}),
latestVersion: vi.fn(() => Promise.resolve('8.0.0')),
} as Partial<JsPackageManager>;

describe('getIncompatibleStorybookPackages', () => {
beforeEach(() => {
vi.resetAllMocks();
});

it('returns an array of incompatible packages', async () => {
vi.mocked(pkgUp).mockResolvedValueOnce({
packageJson: {
name: '@storybook/addon-essentials',
version: '7.0.0',
dependencies: {
'@storybook/core-common': '7.0.0',
},
},
path: '',
});

vi.mocked(packageManagerMock.latestVersion)?.mockResolvedValueOnce('8.0.0');

const result = await getIncompatibleStorybookPackages({
currentStorybookVersion: '8.0.0',
packageManager: packageManagerMock as JsPackageManager,
});

expect(packageManagerMock.latestVersion).toHaveBeenCalled();
expect(result).toEqual([
{
packageName: '@storybook/addon-essentials',
packageVersion: '7.0.0',
hasIncompatibleDependencies: true,
homepage: undefined,
availableUpdate: true,
latestVersionOfPackage: '8.0.0',
},
]);
});

it('returns an array of incompatible packages without upgrade check', async () => {
vi.mocked(pkgUp).mockResolvedValueOnce({
packageJson: {
name: '@storybook/addon-essentials',
version: '7.0.0',
dependencies: {
'@storybook/core-common': '7.0.0',
},
},
path: '',
});

const result = await getIncompatibleStorybookPackages({
currentStorybookVersion: '8.0.0',
packageManager: packageManagerMock as JsPackageManager,
skipUpgradeCheck: true,
});

expect(packageManagerMock.latestVersion).not.toHaveBeenCalled();

expect(result).toEqual([
{
packageName: '@storybook/addon-essentials',
packageVersion: '7.0.0',
hasIncompatibleDependencies: true,
homepage: undefined,
availableUpdate: false,
latestVersionOfPackage: undefined,
},
]);
});
});

describe('getIncompatiblePackagesSummary', () => {
it('generates a summary message for incompatible packages', () => {
const analysedPackages = [
{
packageName: 'storybook-react',
packageVersion: '1.0.0',
hasIncompatibleDependencies: true,
latestVersionOfPackage: '2.0.0',
availableUpdate: true,
},
];
const summary = getIncompatiblePackagesSummary(analysedPackages, '7.0.0');
expect(summary).toMatchInlineSnapshot(`
"The following addons are likely incompatible with Storybook 7.0.0:
- [email protected] (2.0.0 available!)
Please consider updating your packages or contacting the maintainers for compatibility details.
For more on Storybook 8 compatibility, see the linked Github issue:
https://github.com/storybookjs/storybook/issues/26031"
`);
});
});
Loading

0 comments on commit 4865e2f

Please sign in to comment.