Skip to content

Commit ef5c2ee

Browse files
committed
fix(plugin-js-packages-e2e): npm outdated fallback to "wanted" if "current" is missing
happens when package not installed
1 parent efaf790 commit ef5c2ee

File tree

3 files changed

+19
-20
lines changed

3 files changed

+19
-20
lines changed
Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
import { objectToEntries } from '@code-pushup/utils';
22
import type { OutdatedResult } from '../../runner/outdated/types.js';
3-
import type { NpmNormalizedOverview, NpmOutdatedResultJson } from './types.js';
3+
import type { NpmOutdatedResultJson } from './types.js';
44

55
export function npmToOutdatedResult(output: string): OutdatedResult {
66
const npmOutdated = JSON.parse(output) as NpmOutdatedResultJson;
7-
// current might be missing in some cases
7+
// "current" might be missing in some cases, usually it is missing if the dependency is not installed, fallback to "wanted" should avoid this problem
88
// https://stackoverflow.com/questions/42267101/npm-outdated-command-shows-missing-in-current-version
9-
return objectToEntries(npmOutdated)
10-
.filter(
11-
(entry): entry is [string, NpmNormalizedOverview] =>
12-
entry[1].current != null,
13-
)
14-
.map(([name, overview]) => ({
15-
name,
16-
current: overview.current,
17-
latest: overview.latest,
18-
type: overview.type,
19-
...(overview.homepage != null && { url: overview.homepage }),
20-
}));
9+
return objectToEntries(npmOutdated).map(([name, overview]) => ({
10+
name,
11+
current: overview.current || overview.wanted,
12+
latest: overview.latest,
13+
type: overview.type,
14+
...(overview.homepage != null && { url: overview.homepage }),
15+
}));
2116
}

packages/plugin-js-packages/src/lib/package-managers/npm/outdated-result.unit.test.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe('npmToOutdatedResult', () => {
3939
]);
4040
});
4141

42-
it('should skip dependencies without current version', () => {
42+
it('should not skip dependencies without current version', () => {
4343
expect(
4444
npmToOutdatedResult(
4545
JSON.stringify({
@@ -50,7 +50,14 @@ describe('npmToOutdatedResult', () => {
5050
},
5151
}),
5252
),
53-
).toEqual([]);
53+
).toEqual([
54+
{
55+
current: undefined,
56+
latest: '5.3.0',
57+
name: 'typescript',
58+
type: 'dependencies',
59+
},
60+
]);
5461
});
5562

5663
it('should transform no dependencies to empty array', () => {

packages/plugin-js-packages/src/lib/package-managers/npm/types.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,10 @@ export type NpmAuditResultJson = {
3636
// Subset of NPM outdated JSON type
3737
export type NpmVersionOverview = {
3838
current?: string;
39+
wanted: string;
3940
latest: string;
4041
type: DependencyGroupLong;
4142
homepage?: string;
4243
};
4344

44-
export type NpmNormalizedOverview = Omit<NpmVersionOverview, 'current'> & {
45-
current: string;
46-
};
47-
4845
export type NpmOutdatedResultJson = Record<string, NpmVersionOverview>;

0 commit comments

Comments
 (0)