From 9c9fb26fec59f5c40a9e7e84edec596cb49577a9 Mon Sep 17 00:00:00 2001 From: Valentin Palkovic Date: Tue, 17 Sep 2024 13:57:07 +0200 Subject: [PATCH] Merge pull request #29133 from storybookjs/valentin/fix-skip-install-installation CLI: Fix skip-install for stable latest releases (cherry picked from commit 57cdf154a30d35d56629b04ea4640655dd96af3b) --- .circleci/config.yml | 2 + .../JsPackageManager.test.ts | 54 +++++++++++++++++++ .../js-package-manager/JsPackageManager.ts | 12 ++--- 3 files changed, 62 insertions(+), 6 deletions(-) create mode 100644 code/core/src/common/js-package-manager/JsPackageManager.test.ts diff --git a/.circleci/config.yml b/.circleci/config.yml index 3e5c0b499cc6..72f3bf9dc225 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -628,6 +628,8 @@ jobs: mkdir empty-<< parameters.template >>-no-install cd empty-<< parameters.template >>-no-install npx storybook init --yes --skip-install + npm install + npm run build-storybook environment: IN_STORYBOOK_SANDBOX: true STORYBOOK_INIT_EMPTY_TYPE: << parameters.template >> diff --git a/code/core/src/common/js-package-manager/JsPackageManager.test.ts b/code/core/src/common/js-package-manager/JsPackageManager.test.ts new file mode 100644 index 000000000000..153edd0be596 --- /dev/null +++ b/code/core/src/common/js-package-manager/JsPackageManager.test.ts @@ -0,0 +1,54 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest'; + +import { JsPackageManager } from './JsPackageManager'; + +vi.mock('../versions', () => ({ + default: { + '@storybook/react': '8.3.0', + }, +})); + +describe('JsPackageManager', () => { + let jsPackageManager: JsPackageManager; + let mockLatestVersion: ReturnType; + let mockStorybookPackagesVersions: Record; + + beforeEach(() => { + mockLatestVersion = vi.fn(); + mockStorybookPackagesVersions = { + '@storybook/react': '8.3.0', + }; + + // @ts-expect-error Ignore abstract class error + jsPackageManager = new JsPackageManager(); + jsPackageManager.latestVersion = mockLatestVersion; + + vi.clearAllMocks(); + }); + + describe('getVersionedPackages method', () => { + it('should return the latest stable release version when current version is the latest stable release', async () => { + mockLatestVersion.mockResolvedValue('8.3.0'); + + const result = await jsPackageManager.getVersionedPackages(['@storybook/react']); + + expect(result).toEqual(['@storybook/react@^8.3.0']); + }); + + it('should return the current version when it is not the latest stable release', async () => { + mockLatestVersion.mockResolvedValue('8.3.1'); + + const result = await jsPackageManager.getVersionedPackages(['@storybook/react']); + + expect(result).toEqual(['@storybook/react@8.3.0']); + }); + + it('should return the latest stable release version when there is no current version', async () => { + mockLatestVersion.mockResolvedValue('2.0.0'); + + const result = await jsPackageManager.getVersionedPackages(['@storybook/new-addon@^8.3.0']); + + expect(result).toEqual(['@storybook/new-addon@^2.0.0']); + }); + }); +}); diff --git a/code/core/src/common/js-package-manager/JsPackageManager.ts b/code/core/src/common/js-package-manager/JsPackageManager.ts index 4b3e00c2f5e7..305ba8d99ed2 100644 --- a/code/core/src/common/js-package-manager/JsPackageManager.ts +++ b/code/core/src/common/js-package-manager/JsPackageManager.ts @@ -337,13 +337,13 @@ export abstract class JsPackageManager { const k = packageName as keyof typeof storybookPackagesVersions; const currentVersion = storybookPackagesVersions[k]; - if (currentVersion === latestInRange) { - return `${packageName}`; - } - if (currentVersion) { - return `${packageName}@${currentVersion}`; + const isLatestStableRelease = currentVersion === latestInRange; + + if (isLatestStableRelease || !currentVersion) { + return `${packageName}@^${latestInRange}`; } - return `${packageName}@^${latestInRange}`; + + return `${packageName}@${currentVersion}`; }) ); }