Skip to content

Commit

Permalink
fix(ping): ping throws error when registry is offline
Browse files Browse the repository at this point in the history
  • Loading branch information
milaninfy committed Sep 27, 2024
1 parent 63d6a73 commit 85fcd49
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/utils/ping.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
// used by the ping and doctor commands
const fetch = require('npm-registry-fetch')
module.exports = async (flatOptions) => {
const res = await fetch('/-/ping?write=true', flatOptions)
const res = await fetch('/-/ping?write=true', { ...flatOptions, cache: false })
return res.json().catch(() => ({}))
}
18 changes: 18 additions & 0 deletions test/lib/commands/ping.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const t = require('tap')
const { load: loadMockNpm } = require('../../fixtures/mock-npm.js')
const MockRegistry = require('@npmcli/mock-registry')
const cacache = require('cacache')
const path = require('node:path')

t.test('no details', async t => {
const { npm, logs, joinedOutput } = await loadMockNpm(t)
Expand Down Expand Up @@ -74,3 +76,19 @@ t.test('invalid json', async t => {
details: {},
})
})
t.test('fail when registry is unreachable', async t => {
const { npm } = await loadMockNpm(t, {
config: { registry: 'https://ur.npmlocal.npmtest/' },
cacheDir: { _cacache: {} },
})
const url = `${npm.config.get('registry')}-/ping?write=true`
const cache = path.join(npm.cache, '_cacache')
await cacache.put(cache,
`make-fetch-happen:request-cache:${url}`,
'{}', { metadata: { url } }
)
t.rejects(npm.exec('ping', []), {
code: 'ENOTFOUND',
},
'throws ENOTFOUND error')
})

0 comments on commit 85fcd49

Please sign in to comment.