Skip to content

Commit 2bccf91

Browse files
committed
feat: adds mock for dist-tags
1 parent 865ee0a commit 2bccf91

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

test/fixtures/mock-npm.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const os = require('node:os')
22
const fs = require('node:fs').promises
33
const fsSync = require('node:fs')
44
const path = require('node:path')
5+
const npmFetch = require('npm-registry-fetch')
56
const tap = require('tap')
67
const mockLogs = require('./mock-logs.js')
78
const mockGlobals = require('@npmcli/mock-globals')
@@ -449,8 +450,34 @@ function workspaceMock (t, opts) {
449450
}
450451
}
451452

453+
const mockNpmRegistryFetch = (tags) => {
454+
const fetchOpts = {}
455+
const getRequest = async (url, opts) => {
456+
if (fetchOpts[url]) {
457+
fetchOpts[url].push(opts)
458+
} else {
459+
fetchOpts[url] = [opts]
460+
}
461+
const find = ({ ...tags })[url]
462+
if (typeof find === 'function') {
463+
return find()
464+
}
465+
return find
466+
}
467+
const nrf = async (url, opts) => {
468+
return {
469+
json: getRequest(url, opts),
470+
}
471+
}
472+
const mock = Object.assign(nrf, npmFetch, { json: getRequest })
473+
const mocks = { 'npm-registry-fetch': mock }
474+
const getOpts = (url) => fetchOpts[url]
475+
return { mocks, mock, fetchOpts, getOpts }
476+
}
477+
452478
module.exports = setupMockNpm
453479
module.exports.load = setupMockNpm
454480
module.exports.setGlobalNodeModules = setGlobalNodeModules
455481
module.exports.loadNpmWithRegistry = loadNpmWithRegistry
456482
module.exports.workspaceMock = workspaceMock
483+
module.exports.mockNpmRegistryFetch = mockNpmRegistryFetch

0 commit comments

Comments
 (0)