Skip to content

Commit 85c2bc8

Browse files
committed
mockViewMany: Add support for partial Packuments.
1 parent 6d01108 commit 85c2bc8

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/package-managers/npm.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,24 +271,30 @@ export async function packageAuthorChanged(
271271
return false
272272
}
273273

274+
/** Returns true if an object is a Packument. */
275+
const isPackument = (o: any): o is Partial<Packument> => o && (o.name || o.engines || o.version || o.versions)
276+
274277
/** Creates a function with the same signature as viewMany that always returns the given versions. */
275278
export const mockViewMany =
276279
(mockReturnedVersions: MockedVersions) =>
277280
(name: string, fields: string[], currentVersion: Version, options: Options): Promise<Packument> => {
278-
const version =
281+
// a partial Packument
282+
const partialPackument =
279283
typeof mockReturnedVersions === 'function'
280284
? mockReturnedVersions(options)?.[name]
281-
: typeof mockReturnedVersions === 'string'
285+
: typeof mockReturnedVersions === 'string' || isPackument(mockReturnedVersions)
282286
? mockReturnedVersions
283287
: mockReturnedVersions[name]
284288

289+
const version = isPackument(partialPackument) ? partialPackument.version : partialPackument
285290
const packument = {
286291
name,
287292
engines: { node: '' },
288293
time: { [version || '']: new Date().toISOString() },
289294
version: version || '',
290295
// versions are not needed in nested packument
291296
versions: [],
297+
...(isPackument(partialPackument) ? partialPackument : null),
292298
}
293299

294300
return Promise.resolve({

src/types/MockedVersions.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { Index } from './IndexType'
22
import { Options } from './Options'
3+
import { Packument } from './Packument'
34
import { Version } from './Version'
45

5-
export type MockedVersions = Version | Index<Version> | ((options: Options) => Index<Version> | null)
6+
export type MockedVersions =
7+
| Version
8+
| Partial<Packument>
9+
| Index<Version>
10+
| Index<Partial<Packument>>
11+
| ((options: Options) => Index<Version> | Index<Partial<Packument>> | null)

0 commit comments

Comments
 (0)