Merge pull request #27217 from storybookjs/kasper/disable-upgrade-to-same-version-error

CLI: Only log the UpgradeStorybookToSameVersionError but continue the upgrade as normal
This commit is contained in:
Kasper Peulen 2024-06-05 10:34:31 +02:00 committed by GitHub
commit 68809b615f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 19 additions and 9 deletions

View File

@ -1,10 +1,8 @@
import { describe, it, expect, vi } from 'vitest'; import { describe, expect, it, vi } from 'vitest';
import * as sbcc from '@storybook/core-common'; import * as sbcc from '@storybook/core-common';
import { import { UpgradeStorybookToLowerVersionError } from '@storybook/core-events/server-errors';
UpgradeStorybookToLowerVersionError,
UpgradeStorybookToSameVersionError,
} from '@storybook/core-events/server-errors';
import { doUpgrade, getStorybookVersion } from './upgrade'; import { doUpgrade, getStorybookVersion } from './upgrade';
import { logger } from '@storybook/node-logger';
const findInstallationsMock = vi.fn<string[], Promise<sbcc.InstallationMetadata | undefined>>(); const findInstallationsMock = vi.fn<string[], Promise<sbcc.InstallationMetadata | undefined>>();
@ -16,6 +14,8 @@ vi.mock('@storybook/core-common', async (importOriginal) => {
JsPackageManagerFactory: { JsPackageManagerFactory: {
getPackageManager: () => ({ getPackageManager: () => ({
findInstallations: findInstallationsMock, findInstallations: findInstallationsMock,
latestVersion: async () => '8.0.0',
retrievePackageJson: async () => {},
getAllDependencies: async () => ({ storybook: '8.0.0' }), getAllDependencies: async () => ({ storybook: '8.0.0' }),
}), }),
}, },
@ -68,7 +68,7 @@ describe('Upgrade errors', () => {
await expect(doUpgrade({} as any)).rejects.toThrowError(UpgradeStorybookToLowerVersionError); await expect(doUpgrade({} as any)).rejects.toThrowError(UpgradeStorybookToLowerVersionError);
expect(findInstallationsMock).toHaveBeenCalledWith(Object.keys(sbcc.versions)); expect(findInstallationsMock).toHaveBeenCalledWith(Object.keys(sbcc.versions));
}); });
it('should throw an error when upgrading to the same version number', async () => { it('should show a warning when upgrading to the same version number', async () => {
findInstallationsMock.mockResolvedValue({ findInstallationsMock.mockResolvedValue({
dependencies: { dependencies: {
'@storybook/cli': [ '@storybook/cli': [
@ -82,7 +82,15 @@ describe('Upgrade errors', () => {
dedupeCommand: '', dedupeCommand: '',
}); });
await expect(doUpgrade({} as any)).rejects.toThrowError(UpgradeStorybookToSameVersionError); // Mock as a throw, so that we don't have to mock the content of the doUpgrade fn that comes after it
vi.spyOn(logger, 'warn').mockImplementation((error) => {
// eslint-disable-next-line @typescript-eslint/no-throw-literal
throw error;
});
await expect(doUpgrade({ packageManager: 'npm' } as any)).rejects.toContain(
'You are upgrading Storybook to the same version that is currently installed in the project'
);
expect(findInstallationsMock).toHaveBeenCalledWith(Object.keys(sbcc.versions)); expect(findInstallationsMock).toHaveBeenCalledWith(Object.keys(sbcc.versions));
}); });
}); });

View File

@ -142,8 +142,10 @@ export const doUpgrade = async ({
if (!isCanary && lt(currentVersion, beforeVersion)) { if (!isCanary && lt(currentVersion, beforeVersion)) {
throw new UpgradeStorybookToLowerVersionError({ beforeVersion, currentVersion }); throw new UpgradeStorybookToLowerVersionError({ beforeVersion, currentVersion });
} }
if (!isCanary && eq(currentVersion, beforeVersion)) { if (!isCanary && eq(currentVersion, beforeVersion)) {
throw new UpgradeStorybookToSameVersionError({ beforeVersion }); // Not throwing, as the beforeVersion calculation doesn't always work in monorepos.
logger.warn(new UpgradeStorybookToSameVersionError({ beforeVersion }).message);
} }
const [latestVersion, packageJson] = await Promise.all([ const [latestVersion, packageJson] = await Promise.all([

View File

@ -545,7 +545,7 @@ export class UpgradeStorybookToSameVersionError extends StorybookError {
template() { template() {
return dedent` return dedent`
You are trying to upgrade Storybook to the same version that is currently installed in the project, version ${this.data.beforeVersion}. This is not supported. You are upgrading Storybook to the same version that is currently installed in the project, version ${this.data.beforeVersion}.
This usually happens when running the upgrade command without a version specifier, e.g. "npx storybook upgrade". This usually happens when running the upgrade command without a version specifier, e.g. "npx storybook upgrade".
This will cause npm to run the globally cached storybook binary, which might be the same version that you already have. This will cause npm to run the globally cached storybook binary, which might be the same version that you already have.