Skip to content

Commit

Permalink
fix(test): trying to handle errors better
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Jun 25, 2017
1 parent 40add0b commit d41256b
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 17 deletions.
19 changes: 11 additions & 8 deletions src/exec-test.js
Expand Up @@ -4,11 +4,14 @@ var spawn = require('child_process').spawn
var q = require('q')
var npmPath = require('./npm-test').npmPath
var _ = require('lodash')
const debug = require('debug')('next-update')

// returns a promise
// TODO switch to execa
function test (options, testCommand) {
options = options || {}
var log = options.tldr ? _.noop : console.log.bind(console)
debug('exec-test "%s"', testCommand)

verify.unemptyString(testCommand, 'missing test command string')
log(' ', testCommand)
Expand Down Expand Up @@ -40,21 +43,21 @@ function test (options, testCommand) {
console.error('test command: "' + testCommand + '"')
console.error(err)
testErrors += err.toString()
deferred.reject({
code: err.code,
errors: testErrors
})
const e = new Error('test command failed')
e.code = err.code
e.errors = testErrors
deferred.reject(e)
})

testProcess.on('exit', function (code) {
if (code) {
console.error('testProcess test returned', code)
console.error('test errors:\n' + testErrors)
console.error(testOutput)
deferred.reject({
code: code,
errors: testErrors
})
const e = new Error('test exit code means error')
e.code = code
e.errors = testErrors
return deferred.reject(e)
}
deferred.resolve()
})
Expand Down
1 change: 1 addition & 0 deletions src/next-update.js
Expand Up @@ -68,6 +68,7 @@ function checkCurrentInstall (options) {
return cleanDependencies()
.then(checkDependenciesInstalled)
.then(function () {
log('running test command')
return runTest(options, options.testCommand)()
})
.then(function () {
Expand Down
11 changes: 6 additions & 5 deletions src/npm-test.js
Expand Up @@ -61,14 +61,15 @@ function test (options) {
var deferred = q.defer()
npm.on('exit', function (code) {
if (code) {
errorLog('npm test returned', code)
errorLog('npm test returned', code, '\n')
errorLog('test output:\n' + testOutput)
errorLog('test errors:\n' + testErrors)

deferred.reject({
code: code,
errors: testErrors
})
const e = new Error('npm test exit code means error')
e.code = code
e.errors = testErrors

deferred.reject(e)
}
deferred.resolve()
})
Expand Down
13 changes: 12 additions & 1 deletion src/test-module-version.js
Expand Up @@ -174,6 +174,7 @@ function testModuleVersions (options, results) {
})
var checkAllPromise = checkPromises.reduce(q.when, q())
if (options.keep) {
debug('keep working updates for %s', name)
checkAllPromise = checkAllPromise.then(function (result) {
verify.array(result, 'expected array of results', result)
var lastSuccess = _.last(_.filter(result, { works: true }))
Expand All @@ -194,10 +195,16 @@ function testModuleVersions (options, results) {
})
} else {
checkAllPromise = checkAllPromise
.then(restoreVersionFunc)
.then(restoreVersionFunc, (err) => {
console.error('Could not check all versions')
console.error(err)
throw err
})
}
checkAllPromise
.then(function (result) {
debug('got result')
debug(result)
check.verify.array(result, 'could not get result array')
results.push(result)
deferred.resolve(results)
Expand Down Expand Up @@ -290,13 +297,15 @@ function testModuleVersion (options, results) {
}, function (error) {
reportFailure(nameVersion + ' tests failed :(', options.color)

debug('sending stats results')
stats.sendUpdateResult({
name: options.name,
from: options.currentVersion,
to: options.version,
success: false
})

debug('checking error code', error.code)
verify.number(error.code, 'expected code in error ' +
JSON.stringify(error, null, 2))

Expand Down Expand Up @@ -324,6 +333,8 @@ function testPromise (options, command) {
if (command) {
verify.unemptyString(command, 'expected string command, not ' + command)
testFunction = execTest.bind(null, options, command)
} else {
debug('missing test command')
}
return testFunction
}
Expand Down
8 changes: 7 additions & 1 deletion src/utils.js
Expand Up @@ -40,9 +40,15 @@ function getTestCommand (packageFilename, moduleName) {
return config.commands[moduleName]
}

const stringify = (x) => JSON.stringify(x, null, 2)

const errorObject = (x) => (new Error(stringify(x)))

module.exports = {
name,
getConfig,
getSkippedModules,
getTestCommand
getTestCommand,
stringify,
errorObject
}
4 changes: 3 additions & 1 deletion test/as-parent-spec.js
Expand Up @@ -39,7 +39,8 @@ describe('testing check-types', () => {
this.timeout(TWO_MINUTES)
const opts = {
module: 'check-types',
latest: true
latest: true,
keep: false
}
const removeVersions = (results) => results.map(r => {
la(is.semver(r.version), 'expected version', r)
Expand All @@ -64,6 +65,7 @@ describe('testing check-types', () => {

const opts = {
module: 'check-types',
keep: false,
limit
}
return snapShot(nextUpdate(opts))
Expand Down
2 changes: 1 addition & 1 deletion test/test-next-updater/package.json
Expand Up @@ -18,6 +18,6 @@
"author": "Gleb Bahmutov <gleb.bahmutov@gmail.com>",
"license": "MIT",
"dependencies": {
"check-types": "7.0.1"
"check-types": "0.6.5"
}
}

0 comments on commit d41256b

Please sign in to comment.