Skip to content

Commit

Permalink
npm: Throw a proper error when no mock version is provided.
Browse files Browse the repository at this point in the history
getPackageVersionProtected does catch any errors, but it rethrows them if they are not 404's.
  • Loading branch information
raineorshine committed Nov 5, 2023
1 parent 368371f commit 7789c72
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/package-managers/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ export async function packageAuthorChanged(
}

/** Returns true if an object is a Packument. */
const isPackument = (o: any): o is Partial<Packument> => o && (o.name || o.engines || o.version || o.versions)
const isPackument = (o: any): o is Partial<Packument> => !!(o && (o.name || o.engines || o.version || o.versions))

/** Creates a function with the same signature as viewMany that always returns the given versions. */
export const mockViewMany =
Expand All @@ -283,11 +283,10 @@ export const mockViewMany =

const version = isPackument(partialPackument) ? partialPackument.version : partialPackument

// if there is no version, hard exit
// otherwise getPackageProtected will swallow the error
if (!version) {
console.error(`No mock version supplied for ${name}`)
process.exit(1)
throw new Error(
`viewMany is mocked, but no mock version was supplied for ${name}. Make sure that all dependencies are mocked. `,
)
}

const time = (isPackument(partialPackument) && partialPackument.time?.[version]) || new Date().toISOString()
Expand Down

0 comments on commit 7789c72

Please sign in to comment.