Skip to content

Commit

Permalink
rework the logic based on reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
yannbf committed Feb 28, 2024
1 parent b6bc2fa commit 308bee0
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 181 deletions.
140 changes: 88 additions & 52 deletions code/lib/cli/src/doctor/getIncompatibleStorybookPackages.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { describe, it, expect, vi } from 'vitest';
import type { AnalysedPackage } from './getIncompatibleStorybookPackages';
import {
getIncompatibleStorybookPackages,
getIncompatiblePackagesSummary,
checkPackageCompatibility,
} from './getIncompatibleStorybookPackages';
import pkgUp from 'read-pkg-up';
import type { JsPackageManager } from '@storybook/core-common';

import * as doctorUtils from './utils';

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

vi.mock('read-pkg-up', () => ({
default: vi.fn(),
vi.mock('./utils', () => ({
getPackageJsonPath: vi.fn(() => Promise.resolve('package.json')),
getPackageJsonOfDependency: vi.fn(() => Promise.resolve({})),
PackageJsonNotFoundError: Error,
}));

const packageManagerMock = {
Expand All @@ -27,91 +33,121 @@ const packageManagerMock = {
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',
},
describe('checkPackageCompatibility', () => {
it('returns that an package is incompatible', async () => {
const packageName = 'my-storybook-package';
vi.mocked(doctorUtils.getPackageJsonOfDependency).mockResolvedValueOnce({
name: packageName,
version: '1.0.0',
dependencies: {
'@storybook/core-common': '7.0.0',
},
path: '',
} as any);
const result = await checkPackageCompatibility(packageName, {
currentStorybookVersion: '8.0.0',
packageManager: packageManagerMock as JsPackageManager,
});
expect(result).toEqual({
packageName: 'my-storybook-package',
packageVersion: '1.0.0',
hasIncompatibleDependencies: true,
homepage: undefined,
availableUpdate: undefined,
});
});

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

const result = await getIncompatibleStorybookPackages({
it('returns that an package is compatible', async () => {
const packageName = 'my-storybook-package';
vi.mocked(doctorUtils.getPackageJsonOfDependency).mockResolvedValueOnce({
name: packageName,
version: '1.0.0',
dependencies: {
'@storybook/core-common': '8.0.0',
},
} as any);
const result = await checkPackageCompatibility(packageName, {
currentStorybookVersion: '8.0.0',
packageManager: packageManagerMock as JsPackageManager,
});
expect(result).toEqual({
packageName: 'my-storybook-package',
packageVersion: '1.0.0',
hasIncompatibleDependencies: false,
homepage: undefined,
availableUpdate: undefined,
});
});

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 that an package is incompatible and because it is core, can be upgraded', async () => {
const packageName = '@storybook/addon-essentials';
vi.mocked(doctorUtils.getPackageJsonOfDependency).mockResolvedValueOnce({
name: packageName,
version: '7.0.0',
dependencies: {
'@storybook/core-common': '7.0.0',
},
]);
} as any);
const result = await checkPackageCompatibility(packageName, {
currentStorybookVersion: '8.0.0',
packageManager: packageManagerMock as JsPackageManager,
});
expect(result).toEqual({
packageName: '@storybook/addon-essentials',
packageVersion: '7.0.0',
hasIncompatibleDependencies: true,
homepage: undefined,
availableUpdate: '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',
},
describe('getIncompatibleStorybookPackages', () => {
it('returns an array of incompatible packages', async () => {
vi.mocked(doctorUtils.getPackageJsonOfDependency).mockResolvedValueOnce({
name: '@storybook/addon-essentials',
version: '7.0.0',
dependencies: {
'@storybook/core-common': '7.0.0',
},
path: '',
});
} as any);

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,
availableUpdate: '8.0.0',
},
]);
});
});

describe('getIncompatiblePackagesSummary', () => {
it('generates a summary message for incompatible packages', () => {
const analysedPackages = [
const analysedPackages: AnalysedPackage[] = [
{
packageName: 'storybook-react',
packageVersion: '1.0.0',
hasIncompatibleDependencies: true,
latestVersionOfPackage: '2.0.0',
availableUpdate: true,
},
{
packageName: '@storybook/addon-essentials',
packageVersion: '7.0.0',
hasIncompatibleDependencies: true,
availableUpdate: '8.0.0',
},
];
const summary = getIncompatiblePackagesSummary(analysedPackages, '7.0.0');
const summary = getIncompatiblePackagesSummary(analysedPackages, '8.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!)
"The following packages are incompatible with Storybook 8.0.0 as they depend on different major versions of Storybook packages:
- storybook-react@1.0.0
- @storybook/addon-essentials@7.0.0 (8.0.0 available!)
Please consider updating your packages or contacting the maintainers for compatibility details.
Expand Down
Loading

0 comments on commit 308bee0

Please sign in to comment.