Skip to content

Commit

Permalink
test for npm test
Browse files Browse the repository at this point in the history
(obligatory 'yo dawg' reference)
  • Loading branch information
isaacs authored and ruyadorno committed Jul 29, 2020
1 parent 83d2591 commit d2821b7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 14 deletions.
23 changes: 9 additions & 14 deletions lib/test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
/* eslint-disable standard/no-callback-literal */
module.exports = test

const testCmd = require('./utils/lifecycle-cmd.js')('test')
const { completion, usage } = testCmd
const cmd = (args, cb) => testCmd(args, er => {
if (er && er.code === 'ELIFECYCLE') {
cb('Test failed. See above for more details.')
} else {
cb(er)
}
})

test.usage = testCmd.usage

function test (args, cb) {
testCmd(args, function (er) {
if (!er) return cb()
if (er.code === 'ELIFECYCLE') {
return cb('Test failed. See above for more details.')
}
return cb(er)
})
}
module.exports = Object.assign(cmd, { completion, usage })
40 changes: 40 additions & 0 deletions test/lib/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const t = require('tap')
const requireInject = require('require-inject')
let RUN_ARGS = null
const npmock = {
commands: {
run: (args, cb) => {
RUN_ARGS = args
cb()
}
}
}
const test = requireInject('../../lib/test.js', {
'../../lib/npm.js': npmock
})

t.test('run a test', t => {
test([], (er) => {
t.strictSame(RUN_ARGS, ['test'], 'added "test" to the args')
})
test(['hello', 'world'], (er) => {
t.strictSame(RUN_ARGS, ['test', 'hello', 'world'], 'added positional args')
})

const lcErr = Object.assign(new Error('should not see this'), {
code: 'ELIFECYCLE'
})
const otherErr = new Error('should see this')

npmock.commands.run = (args, cb) => cb(lcErr)
test([], (er) => {
t.equal(er, 'Test failed. See above for more details.')
})

npmock.commands.run = (args, cb) => cb(otherErr)
test([], (er) => {
t.match(er, { message: 'should see this' })
})

t.end()
})

0 comments on commit d2821b7

Please sign in to comment.