0

I am writing integration tests against my db and graphql server.
jest is my test runner and assertions library.

For each test, I validate that I don't have any errors:

const actual = await graphql(schema, query);
expect(actual.errors).toBeUndefined();

When my tests are failing (something goes wrong along the way),
the actual.errors is not undefined and I expect jest to display the errors,
but this is what I get:

Expected value to be undefined, instead received
  [[GraphQLError: Invalid value [object Object]]]

This error message is useless if I can't see what is the "invalid value".
How can I configure jest so that it will print the error object?

itaied
  • 6,827
  • 13
  • 51
  • 86

2 Answers2

0

That is nothing with jest.The [object Object] is return by object.toString(),it invoked by the printer of the error log. If you want see detail of the object,you need rewrite the toString of the object It is like the below code:

obj = {x:"x"};
alert(obj)
obj.toString = function(){return JSON.stringify(obj)};
alert(obj)
sinbar
  • 933
  • 2
  • 7
  • 25
  • In order to do it, I need to override the language `Object`, which is a really bad idea... – itaied Oct 03 '17 at 07:20
  • Yes, override the native `Object` is a bad idea, but you can override the objects which you want to print,and override the `toString()` is a universal approach for log in java,reference :https://stackoverflow.com/questions/10734106/how-to-override-tostring-properly-in-java. I analogy it to javascript. – sinbar Oct 03 '17 at 11:46
0

The error message is GraphQLError: Invalid value [object Object] you're probably passing the string [object Object] when you mean to pass the object. Or your schema has a string where you think it has an object, or you're passing an object when you should be passing a string. With out your schema, query, or roots I'm just guessing.

The graphql object has a few properties that wont automatically log. They do help debugging a ton however. error.path and error.stack are my favorite. You can see the full list of them in the error object source.

reconbot
  • 5,138
  • 6
  • 45
  • 63