Skip to content

Commit

Permalink
t.rejects: handle getting expected error but not options
Browse files Browse the repository at this point in the history
Fix #384
  • Loading branch information
isaacs committed Aug 12, 2017
1 parent 716414a commit 52d5983
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
4 changes: 1 addition & 3 deletions lib/asserts.js
Expand Up @@ -282,7 +282,7 @@ function decorate (t) {
// like throws, but rejects a returned promise instead
// also, can pass in a promise instead of a function
t.addAssert('rejects', 4, function (fn_, wanted_, m_, e_, m, e__) {
var fn, wanted, e, promise
var fn, wanted, e = {}, promise
for (var i = 0; i < arguments.length - 1; i++) {
var arg = arguments[i]
if (typeof arg === 'function') {
Expand Down Expand Up @@ -330,15 +330,13 @@ function decorate (t) {
wanted = w

m += ': ' + (wanted.name || 'Error') + ' ' + wanted.message
e = e || {}
if (e !== wanted) {
e.wanted = wanted
}
}
}

if (!promise && typeof fn !== 'function') {
e = e || {}
e.todo = true
return this.pass(m, e)
}
Expand Down
19 changes: 17 additions & 2 deletions test/test/rejects--buffer.tap
Expand Up @@ -99,7 +99,22 @@ not ok 12 - expect rejected Promise: Error own ___/# time=[0-9.]+(ms)?/~~~ {
# failed 1 test
}

1..12
# failed 8 of 12 tests
ok 13 - a ___/# time=[0-9.]+(ms)?/~~~ {
ok 1 - a
1..1
}

not ok 14 - b ___/# time=[0-9.]+(ms)?/~~~ {
not ok 1 - b
---
{"at":{"column":3,"file":"test/test/rejects.js","line":78},"found":{"message":"foo","name":"Error"},"pattern":{"message":"bar"},"source":"t.rejects(prom, { message: 'bar' }, 'b')\n"}
...

1..1
# failed 1 test
}

1..14
# failed 9 of 14 tests
___/# time=[0-9.]+(ms)?/~~~

4 changes: 4 additions & 0 deletions test/test/rejects.js
Expand Up @@ -72,3 +72,7 @@ t.rejects(
resolve(10)
}),
new Error('own'))

const prom = new Promise((resolve, reject) => reject(new Error('foo')))
t.rejects(prom, { message: 'foo' }, 'a')
t.rejects(prom, { message: 'bar' }, 'b')
19 changes: 17 additions & 2 deletions test/test/rejects.tap
Expand Up @@ -99,7 +99,22 @@ not ok 12 - expect rejected Promise: Error own ___/# time=[0-9.]+(ms)?/~~~ {
# failed 1 test
}

1..12
# failed 8 of 12 tests
ok 13 - a ___/# time=[0-9.]+(ms)?/~~~ {
ok 1 - a
1..1
}

not ok 14 - b ___/# time=[0-9.]+(ms)?/~~~ {
not ok 1 - b
---
{"at":{"column":3,"file":"test/test/rejects.js","line":78},"found":{"message":"foo","name":"Error"},"pattern":{"message":"bar"},"source":"t.rejects(prom, { message: 'bar' }, 'b')\n"}
...

1..1
# failed 1 test
}

1..14
# failed 9 of 14 tests
___/# time=[0-9.]+(ms)?/~~~

0 comments on commit 52d5983

Please sign in to comment.