question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Getting [object Object] for custom error, not being reported by AVA

See original GitHub issue

Description

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:closed
  • Created 7 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
novemberborncommented, Oct 31, 2016

t.fail() will cause your test to fail right away. Which is appropriate for unwanted rejections.

Perhaps try:

test('uuid', async t => {
  const first = await t.throws(validation.validate({ uuid: 'f47ac10b-58cc-x4372-a567-0e02b2c3d479' }))
  t.true(first.name === 'ValidationError')

  t.notThrows(validation.validate({ uuid: '123e4567-e89b-12d3-a456-426655440000' }))
  t.notThrows(validation.validate({ uuid: 'a' }))
})

t.throws() and t.notThrows() work with promises, too.

0reactions
vadimdemedescommented, Nov 26, 2016

Closing due to inactivity.

@pocesar feel free to reply back whenever you have time and we’ll reopen the issue 😉

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found