Skip to content

Commit 85fcd49

Browse files
author
Milan Meva
committed
fix(ping): ping throws error when registry is offline
1 parent 63d6a73 commit 85fcd49

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

lib/utils/ping.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
// used by the ping and doctor commands
33
const fetch = require('npm-registry-fetch')
44
module.exports = async (flatOptions) => {
5-
const res = await fetch('/-/ping?write=true', flatOptions)
5+
const res = await fetch('/-/ping?write=true', { ...flatOptions, cache: false })
66
return res.json().catch(() => ({}))
77
}

test/lib/commands/ping.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const t = require('tap')
22
const { load: loadMockNpm } = require('../../fixtures/mock-npm.js')
33
const MockRegistry = require('@npmcli/mock-registry')
4+
const cacache = require('cacache')
5+
const path = require('node:path')
46

57
t.test('no details', async t => {
68
const { npm, logs, joinedOutput } = await loadMockNpm(t)
@@ -74,3 +76,19 @@ t.test('invalid json', async t => {
7476
details: {},
7577
})
7678
})
79+
t.test('fail when registry is unreachable', async t => {
80+
const { npm } = await loadMockNpm(t, {
81+
config: { registry: 'https://ur.npmlocal.npmtest/' },
82+
cacheDir: { _cacache: {} },
83+
})
84+
const url = `${npm.config.get('registry')}-/ping?write=true`
85+
const cache = path.join(npm.cache, '_cacache')
86+
await cacache.put(cache,
87+
`make-fetch-happen:request-cache:${url}`,
88+
'{}', { metadata: { url } }
89+
)
90+
t.rejects(npm.exec('ping', []), {
91+
code: 'ENOTFOUND',
92+
},
93+
'throws ENOTFOUND error')
94+
})

0 commit comments

Comments
 (0)