-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add tests for ping util and cmd
- Loading branch information
Showing
2 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
const { test } = require('tap') | ||
const requireInject = require('require-inject') | ||
|
||
test('pings', (t) => { | ||
t.plan(8) | ||
|
||
const flatOptions = { registry: 'https://registry.npmjs.org' } | ||
let noticeCalls = 0 | ||
const ping = requireInject('../../lib/ping.js', { | ||
'../../lib/npm.js': { flatOptions }, | ||
'../../lib/utils/ping.js': function (spec) { | ||
t.equal(spec, flatOptions, 'passes flatOptions') | ||
return {} | ||
}, | ||
npmlog: { | ||
notice: (type, spec) => { | ||
++noticeCalls | ||
if (noticeCalls === 1) { | ||
t.equal(type, 'PING', 'should log a PING') | ||
t.equal(spec, flatOptions.registry, 'should log the registry url') | ||
} else { | ||
t.equal(type, 'PONG', 'should log a PONG') | ||
t.match(spec, /\d+ms/, 'should log the elapsed milliseconds') | ||
} | ||
} | ||
} | ||
}) | ||
|
||
ping([], (err) => { | ||
t.equal(noticeCalls, 2, 'should have logged 2 lines') | ||
t.ifError(err, 'npm ping') | ||
t.ok('should be able to ping') | ||
}) | ||
}) | ||
|
||
test('pings and logs details', (t) => { | ||
t.plan(10) | ||
|
||
const flatOptions = { registry: 'https://registry.npmjs.org' } | ||
const details = { extra: 'data' } | ||
let noticeCalls = 0 | ||
const ping = requireInject('../../lib/ping.js', { | ||
'../../lib/npm.js': { flatOptions }, | ||
'../../lib/utils/ping.js': function (spec) { | ||
t.equal(spec, flatOptions, 'passes flatOptions') | ||
return details | ||
}, | ||
npmlog: { | ||
notice: (type, spec) => { | ||
++noticeCalls | ||
if (noticeCalls === 1) { | ||
t.equal(type, 'PING', 'should log a PING') | ||
t.equal(spec, flatOptions.registry, 'should log the registry url') | ||
} else if (noticeCalls == 2) { | ||
t.equal(type, 'PONG', 'should log a PONG') | ||
t.match(spec, /\d+ms/, 'should log the elapsed milliseconds') | ||
} else { | ||
t.equal(type, 'PONG', 'should log a PONG') | ||
const parsed = JSON.parse(spec) | ||
t.match(parsed, details, 'should log JSON stringified details') | ||
} | ||
} | ||
} | ||
}) | ||
|
||
ping([], (err) => { | ||
t.equal(noticeCalls, 3, 'should have logged 3 lines') | ||
t.ifError(err, 'npm ping') | ||
t.ok('should be able to ping') | ||
}) | ||
}) | ||
|
||
test('pings and returns json', (t) => { | ||
t.plan(11) | ||
|
||
const flatOptions = { registry: 'https://registry.npmjs.org', json: true } | ||
const details = { extra: 'data' } | ||
let noticeCalls = 0 | ||
const ping = requireInject('../../lib/ping.js', { | ||
'../../lib/npm.js': { flatOptions }, | ||
'../../lib/utils/ping.js': function (spec) { | ||
t.equal(spec, flatOptions, 'passes flatOptions') | ||
return details | ||
}, | ||
'../../lib/utils/output.js': function (spec) { | ||
const parsed = JSON.parse(spec) | ||
t.equal(parsed.registry, flatOptions.registry, 'returns the correct registry url') | ||
t.match(parsed.details, details, 'prints returned details') | ||
t.type(parsed.time, 'number', 'returns time as a number') | ||
}, | ||
npmlog: { | ||
notice: (type, spec) => { | ||
++noticeCalls | ||
if (noticeCalls === 1) { | ||
t.equal(type, 'PING', 'should log a PING') | ||
t.equal(spec, flatOptions.registry, 'should log the registry url') | ||
} else { | ||
t.equal(type, 'PONG', 'should log a PONG') | ||
t.match(spec, /\d+ms/, 'should log the elapsed milliseconds') | ||
} | ||
} | ||
} | ||
}) | ||
|
||
ping([], (err) => { | ||
t.equal(noticeCalls, 2, 'should have logged 2 lines') | ||
t.ifError(err, 'npm ping') | ||
t.ok('should be able to ping') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const { test } = require('tap') | ||
const requireInject = require('require-inject') | ||
|
||
test('pings', async (t) => { | ||
t.plan(3) | ||
|
||
const options = { fake: 'options' } | ||
const response = { some: 'details' } | ||
const ping = requireInject('../../../lib/utils/ping.js', { | ||
'npm-registry-fetch': (url, opts) => { | ||
t.equal(url, '/-/ping?write=true', 'calls the correct url') | ||
t.equal(opts, options, 'passes through options') | ||
return { json: () => Promise.resolve(response) } | ||
} | ||
}) | ||
|
||
const res = await ping(options) | ||
t.match(res, response, 'returns json response') | ||
}) | ||
|
||
test('catches errors and returns empty json', async (t) => { | ||
t.plan(3) | ||
|
||
const options = { fake: 'options' } | ||
const response = { some: 'details' } | ||
const ping = requireInject('../../../lib/utils/ping.js', { | ||
'npm-registry-fetch': (url, opts) => { | ||
t.equal(url, '/-/ping?write=true', 'calls the correct url') | ||
t.equal(opts, options, 'passes through options') | ||
return { json: () => Promise.reject(response) } | ||
} | ||
}) | ||
|
||
const res = await ping(options) | ||
t.match(res, {}, 'returns empty json response') | ||
}) |