1const t = require('tap') 2const { load: loadMockNpm } = require('../../fixtures/mock-npm.js') 3const MockRegistry = require('@npmcli/mock-registry') 4 5t.test('no details', async t => { 6 const { npm, logs, joinedOutput } = await loadMockNpm(t) 7 const registry = new MockRegistry({ 8 tap: t, 9 registry: npm.config.get('registry'), 10 }) 11 registry.ping() 12 await npm.exec('ping', []) 13 t.match(logs.notice, [['PING', 'https://registry.npmjs.org/'], ['PONG', /[0-9]+ms/]]) 14 t.equal(joinedOutput(), '') 15}) 16 17t.test('with details', async t => { 18 const { npm, logs, joinedOutput } = await loadMockNpm(t) 19 const registry = new MockRegistry({ 20 tap: t, 21 registry: npm.config.get('registry'), 22 }) 23 registry.ping({ body: { test: true } }) 24 await npm.exec('ping', []) 25 t.match(logs.notice, [ 26 ['PING', 'https://registry.npmjs.org/'], 27 ['PONG', /[0-9]+ms/], 28 ['PONG', '{\n "test": true\n}'], 29 ]) 30 t.match(joinedOutput(), '') 31}) 32 33t.test('valid json', async t => { 34 const { npm, logs, joinedOutput } = await loadMockNpm(t, { 35 config: { json: true }, 36 }) 37 const registry = new MockRegistry({ 38 tap: t, 39 registry: npm.config.get('registry'), 40 }) 41 registry.ping() 42 await npm.exec('ping', []) 43 t.match(logs.notice, [['PING', 'https://registry.npmjs.org/'], ['PONG', /[0-9]+ms/]]) 44 t.match(JSON.parse(joinedOutput()), { 45 registry: npm.config.get('registry'), 46 time: /[0-9]+/, 47 details: {}, 48 }) 49}) 50 51t.test('invalid json', async t => { 52 const { npm, logs, joinedOutput } = await loadMockNpm(t, { 53 config: { json: true }, 54 }) 55 const registry = new MockRegistry({ 56 tap: t, 57 registry: npm.config.get('registry'), 58 }) 59 registry.ping({ body: '{not: real"json]' }) 60 await npm.exec('ping', []) 61 t.match(logs.notice, [['PING', 'https://registry.npmjs.org/'], ['PONG', /[0-9]+ms/]]) 62 t.match(JSON.parse(joinedOutput()), { 63 registry: npm.config.get('registry'), 64 time: /[0-9]+/, 65 details: {}, 66 }) 67}) 68