Skip to content

Commit

Permalink
improve tap error and runtime error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
tbeseda committed Sep 20, 2023
1 parent e72922c commit a13abc6
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 25 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@
"tap-arc.simple": "npm run --silent tape.simple | node index.js",
"tap-arc.throws": "npm run --silent tape.throws | node index.js",
"tap-arc.upstream-error": "npm run --silent tape.upstream-error | node index.js",
"tap-arc.runtime-error": "npm run --silent tape.runtime-error | node index.js",
"tape.diff": "tape test/mock/create-diff-tap.cjs",
"tape.empty": "tape test/mock/create-empty-tap.cjs",
"tape.mixed": "tape test/mock/create-mixed-tap.cjs",
"tape.passing": "tape test/mock/create-passing-tap.cjs",
"tape.simple": "tape test/mock/create-simple-tap.cjs",
"tape.throws": "tape test/mock/create-throws-tap.cjs",
"tape.upstream-error": "tape test/mock/create-upstream-error-tap.cjs",
"tape.runtime-error": "tape test/mock/create-runtime-error-tap.js",
"tape": "tape test/**/*-test.js | tap-min",
"test": "npm run lint && npm run tape"
},
Expand Down
4 changes: 2 additions & 2 deletions src/_printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export default function (options) {
pass ({ id, name }) {
return `${passMark}${d ? ` [${id}]` : ''} ${dim(name)}`
},
fail ({ id, name }) {
return `${failMark} [${id}] ${red(name)}`
fail ({ id, name, tapError }) {
return `${failMark} ${tapError ? red(`"${tapError}"`) : `[${id}] ${red(name)}`}`
},
skip ({ id, name }) {
return cyan(`${skipMark}${d ? ` [${id}]` : ''} ${name}`)
Expand Down
25 changes: 15 additions & 10 deletions src/tap-arc.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,21 @@ export default function createParser (options) {

parser.on('complete', (result) => {
if (!result.ok) {
if (
result.failures[0] &&
result.failures[0].tapError &&
result.failures[0].tapError.startsWith('incorrect number of tests')
) {
// custom failure was created by tap-parser
result.badCount = true // persisted to CLI process handler
result.failures.shift()
result.fail--
P(_.realBad(`\nExpected ${result.plan.end || '?'} assertions, parsed ${result.count || '?'}`))
const tapFailures = result.failures.filter((f) => f.tapError)
for (const tapFailure of tapFailures){
const { tapError } = tapFailure

if (tapError.startsWith('incorrect number of tests')) {
// custom failure was created by tap-parser
P(_.realBad(`\nExpected ${result.plan.end || '?'} assertions, parsed ${result.count || '?'}`))
result.badCount = true // persisted to CLI process handler
result.failures.shift()
result.fail--
}
else if (tapError.startsWith('no plan'))
P(_.realBad(`\nTAP test plan not found`))
else
P(_.realBad(`\n${tapError}`))
}

if (result.failures.length > 0) {
Expand Down
28 changes: 15 additions & 13 deletions test/exit-codes-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ test('streams and exit codes', (t) => {
})
}

for (const ft of [ 'upstream-error', 'runtime-error' ]) {
const filename = `create-${ft}-tap.cjs`
t.test(`exit(1) "${filename}" | tap-arc`, (t) => {
t.plan(3)
exec(
`npx tape ${join(mockPath, filename)} | node index.js`,
(error, stdout, stderr) => {
t.ok(error, `"${filename}" creates an error`)
t.ok(stderr, 'stderror should not be empty')
t.ok(stdout.indexOf('total: 0') > 0, '"total: 0" should occur in output')
}
)
})
}

for (const pt of [ 'passing', 'empty' ]) {
const filename = `create-${pt}-tap.cjs`
t.test(`exit(0) "${filename}" | tap-arc`, (t) => {
Expand All @@ -41,19 +56,6 @@ test('streams and exit codes', (t) => {
})
}

const filename = 'create-upstream-error-tap.cjs'
t.test(`exit(1) "${filename}" | tap-arc`, (t) => {
t.plan(3)
exec(
`npx tape ${join(mockPath, filename)} | node index.js`,
(error, stdout, stderr) => {
t.ok(error, `"${filename}" creates an error`)
t.ok(stderr, 'stderror should not be empty')
t.ok(stdout.indexOf('total: 0') > 0, '"total: 0" should occur in output')
}
)
})

const badCountPassing = 'missing-assertions-passing.txt'
t.test(`exit(0) "${badCountPassing}" | tap-arc`, (t) => {
t.plan(3)
Expand Down
11 changes: 11 additions & 0 deletions test/mock/create-runtime-error-tap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import test from 'tape'

test('runtime error test', (t) => {
t.plan(4)
const foo = {}

t.error(foo.bar, 'foo.bar should be error-ish') // pass
t.notok(foo, 'foo should be falsy') // fail
t.ok(foo.bar.baz, 'foo.bar.baz should be truthy') // runtime error
t.error(foo.bar.baz.quux, 'foo.bar.baz.quux should be error-ish') // doesn't run
})

0 comments on commit a13abc6

Please sign in to comment.