Skip to content

Commit

Permalink
Error.toStringTag change return string from 'Object' to 'GraphQLError' (
Browse files Browse the repository at this point in the history
#3261)

We maintained this hack for a long time to make our Errors compatible
with with chai's `to.deep.equal`
  • Loading branch information
IvanGoncharov committed Sep 15, 2021
1 parent 2df59f1 commit 9e584e3
Show file tree
Hide file tree
Showing 21 changed files with 347 additions and 311 deletions.
40 changes: 40 additions & 0 deletions src/__testUtils__/expectJSON.ts
@@ -0,0 +1,40 @@
import { expect } from 'chai';

import { mapValue } from '../jsutils/mapValue';
import { isObjectLike } from '../jsutils/isObjectLike';

/**
* Deeply transforms an arbitrary value to a JSON-safe value by calling toJSON
* on any nested value which defines it.
*/
function toJSONDeep(value: unknown): unknown {
if (!isObjectLike(value)) {
return value;
}

if (typeof value.toJSON === 'function') {
return value.toJSON();
}

if (Array.isArray(value)) {
return value.map(toJSONDeep);
}

return mapValue(value, toJSONDeep);
}

export function expectJSON(value: unknown) {
return expect(toJSONDeep(value));
}

export function expectToThrowJSON(fn: () => unknown) {
function mapException(): unknown {
try {
return fn();
} catch (error) {
throw toJSONDeep(error);
}
}

return expect(mapException).to.throw();
}
8 changes: 5 additions & 3 deletions src/__tests__/starWarsQuery-test.ts
@@ -1,6 +1,8 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';

import { expectJSON } from '../__testUtils__/expectJSON';

import { graphql } from '../graphql';

import { StarWarsSchema as schema } from './starWarsSchema';
Expand Down Expand Up @@ -393,7 +395,7 @@ describe('Star Wars Query Tests', () => {
`;

const result = await graphql({ schema, source });
expect(result).to.deep.equal({
expectJSON(result).to.deep.equal({
data: {
hero: {
name: 'R2-D2',
Expand Down Expand Up @@ -424,7 +426,7 @@ describe('Star Wars Query Tests', () => {
`;

const result = await graphql({ schema, source });
expect(result).to.deep.equal({
expectJSON(result).to.deep.equal({
data: {
hero: {
name: 'R2-D2',
Expand Down Expand Up @@ -475,7 +477,7 @@ describe('Star Wars Query Tests', () => {
`;

const result = await graphql({ schema, source });
expect(result).to.deep.equal({
expectJSON(result).to.deep.equal({
data: {
mainHero: {
name: 'R2-D2',
Expand Down
2 changes: 1 addition & 1 deletion src/error/GraphQLError.ts
Expand Up @@ -240,7 +240,7 @@ export class GraphQLError extends Error {
}

get [Symbol.toStringTag](): string {
return 'Object';
return 'GraphQLError';
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/execution/__tests__/abstract-test.ts
@@ -1,6 +1,8 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';

import { expectJSON } from '../../__testUtils__/expectJSON';

import { parse } from '../../language/parser';

import { GraphQLSchema } from '../../type/schema';
Expand Down Expand Up @@ -204,7 +206,7 @@ describe('Execute: Handles execution of abstract types', () => {
}
`;

expect(await executeQuery({ schema, query })).to.deep.equal({
expectJSON(await executeQuery({ schema, query })).to.deep.equal({
data: {
pets: [null, null],
},
Expand Down Expand Up @@ -358,7 +360,7 @@ describe('Execute: Handles execution of abstract types', () => {
}
`;

expect(await executeQuery({ schema, query })).to.deep.equal({
expectJSON(await executeQuery({ schema, query })).to.deep.equal({
data: {
pets: [null, null],
},
Expand Down Expand Up @@ -539,7 +541,7 @@ describe('Execute: Handles execution of abstract types', () => {
const result = executeSync({ schema, document, rootValue });
return {
toEqual(message: string) {
expect(result).to.deep.equal({
expectJSON(result).to.deep.equal({
data: { pet: null },
errors: [
{
Expand Down
20 changes: 11 additions & 9 deletions src/execution/__tests__/executor-test.ts
@@ -1,6 +1,8 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';

import { expectJSON } from '../../__testUtils__/expectJSON';

import { inspect } from '../../jsutils/inspect';
import { invariant } from '../../jsutils/invariant';

Expand Down Expand Up @@ -497,7 +499,7 @@ describe('Execute: Handles basic execution tasks', () => {
};

const result = await execute({ schema, document, rootValue });
expect(result).to.deep.equal({
expectJSON(result).to.deep.equal({
data: {
sync: 'sync',
syncError: null,
Expand Down Expand Up @@ -611,7 +613,7 @@ describe('Execute: Handles basic execution tasks', () => {

const result = await execute({ schema, document });

expect(result).to.deep.equal({
expectJSON(result).to.deep.equal({
data: { foods: null },
errors: [
{
Expand Down Expand Up @@ -670,7 +672,7 @@ describe('Execute: Handles basic execution tasks', () => {
`);

const result = executeSync({ schema, document });
expect(result).to.deep.equal({
expectJSON(result).to.deep.equal({
data: {
nullableA: {
aliasedA: null,
Expand Down Expand Up @@ -752,7 +754,7 @@ describe('Execute: Handles basic execution tasks', () => {
const rootValue = { a: 'b' };

const result = executeSync({ schema, document, rootValue });
expect(result).to.deep.equal({
expectJSON(result).to.deep.equal({
errors: [{ message: 'Must provide an operation.' }],
});
});
Expand All @@ -772,7 +774,7 @@ describe('Execute: Handles basic execution tasks', () => {
`);

const result = executeSync({ schema, document });
expect(result).to.deep.equal({
expectJSON(result).to.deep.equal({
errors: [
{
message:
Expand All @@ -798,7 +800,7 @@ describe('Execute: Handles basic execution tasks', () => {
const operationName = 'UnknownExample';

const result = executeSync({ schema, document, operationName });
expect(result).to.deep.equal({
expectJSON(result).to.deep.equal({
errors: [{ message: 'Unknown operation named "UnknownExample".' }],
});
});
Expand All @@ -816,7 +818,7 @@ describe('Execute: Handles basic execution tasks', () => {
const operationName = '';

const result = executeSync({ schema, document, operationName });
expect(result).to.deep.equal({
expectJSON(result).to.deep.equal({
errors: [{ message: 'Unknown operation named "".' }],
});
});
Expand Down Expand Up @@ -1074,7 +1076,7 @@ describe('Execute: Handles basic execution tasks', () => {
};

const result = executeSync({ schema, document, rootValue });
expect(result).to.deep.equal({
expectJSON(result).to.deep.equal({
data: {
specials: [{ value: 'foo' }, null],
},
Expand Down Expand Up @@ -1118,7 +1120,7 @@ describe('Execute: Handles basic execution tasks', () => {
});

const result = executeSync({ schema, document: parse('{ customScalar }') });
expect(result).to.deep.equal({
expectJSON(result).to.deep.equal({
data: { customScalar: null },
errors: [
{
Expand Down
28 changes: 15 additions & 13 deletions src/execution/__tests__/lists-test.ts
@@ -1,6 +1,8 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';

import { expectJSON } from '../../__testUtils__/expectJSON';

import { parse } from '../../language/parser';

import { buildSchema } from '../../utilities/buildASTSchema';
Expand Down Expand Up @@ -50,7 +52,7 @@ describe('Execute: Accepts any iterable as list value', () => {
it('Does not accept (Iterable) String-literal as a List value', () => {
const listField = 'Singular';

expect(complete({ listField })).to.deep.equal({
expectJSON(complete({ listField })).to.deep.equal({
data: { listField: null },
errors: [
{
Expand Down Expand Up @@ -129,11 +131,11 @@ describe('Execute: Handles list nullability', () => {
expect(await complete({ listField, as: '[Int]!' })).to.deep.equal({
data: { listField: [1, null, 2] },
});
expect(await complete({ listField, as: '[Int!]' })).to.deep.equal({
expectJSON(await complete({ listField, as: '[Int!]' })).to.deep.equal({
data: { listField: null },
errors,
});
expect(await complete({ listField, as: '[Int!]!' })).to.deep.equal({
expectJSON(await complete({ listField, as: '[Int!]!' })).to.deep.equal({
data: null,
errors,
});
Expand All @@ -152,14 +154,14 @@ describe('Execute: Handles list nullability', () => {
expect(await complete({ listField, as: '[Int]' })).to.deep.equal({
data: { listField: null },
});
expect(await complete({ listField, as: '[Int]!' })).to.deep.equal({
expectJSON(await complete({ listField, as: '[Int]!' })).to.deep.equal({
data: null,
errors,
});
expect(await complete({ listField, as: '[Int!]' })).to.deep.equal({
data: { listField: null },
});
expect(await complete({ listField, as: '[Int!]!' })).to.deep.equal({
expectJSON(await complete({ listField, as: '[Int!]!' })).to.deep.equal({
data: null,
errors,
});
Expand All @@ -175,19 +177,19 @@ describe('Execute: Handles list nullability', () => {
},
];

expect(await complete({ listField, as: '[Int]' })).to.deep.equal({
expectJSON(await complete({ listField, as: '[Int]' })).to.deep.equal({
data: { listField: [1, null, 2] },
errors,
});
expect(await complete({ listField, as: '[Int]!' })).to.deep.equal({
expectJSON(await complete({ listField, as: '[Int]!' })).to.deep.equal({
data: { listField: [1, null, 2] },
errors,
});
expect(await complete({ listField, as: '[Int!]' })).to.deep.equal({
expectJSON(await complete({ listField, as: '[Int!]' })).to.deep.equal({
data: { listField: null },
errors,
});
expect(await complete({ listField, as: '[Int!]!' })).to.deep.equal({
expectJSON(await complete({ listField, as: '[Int!]!' })).to.deep.equal({
data: null,
errors,
});
Expand All @@ -203,19 +205,19 @@ describe('Execute: Handles list nullability', () => {
},
];

expect(await complete({ listField, as: '[Int]' })).to.deep.equal({
expectJSON(await complete({ listField, as: '[Int]' })).to.deep.equal({
data: { listField: null },
errors,
});
expect(await complete({ listField, as: '[Int]!' })).to.deep.equal({
expectJSON(await complete({ listField, as: '[Int]!' })).to.deep.equal({
data: null,
errors,
});
expect(await complete({ listField, as: '[Int!]' })).to.deep.equal({
expectJSON(await complete({ listField, as: '[Int!]' })).to.deep.equal({
data: { listField: null },
errors,
});
expect(await complete({ listField, as: '[Int!]!' })).to.deep.equal({
expectJSON(await complete({ listField, as: '[Int!]!' })).to.deep.equal({
data: null,
errors,
});
Expand Down
4 changes: 3 additions & 1 deletion src/execution/__tests__/mutations-test.ts
@@ -1,6 +1,8 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';

import { expectJSON } from '../../__testUtils__/expectJSON';

import { resolveOnNextTick } from '../../__testUtils__/resolveOnNextTick';

import { parse } from '../../language/parser';
Expand Down Expand Up @@ -167,7 +169,7 @@ describe('Execute: Handles mutation execution ordering', () => {
const rootValue = new Root(6);
const result = await execute({ schema, document, rootValue });

expect(result).to.deep.equal({
expectJSON(result).to.deep.equal({
data: {
first: { theNumber: 1 },
second: { theNumber: 2 },
Expand Down

0 comments on commit 9e584e3

Please sign in to comment.