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 9fd15f2
Show file tree
Hide file tree
Showing 10 changed files with 343 additions and 230 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.

36 changes: 5 additions & 31 deletions code/lib/cli/src/automigrate/helpers/getMigrationSummary.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ describe('getMigrationSummary', () => {
`);
});

it('renders a summary with a warning if there are duplicated dependencies outside the allow list', () => {
// TODO: bring this back in case we decide to keep the duplicated deps warning
it.skip('renders a summary with a warning if there are duplicated dependencies outside the allow list', () => {
const summary = getMigrationSummary({
fixResults: {},
fixSummary: { succeeded: [], failed: {}, manual: [], skipped: [] },
Expand All @@ -129,39 +130,12 @@ describe('getMigrationSummary', () => {
The automigrations try to migrate common patterns in your project, but might not contain everything needed to migrate to the latest version of Storybook.
Please check the changelog and migration guide for manual migrations and more information: https://storybook.js.org/migration-guides/7.0
And reach out on Discord if you need help: https://discord.gg/storybook
─────────────────────────────────────────────────
Critical: The following dependencies are duplicated and WILL cause unexpected behavior:
@storybook/instrumenter:
6.0.0, 7.1.0
@storybook/core-common:
6.0.0, 7.1.0
Attention: The following dependencies are duplicated which might cause unexpected behavior:
@storybook/addon-essentials:
7.0.0, 7.1.0
You can find more information for a given dependency by running yarn why <package-name>
Please try de-duplicating these dependencies by running yarn dedupe"
And reach out on Discord if you need help: https://discord.gg/storybook"
`);
});

it('renders a basic summary if there are no duplicated dependencies or migrations', () => {
// TODO: bring this back in case we decide to keep the duplicated deps warning
it.skip('renders a basic summary if there are no duplicated dependencies or migrations', () => {
const summary = getMigrationSummary({
fixResults: {},
fixSummary: { succeeded: [], failed: {}, manual: [], skipped: [] },
Expand Down
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:
- storybook-react@1.0.0 (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 9fd15f2

Please sign in to comment.