Getting [object Object] for custom error, not being reported by AVA
See original GitHub issueDescription
Custom error when returning a promise inside the test is returning a non descriptive error.
When doing a console.log
on the rejected promise, I can see the ValidationError name from Joi.
The error should show as: ValidationError: child "uuid" fails because ["uuid" with value "f47ac10b-58cc-x372-a567-0e02b2c3d479" fails to match the required pattern: /[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}/i]
Test Source
// index.js
import * as Joi from 'joi'
import * as bluebird from 'bluebird'
export const schema = Joi.object().keys({
fullname: Joi.string()
.description('validar nome completo')
.min(5)
.regex(/\s{1}/)
})
export function validate(obj) {
return bluebird.fromCallback((resolver) => {
Joi.validate(obj, schema, resolver)
})
}
// test/index.test.js
import test from 'ava'
import * as validation from '../index'
const valid = (obj, t) => {
return validation.validate(obj).then((c) => {
t.pass(c)
})
}
const ValidationError = (e) => e.name === 'ValidationError'
test('uuid', (t) => {
t.plan(3)
return valid({
uuid: 'f47ac10b-58cc-x372-a567-0e02b2c3d479' //should fail ava on purpose, change x to 4 to pass
}, t).then(() => valid({
uuid: '123e4567-e89b-12d3-a456-426655440000'
}, t)).catch(ValidationError, (c) => { // c is ValidationError, so it passes
t.pass()
}).then(() => valid({
uuid: 'a'
}, t)).catch(ValidationError, (c) => { // c is ValidationError, so it passes
t.pass()
})
})
Error Message & Stack Trace
> ava test/*.test.js --verbose
× uuid Planned for 3 assertions, but got 2.
1 test failed [19:01:55]
1. uuid
[object Object]
Test.fn (index.test.js:13:5)
Command-Line Arguments
ava test/*.test.js --verbose
Environment
Node.js v6.9.1
win32 10.0.14393
npm 4.0.1
ava 0.16.0
Issue Analytics
- State:
- Created 7 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
ava/03-assertions.md at main · avajs/ava - GitHub
Assert that actual is not the same as expected . This is based on Object.is() . Returns a boolean indicating whether the assertion...
Read more >Custom errors, extending Error - The Modern JavaScript Tutorial
But sometimes we have an error object coming from a 3rd-party library and there's no easy way to get its class. Then name...
Read more >java - What is a NullPointerException, and how do I fix it?
Throws an NPE with a custom error message if obj is null Objects.requireNonNull(obj, "obj must not be null");. Note that it's helpful to...
Read more >Error - JavaScript - MDN Web Docs - Mozilla
Runtime errors result in new Error objects being created and thrown. ... the constructed instance will not be a CustomError instance.
Read more >Creating Custom Errors in JavaScript - YouTube
Did you know that you can create your own custom Error objects in JavaScript? This can be accomplished with both the older ES5...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
t.fail()
will cause your test to fail right away. Which is appropriate for unwanted rejections.Perhaps try:
t.throws()
andt.notThrows()
work with promises, too.Closing due to inactivity.
@pocesar feel free to reply back whenever you have time and we’ll reopen the issue 😉