Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 40 additions & 26 deletions src/error/__tests__/GraphQLError-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ describe('GraphQLError', () => {

it('has a name, message, and stack trace', () => {
const e = new GraphQLError('msg');
expect(e.name).to.equal('GraphQLError');

expect(e).to.include({ name: 'GraphQLError', message: 'msg' });
expect(e.stack).to.be.a('string');
expect(e.message).to.equal('msg');
});

it('uses the stack of an original error', () => {
Expand All @@ -48,51 +48,65 @@ describe('GraphQLError', () => {
undefined,
original,
);
expect(e.name).to.equal('GraphQLError');
expect(e.stack).to.equal(original.stack);
expect(e.message).to.equal('msg');
expect(e.originalError).to.equal(original);

expect(e).to.include({
name: 'GraphQLError',
message: 'msg',
stack: original.stack,
originalError: original,
});
});

it('creates new stack if original error has no stack', () => {
const original = new Error('original');
const e = new GraphQLError('msg', null, null, null, null, original);
expect(e.name).to.equal('GraphQLError');

expect(e).to.include({
name: 'GraphQLError',
message: 'msg',
originalError: original,
});
expect(e.stack).to.be.a('string');
expect(e.message).to.equal('msg');
expect(e.originalError).to.equal(original);
});

it('converts nodes to positions and locations', () => {
const e = new GraphQLError('msg', [fieldNode]);
expect(e.nodes).to.deep.equal([fieldNode]);
expect(e.source).to.equal(source);
expect(e.positions).to.deep.equal([4]);
expect(e.locations).to.deep.equal([{ line: 2, column: 3 }]);
expect(e).to.have.property('source', source);
expect(e).to.deep.include({
nodes: [fieldNode],
positions: [4],
locations: [{ line: 2, column: 3 }],
});
});

it('converts single node to positions and locations', () => {
const e = new GraphQLError('msg', fieldNode); // Non-array value.
expect(e.nodes).to.deep.equal([fieldNode]);
expect(e.source).to.equal(source);
expect(e.positions).to.deep.equal([4]);
expect(e.locations).to.deep.equal([{ line: 2, column: 3 }]);
expect(e).to.have.property('source', source);
expect(e).to.deep.include({
nodes: [fieldNode],
positions: [4],
locations: [{ line: 2, column: 3 }],
});
});

it('converts node with loc.start === 0 to positions and locations', () => {
const e = new GraphQLError('msg', [operationNode]);
expect(e.nodes).to.deep.equal([operationNode]);
expect(e.source).to.equal(source);
expect(e.positions).to.deep.equal([0]);
expect(e.locations).to.deep.equal([{ line: 1, column: 1 }]);
expect(e).to.have.property('source', source);
expect(e).to.deep.include({
nodes: [operationNode],
positions: [0],
locations: [{ line: 1, column: 1 }],
});
});

it('converts source and positions to locations', () => {
const e = new GraphQLError('msg', null, source, [6]);
expect(e.nodes).to.equal(undefined);
expect(e.source).to.equal(source);
expect(e.positions).to.deep.equal([6]);
expect(e.locations).to.deep.equal([{ line: 2, column: 5 }]);
expect(e).to.have.property('source', source);
expect(e).to.deep.include({
nodes: undefined,
positions: [6],
locations: [{ line: 2, column: 5 }],
});
});

it('serializes to include message', () => {
Expand All @@ -114,7 +128,7 @@ describe('GraphQLError', () => {
'to',
'field',
]);
expect(e.path).to.deep.equal(['path', 3, 'to', 'field']);
expect(e).to.have.deep.property('path', ['path', 3, 'to', 'field']);
expect(JSON.stringify(e)).to.equal(
'{"message":"msg","path":["path",3,"to","field"]}',
);
Expand Down
15 changes: 13 additions & 2 deletions src/execution/__tests__/abstract-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @noflow
* @flow strict
*/

import { expect } from 'chai';
Expand All @@ -21,20 +21,28 @@ import {
} from '../../';

class Dog {
name: string;
woofs: boolean;

constructor(name, woofs) {
this.name = name;
this.woofs = woofs;
}
}

class Cat {
name: string;
meows: boolean;

constructor(name, meows) {
this.name = name;
this.meows = meows;
}
}

class Human {
name: string;

constructor(name) {
this.name = name;
}
Expand Down Expand Up @@ -383,7 +391,10 @@ describe('Execute: Handles execution of abstract types', () => {
const fooInterface = new GraphQLInterfaceType({
name: 'FooInterface',
fields: { bar: { type: GraphQLString } },
resolveType: () => [],
resolveType() {
// $DisableFlowOnNegativeTest
return [];
},
});

const fooObject = new GraphQLObjectType({
Expand Down
36 changes: 15 additions & 21 deletions src/language/__tests__/lexer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @noflow
* @flow strict
*/

import { inspect as utilInspect } from 'util';

import { expect } from 'chai';
import { describe, it } from 'mocha';
import dedent from '../../jsutils/dedent';
import { GraphQLError } from '../../error';
import { Source } from '../source';
import { createLexer, TokenKind } from '../lexer';

Expand All @@ -21,20 +22,16 @@ function lexOne(str) {
}

function expectSyntaxError(text, message, location) {
try {
lexOne(text);
expect.fail('Expected to throw syntax error');
} catch (error) {
expect(error.message).to.contain(message);
expect(error.locations).to.deep.equal([location]);
}
expect(() => lexOne(text))
.to.throw('Syntax Error: ' + message)
.with.deep.property('locations', [location]);
}

describe('Lexer', () => {
it('disallows uncommon control characters', () => {
expectSyntaxError(
'\u0007',
'Cannot contain the invalid character "\\u0007"',
'Cannot contain the invalid character "\\u0007".',
{ line: 1, column: 1 },
);
});
Expand Down Expand Up @@ -236,12 +233,12 @@ describe('Lexer', () => {
{ line: 1, column: 19 },
);

expectSyntaxError('"multi\nline"', 'Unterminated string', {
expectSyntaxError('"multi\nline"', 'Unterminated string.', {
line: 1,
column: 7,
});

expectSyntaxError('"multi\rline"', 'Unterminated string', {
expectSyntaxError('"multi\rline"', 'Unterminated string.', {
line: 1,
column: 7,
});
Expand Down Expand Up @@ -672,16 +669,13 @@ describe('Lexer', () => {
end: 1,
value: 'a',
});
let caughtError;
try {
lexer.advance();
} catch (error) {
caughtError = error;
}
expect(caughtError.message).to.equal(
'Syntax Error: Invalid number, expected digit but got: "b".',
);
expect(caughtError.locations).to.deep.equal([{ line: 1, column: 3 }]);

expect(() => lexer.advance())
.throw(GraphQLError)
.that.deep.include({
message: 'Syntax Error: Invalid number, expected digit but got: "b".',
locations: [{ line: 1, column: 3 }],
});
});

it('produces double linked list of tokens, including comments', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/language/__tests__/schema-printer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @noflow
* @flow strict
*/

import { expect } from 'chai';
Expand Down
3 changes: 1 addition & 2 deletions src/type/__tests__/definition-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,8 +416,7 @@ describe('Type System: Example', () => {
});

const types = union.getTypes();
expect(types.length).to.equal(1);
expect(types[0]).to.equal(ObjectType);
expect(types).to.have.members([ObjectType]);
});

it('does not mutate passed field definitions', () => {
Expand Down
29 changes: 21 additions & 8 deletions src/type/__tests__/enumType-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @noflow
* @flow
*/

import { describe, it } from 'mocha';
Expand Down Expand Up @@ -350,18 +350,31 @@ describe('Type System: Enum Values', () => {

it('presents a getValues() API for complex enums', () => {
const values = ComplexEnum.getValues();
expect(values.length).to.equal(2);
expect(values[0].name).to.equal('ONE');
expect(values[0].value).to.equal(Complex1);
expect(values[1].name).to.equal('TWO');
expect(values[1].value).to.equal(Complex2);
expect(values).to.have.deep.ordered.members([
{
name: 'ONE',
value: Complex1,
description: undefined,
isDeprecated: false,
deprecationReason: undefined,
astNode: undefined,
},
{
name: 'TWO',
value: Complex2,
description: undefined,
isDeprecated: false,
deprecationReason: undefined,
astNode: undefined,
},
]);
});

it('presents a getValue() API for complex enums', () => {
const oneValue = ComplexEnum.getValue('ONE');
expect(oneValue.name).to.equal('ONE');
expect(oneValue.value).to.equal(Complex1);
expect(oneValue).to.include({ name: 'ONE', value: Complex1 });

// $DisableFlowOnNegativeTest
const badUsage = ComplexEnum.getValue(Complex1);
expect(badUsage).to.equal(undefined);
});
Expand Down
1 change: 0 additions & 1 deletion src/type/__tests__/introspection-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,6 @@ describe('Introspection', () => {
field: {
type: GraphQLString,
args: { complex: { type: TestInputObject } },
resolve: (_, { complex }) => JSON.stringify(complex),
},
},
});
Expand Down
Loading