diff --git a/app/composables/useInstallSizeDiff.ts b/app/composables/useInstallSizeDiff.ts index d084ba14c3..0dd85206e2 100644 --- a/app/composables/useInstallSizeDiff.ts +++ b/app/composables/useInstallSizeDiff.ts @@ -31,7 +31,9 @@ function getComparisonVersion(pkg: SlimPackument, resolvedVersion: string): stri .sort((a, b) => compare(a, b)) const currentIdx = stableVersions.indexOf(resolvedVersion) - if (currentIdx <= 0) return null + // Don't compare the second version against the first as the first + // has no baseline so a large size difference is expected + if (currentIdx <= 1) return null return stableVersions[currentIdx - 1]! } diff --git a/test/nuxt/composables/use-install-size-diff.spec.ts b/test/nuxt/composables/use-install-size-diff.spec.ts index d464e2e353..100ca89f93 100644 --- a/test/nuxt/composables/use-install-size-diff.spec.ts +++ b/test/nuxt/composables/use-install-size-diff.spec.ts @@ -61,8 +61,19 @@ describe('useInstallSizeDiff', () => { expect(comparisonVersion.value).toBeNull() }) + it('returns null for the second stable version (no baseline for first)', () => { + const pkg = createPackage('test', { + '1.0.0': '2020-01-01', + '1.1.0': '2021-01-01', + }) + + const { comparisonVersion } = useInstallSizeDiff('test', '1.1.0', pkg, null) + expect(comparisonVersion.value).toBeNull() + }) + it('skips prerelease versions when finding previous stable', () => { const pkg = createPackage('test', { + '0.9.0': '2019-01-01', '1.0.0': '2020-01-01', '2.0.0-beta.1': '2021-01-01', '2.0.0-beta.2': '2021-06-01', @@ -117,6 +128,7 @@ describe('useInstallSizeDiff', () => { it('returns null when both thresholds are not met', async () => { const pkg = createPackage('pkg-no-threshold', { + '0.9.0': '2019-01-01', '1.0.0': '2020-01-01', '1.1.0': '2021-01-01', }) @@ -140,7 +152,11 @@ describe('useInstallSizeDiff', () => { }) it('sets sizeThresholdExceeded when size increased by more than 25%', async () => { - const pkg = createPackage('pkg-size-only', { '1.0.0': '2020-01-01', '1.1.0': '2021-01-01' }) + const pkg = createPackage('pkg-size-only', { + '0.9.0': '2019-01-01', + '1.0.0': '2020-01-01', + '1.1.0': '2021-01-01', + }) const current = createInstallSize('pkg-size-only', { version: '1.1.0', totalSize: 7000, @@ -172,7 +188,11 @@ describe('useInstallSizeDiff', () => { }) it('sets depThresholdExceeded when more than 5 dependencies were added', async () => { - const pkg = createPackage('pkg-deps-only', { '1.0.0': '2020-01-01', '1.1.0': '2021-01-01' }) + const pkg = createPackage('pkg-deps-only', { + '0.9.0': '2019-01-01', + '1.0.0': '2020-01-01', + '1.1.0': '2021-01-01', + }) const current = createInstallSize('pkg-deps-only', { version: '1.1.0', totalSize: 5100, @@ -204,7 +224,11 @@ describe('useInstallSizeDiff', () => { }) it('returns correct diff values', async () => { - const pkg = createPackage('pkg-both', { '1.0.0': '2020-01-01', '1.1.0': '2021-01-01' }) + const pkg = createPackage('pkg-both', { + '0.9.0': '2019-01-01', + '1.0.0': '2020-01-01', + '1.1.0': '2021-01-01', + }) const current = createInstallSize('pkg-both', { version: '1.1.0', totalSize: 10000, @@ -234,7 +258,11 @@ describe('useInstallSizeDiff', () => { describe('fetch behavior', () => { it('calls the correct API endpoint for the comparison version', async () => { - const pkg = createPackage('my-pkg', { '1.0.0': '2020-01-01', '1.1.0': '2021-01-01' }) + const pkg = createPackage('my-pkg', { + '0.9.0': '2019-01-01', + '1.0.0': '2020-01-01', + '1.1.0': '2021-01-01', + }) const current = createInstallSize('my-pkg', { version: '1.1.0', totalSize: 7000 }) fetchSpy.mockResolvedValue(createInstallSize('my-pkg', { version: '1.0.0', totalSize: 5000 }))