Skip to content

Commit

Permalink
feat: adds mock for dist-tags
Browse files Browse the repository at this point in the history
  • Loading branch information
reggi committed Nov 25, 2024
1 parent 295c0ca commit ec8b77c
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions test/fixtures/mock-npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const os = require('node:os')
const fs = require('node:fs').promises
const fsSync = require('node:fs')
const path = require('node:path')
const npmFetch = require('npm-registry-fetch')
const tap = require('tap')
const mockLogs = require('./mock-logs.js')
const mockGlobals = require('@npmcli/mock-globals')
Expand Down Expand Up @@ -449,8 +450,34 @@ function workspaceMock (t, opts) {
}
}

const mockNpmRegistryFetch = (tags) => {
const fetchOpts = {}
const getRequest = async (url, opts) => {
if (fetchOpts[url]) {
fetchOpts[url].push(opts)
} else {
fetchOpts[url] = [opts]
}
const find = ({ ...tags })[url]
if (typeof find === 'function') {
return find()
}
return find
}
const nrf = async (url, opts) => {
return {
json: getRequest(url, opts),
}
}
const mock = Object.assign(nrf, npmFetch, { json: getRequest })
const mocks = { 'npm-registry-fetch': mock }
const getOpts = (url) => fetchOpts[url]
return { mocks, mock, fetchOpts, getOpts }
}

module.exports = setupMockNpm
module.exports.load = setupMockNpm
module.exports.setGlobalNodeModules = setGlobalNodeModules
module.exports.loadNpmWithRegistry = loadNpmWithRegistry
module.exports.workspaceMock = workspaceMock
module.exports.mockNpmRegistryFetch = mockNpmRegistryFetch

0 comments on commit ec8b77c

Please sign in to comment.