Skip to content

Commit

Permalink
expectJSON: return custom object instead of expect (#3310)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Oct 12, 2021
1 parent f45bc0a commit 01fa868
Show file tree
Hide file tree
Showing 52 changed files with 556 additions and 546 deletions.
14 changes: 12 additions & 2 deletions src/__testUtils__/expectJSON.ts
Expand Up @@ -23,8 +23,18 @@ function toJSONDeep(value: unknown): unknown {
return mapValue(value, toJSONDeep);
}

export function expectJSON(value: unknown) {
return expect(toJSONDeep(value));
export function expectJSON(actual: unknown) {
return {
toDeepEqual(expected: unknown) {
expect(toJSONDeep(actual)).to.deep.equal(toJSONDeep(expected));
},
toDeepNestedProperty(path: string, expected: unknown) {
expect(toJSONDeep(actual)).to.deep.nested.property(
path,
toJSONDeep(expected),
);
},
};
}

export function expectToThrowJSON(fn: () => unknown) {
Expand Down
6 changes: 3 additions & 3 deletions src/__tests__/starWarsQuery-test.ts
Expand Up @@ -395,7 +395,7 @@ describe('Star Wars Query Tests', () => {
`;

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

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

const result = await graphql({ schema, source });
expectJSON(result).to.deep.equal({
expectJSON(result).toDeepEqual({
data: {
mainHero: {
name: 'R2-D2',
Expand Down
8 changes: 4 additions & 4 deletions src/execution/__tests__/abstract-test.ts
Expand Up @@ -39,7 +39,7 @@ async function executeQuery(args: {
contextValue: { async: true },
});

expect(result).to.deep.equal(asyncResult);
expectJSON(result).toDeepEqual(asyncResult);
return result;
}

Expand Down Expand Up @@ -206,7 +206,7 @@ describe('Execute: Handles execution of abstract types', () => {
}
`;

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

expectJSON(await executeQuery({ schema, query })).to.deep.equal({
expectJSON(await executeQuery({ schema, query })).toDeepEqual({
data: {
pets: [null, null],
},
Expand Down Expand Up @@ -541,7 +541,7 @@ describe('Execute: Handles execution of abstract types', () => {
const result = executeSync({ schema, document, rootValue });
return {
toEqual(message: string) {
expectJSON(result).to.deep.equal({
expectJSON(result).toDeepEqual({
data: { pet: null },
errors: [
{
Expand Down
24 changes: 12 additions & 12 deletions src/execution/__tests__/executor-test.ts
Expand Up @@ -499,7 +499,7 @@ describe('Execute: Handles basic execution tasks', () => {
};

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

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

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

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

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

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

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

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

expectJSON(
executeSync({ schema, document, operationName: 'Q' }),
).to.deep.equal({
).toDeepEqual({
data: null,
errors: [
{
Expand All @@ -931,7 +931,7 @@ describe('Execute: Handles basic execution tasks', () => {

expectJSON(
executeSync({ schema, document, operationName: 'M' }),
).to.deep.equal({
).toDeepEqual({
data: null,
errors: [
{
Expand All @@ -943,7 +943,7 @@ describe('Execute: Handles basic execution tasks', () => {

expectJSON(
executeSync({ schema, document, operationName: 'S' }),
).to.deep.equal({
).toDeepEqual({
data: null,
errors: [
{
Expand Down Expand Up @@ -1123,7 +1123,7 @@ describe('Execute: Handles basic execution tasks', () => {
};

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

const result = executeSync({ schema, document: parse('{ customScalar }') });
expectJSON(result).to.deep.equal({
expectJSON(result).toDeepEqual({
data: { customScalar: null },
errors: [
{
Expand Down
32 changes: 16 additions & 16 deletions src/execution/__tests__/lists-test.ts
Expand Up @@ -52,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';

expectJSON(complete({ listField })).to.deep.equal({
expectJSON(complete({ listField })).toDeepEqual({
data: { listField: null },
errors: [
{
Expand All @@ -74,14 +74,14 @@ describe('Execute: Handles list nullability', () => {

const result = await executeQuery(listField);
// Promise<Array<T>> === Array<T>
expect(await executeQuery(promisify(listField))).to.deep.equal(result);
expectJSON(await executeQuery(promisify(listField))).toDeepEqual(result);
if (Array.isArray(listField)) {
const listOfPromises = listField.map(promisify);

// Array<Promise<T>> === Array<T>
expect(await executeQuery(listOfPromises)).to.deep.equal(result);
expectJSON(await executeQuery(listOfPromises)).toDeepEqual(result);
// Promise<Array<Promise<T>>> === Array<T>
expect(await executeQuery(promisify(listOfPromises))).to.deep.equal(
expectJSON(await executeQuery(promisify(listOfPromises))).toDeepEqual(
result,
);
}
Expand Down Expand Up @@ -131,11 +131,11 @@ describe('Execute: Handles list nullability', () => {
expect(await complete({ listField, as: '[Int]!' })).to.deep.equal({
data: { listField: [1, null, 2] },
});
expectJSON(await complete({ listField, as: '[Int!]' })).to.deep.equal({
expectJSON(await complete({ listField, as: '[Int!]' })).toDeepEqual({
data: { listField: null },
errors,
});
expectJSON(await complete({ listField, as: '[Int!]!' })).to.deep.equal({
expectJSON(await complete({ listField, as: '[Int!]!' })).toDeepEqual({
data: null,
errors,
});
Expand All @@ -154,14 +154,14 @@ describe('Execute: Handles list nullability', () => {
expect(await complete({ listField, as: '[Int]' })).to.deep.equal({
data: { listField: null },
});
expectJSON(await complete({ listField, as: '[Int]!' })).to.deep.equal({
expectJSON(await complete({ listField, as: '[Int]!' })).toDeepEqual({
data: null,
errors,
});
expect(await complete({ listField, as: '[Int!]' })).to.deep.equal({
data: { listField: null },
});
expectJSON(await complete({ listField, as: '[Int!]!' })).to.deep.equal({
expectJSON(await complete({ listField, as: '[Int!]!' })).toDeepEqual({
data: null,
errors,
});
Expand All @@ -177,19 +177,19 @@ describe('Execute: Handles list nullability', () => {
},
];

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

expectJSON(await complete({ listField, as: '[Int]' })).to.deep.equal({
expectJSON(await complete({ listField, as: '[Int]' })).toDeepEqual({
data: { listField: null },
errors,
});
expectJSON(await complete({ listField, as: '[Int]!' })).to.deep.equal({
expectJSON(await complete({ listField, as: '[Int]!' })).toDeepEqual({
data: null,
errors,
});
expectJSON(await complete({ listField, as: '[Int!]' })).to.deep.equal({
expectJSON(await complete({ listField, as: '[Int!]' })).toDeepEqual({
data: { listField: null },
errors,
});
expectJSON(await complete({ listField, as: '[Int!]!' })).to.deep.equal({
expectJSON(await complete({ listField, as: '[Int!]!' })).toDeepEqual({
data: null,
errors,
});
Expand Down
2 changes: 1 addition & 1 deletion src/execution/__tests__/mutations-test.ts
Expand Up @@ -169,7 +169,7 @@ describe('Execute: Handles mutation execution ordering', () => {
const rootValue = new Root(6);
const result = await execute({ schema, document, rootValue });

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

0 comments on commit 01fa868

Please sign in to comment.