Skip to content

Commit b92fff1

Browse files
committed
tests: Use chai's assert instead of invariant
1 parent dce6528 commit b92fff1

14 files changed

+69
-86
lines changed

src/error/__tests__/GraphQLError-test.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import { expect } from 'chai';
1+
import { assert, expect } from 'chai';
22
import { describe, it } from 'mocha';
33

44
import { dedent } from '../../__testUtils__/dedent';
55

6-
import { invariant } from '../../jsutils/invariant';
7-
86
import { Kind } from '../../language/kinds';
97
import { parse } from '../../language/parser';
108
import { Source } from '../../language/source';
@@ -18,9 +16,9 @@ const source = new Source(dedent`
1816
`);
1917
const ast = parse(source);
2018
const operationNode = ast.definitions[0];
21-
invariant(operationNode.kind === Kind.OPERATION_DEFINITION);
19+
assert(operationNode.kind === Kind.OPERATION_DEFINITION);
2220
const fieldNode = operationNode.selectionSet.selections[0];
23-
invariant(fieldNode != null);
21+
assert(fieldNode != null);
2422

2523
describe('GraphQLError', () => {
2624
it('is a class and is a subclass of Error', () => {
@@ -249,7 +247,7 @@ describe('toString', () => {
249247
),
250248
);
251249
const opA = docA.definitions[0];
252-
invariant(opA.kind === Kind.OBJECT_TYPE_DEFINITION && opA.fields != null);
250+
assert(opA.kind === Kind.OBJECT_TYPE_DEFINITION && opA.fields != null);
253251
const fieldA = opA.fields[0];
254252

255253
const docB = parse(
@@ -263,7 +261,7 @@ describe('toString', () => {
263261
),
264262
);
265263
const opB = docB.definitions[0];
266-
invariant(opB.kind === Kind.OBJECT_TYPE_DEFINITION && opB.fields != null);
264+
assert(opB.kind === Kind.OBJECT_TYPE_DEFINITION && opB.fields != null);
267265
const fieldB = opB.fields[0];
268266

269267
const error = new GraphQLError('Example error with two nodes', {

src/execution/__tests__/executor-test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { expect } from 'chai';
1+
import { assert, expect } from 'chai';
22
import { describe, it } from 'mocha';
33

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

66
import { inspect } from '../../jsutils/inspect';
7-
import { invariant } from '../../jsutils/invariant';
87

98
import { Kind } from '../../language/kinds';
109
import { parse } from '../../language/parser';
@@ -265,7 +264,7 @@ describe('Execute: Handles basic execution tasks', () => {
265264
);
266265

267266
const operation = document.definitions[0];
268-
invariant(operation.kind === Kind.OPERATION_DEFINITION);
267+
assert(operation.kind === Kind.OPERATION_DEFINITION);
269268

270269
expect(resolvedInfo).to.include({
271270
fieldName: 'test',

src/execution/__tests__/simplePubSub.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { invariant } from '../../jsutils/invariant';
1+
import { assert } from 'chai';
22

33
/**
44
* Create an AsyncIterator from an EventEmitter. Useful for mocking a
@@ -64,7 +64,7 @@ export class SimplePubSub<T> {
6464
const value: R = transform(event);
6565
if (pullQueue.length > 0) {
6666
const receiver = pullQueue.shift();
67-
invariant(receiver != null);
67+
assert(receiver != null);
6868
receiver({ value, done: false });
6969
} else {
7070
pushQueue.push(value);

src/execution/__tests__/subscribe-test.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { expect } from 'chai';
1+
import { assert, expect } from 'chai';
22
import { describe, it } from 'mocha';
33

44
import { expectJSON } from '../../__testUtils__/expectJSON';
55
import { resolveOnNextTick } from '../../__testUtils__/resolveOnNextTick';
66

7-
import { invariant } from '../../jsutils/invariant';
87
import { isAsyncIterable } from '../../jsutils/isAsyncIterable';
98

109
import { parse } from '../../language/parser';
@@ -176,7 +175,7 @@ describe('Subscription Initialization Phase', () => {
176175
document: parse('subscription { foo }'),
177176
rootValue: { foo: fooGenerator },
178177
});
179-
invariant(isAsyncIterable(subscription));
178+
assert(isAsyncIterable(subscription));
180179

181180
expect(await subscription.next()).to.deep.equal({
182181
done: false,
@@ -211,7 +210,7 @@ describe('Subscription Initialization Phase', () => {
211210
schema,
212211
document: parse('subscription { foo }'),
213212
});
214-
invariant(isAsyncIterable(subscription));
213+
assert(isAsyncIterable(subscription));
215214

216215
expect(await subscription.next()).to.deep.equal({
217216
done: false,
@@ -249,7 +248,7 @@ describe('Subscription Initialization Phase', () => {
249248
schema,
250249
document: parse('subscription { foo }'),
251250
});
252-
invariant(isAsyncIterable(subscription));
251+
assert(isAsyncIterable(subscription));
253252

254253
expect(await subscription.next()).to.deep.equal({
255254
done: false,
@@ -283,7 +282,7 @@ describe('Subscription Initialization Phase', () => {
283282
rootValue: { customFoo: fooGenerator },
284283
subscribeFieldResolver: (root) => root.customFoo(),
285284
});
286-
invariant(isAsyncIterable(subscription));
285+
assert(isAsyncIterable(subscription));
287286

288287
expect(await subscription.next()).to.deep.equal({
289288
done: false,
@@ -331,7 +330,7 @@ describe('Subscription Initialization Phase', () => {
331330
schema,
332331
document: parse('subscription { foo bar }'),
333332
});
334-
invariant(isAsyncIterable(subscription));
333+
assert(isAsyncIterable(subscription));
335334

336335
expect(didResolveFoo).to.equal(true);
337336
expect(didResolveBar).to.equal(false);
@@ -547,10 +546,10 @@ describe('Subscription Publish Phase', () => {
547546
const pubsub = new SimplePubSub<Email>();
548547

549548
const subscription = await createSubscription(pubsub);
550-
invariant(isAsyncIterable(subscription));
549+
assert(isAsyncIterable(subscription));
551550

552551
const secondSubscription = await createSubscription(pubsub);
553-
invariant(isAsyncIterable(secondSubscription));
552+
assert(isAsyncIterable(secondSubscription));
554553

555554
const payload1 = subscription.next();
556555
const payload2 = secondSubscription.next();
@@ -589,7 +588,7 @@ describe('Subscription Publish Phase', () => {
589588
it('produces a payload per subscription event', async () => {
590589
const pubsub = new SimplePubSub<Email>();
591590
const subscription = await createSubscription(pubsub);
592-
invariant(isAsyncIterable(subscription));
591+
assert(isAsyncIterable(subscription));
593592

594593
// Wait for the next subscription payload.
595594
const payload = subscription.next();
@@ -678,7 +677,7 @@ describe('Subscription Publish Phase', () => {
678677
it('produces a payload when there are multiple events', async () => {
679678
const pubsub = new SimplePubSub<Email>();
680679
const subscription = await createSubscription(pubsub);
681-
invariant(isAsyncIterable(subscription));
680+
assert(isAsyncIterable(subscription));
682681

683682
let payload = subscription.next();
684683

@@ -744,7 +743,7 @@ describe('Subscription Publish Phase', () => {
744743
it('should not trigger when subscription is already done', async () => {
745744
const pubsub = new SimplePubSub<Email>();
746745
const subscription = await createSubscription(pubsub);
747-
invariant(isAsyncIterable(subscription));
746+
assert(isAsyncIterable(subscription));
748747

749748
let payload = subscription.next();
750749

@@ -798,7 +797,7 @@ describe('Subscription Publish Phase', () => {
798797
it('should not trigger when subscription is thrown', async () => {
799798
const pubsub = new SimplePubSub<Email>();
800799
const subscription = await createSubscription(pubsub);
801-
invariant(isAsyncIterable(subscription));
800+
assert(isAsyncIterable(subscription));
802801

803802
let payload = subscription.next();
804803

@@ -851,7 +850,7 @@ describe('Subscription Publish Phase', () => {
851850
it('event order is correct for multiple publishes', async () => {
852851
const pubsub = new SimplePubSub<Email>();
853852
const subscription = await createSubscription(pubsub);
854-
invariant(isAsyncIterable(subscription));
853+
assert(isAsyncIterable(subscription));
855854

856855
let payload = subscription.next();
857856

@@ -942,7 +941,7 @@ describe('Subscription Publish Phase', () => {
942941

943942
const document = parse('subscription { newMessage }');
944943
const subscription = await subscribe({ schema, document });
945-
invariant(isAsyncIterable(subscription));
944+
assert(isAsyncIterable(subscription));
946945

947946
expect(await subscription.next()).to.deep.equal({
948947
done: false,
@@ -1003,7 +1002,7 @@ describe('Subscription Publish Phase', () => {
10031002

10041003
const document = parse('subscription { newMessage }');
10051004
const subscription = await subscribe({ schema, document });
1006-
invariant(isAsyncIterable(subscription));
1005+
assert(isAsyncIterable(subscription));
10071006

10081007
expect(await subscription.next()).to.deep.equal({
10091008
done: false,

src/execution/__tests__/variables-test.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { expect } from 'chai';
1+
import { assert, expect } from 'chai';
22
import { describe, it } from 'mocha';
33

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

66
import { inspect } from '../../jsutils/inspect';
7-
import { invariant } from '../../jsutils/invariant';
87

98
import { Kind } from '../../language/kinds';
109
import { parse } from '../../language/parser';
@@ -1015,9 +1014,9 @@ describe('Execute: Handles inputs', () => {
10151014
`);
10161015

10171016
const operation = doc.definitions[0];
1018-
invariant(operation.kind === Kind.OPERATION_DEFINITION);
1017+
assert(operation.kind === Kind.OPERATION_DEFINITION);
10191018
const { variableDefinitions } = operation;
1020-
invariant(variableDefinitions != null);
1019+
assert(variableDefinitions != null);
10211020

10221021
const inputValue = { input: [0, 1, 2] };
10231022

src/language/__tests__/blockString-fuzz.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1+
import { assert } from 'chai';
12
import { describe, it } from 'mocha';
23

34
import { dedent } from '../../__testUtils__/dedent';
45
import { genFuzzStrings } from '../../__testUtils__/genFuzzStrings';
56
import { inspectStr } from '../../__testUtils__/inspectStr';
67

7-
import { invariant } from '../../jsutils/invariant';
8-
98
import { isPrintableAsBlockString, printBlockString } from '../blockString';
109
import { Lexer } from '../lexer';
1110
import { Source } from '../source';
@@ -14,8 +13,8 @@ function lexValue(str: string): string {
1413
const lexer = new Lexer(new Source(str));
1514
const value = lexer.advance().value;
1615

17-
invariant(typeof value === 'string');
18-
invariant(lexer.advance().kind === '<EOF>', 'Expected EOF');
16+
assert(typeof value === 'string');
17+
assert(lexer.advance().kind === '<EOF>', 'Expected EOF');
1918
return value;
2019
}
2120

@@ -25,7 +24,7 @@ function testPrintableBlockString(
2524
): void {
2625
const blockString = printBlockString(testValue, options);
2726
const printedValue = lexValue(blockString);
28-
invariant(
27+
assert(
2928
testValue === printedValue,
3029
dedent`
3130
Expected lexValue(${inspectStr(blockString)})
@@ -38,7 +37,7 @@ function testPrintableBlockString(
3837
function testNonPrintableBlockString(testValue: string): void {
3938
const blockString = printBlockString(testValue);
4039
const printedValue = lexValue(blockString);
41-
invariant(
40+
assert(
4241
testValue !== printedValue,
4342
dedent`
4443
Expected lexValue(${inspectStr(blockString)})

src/type/__tests__/extensions-test.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import { expect } from 'chai';
1+
import { assert, expect } from 'chai';
22
import { describe, it } from 'mocha';
33

4-
import { invariant } from '../../jsutils/invariant';
5-
64
import {
75
GraphQLEnumType,
86
GraphQLInputObjectType,
@@ -17,7 +15,7 @@ import { GraphQLSchema } from '../schema';
1715
const dummyType = new GraphQLScalarType({ name: 'DummyScalar' });
1816

1917
function expectObjMap(value: unknown) {
20-
invariant(value != null && typeof value === 'object');
18+
assert(value != null && typeof value === 'object');
2119
expect(Object.getPrototypeOf(value)).to.equal(null);
2220
return expect(value);
2321
}
@@ -72,7 +70,7 @@ describe('Type System: Extensions', () => {
7270
expect(config.extensions).to.deep.equal({});
7371
const someFieldConfig = config.fields.someField;
7472
expect(someFieldConfig.extensions).to.deep.equal({});
75-
invariant(someFieldConfig.args != null);
73+
assert(someFieldConfig.args != null);
7674
const someArgConfig = someFieldConfig.args.someArg;
7775
expect(someArgConfig.extensions).to.deep.equal({});
7876
});
@@ -109,7 +107,7 @@ describe('Type System: Extensions', () => {
109107
expectObjMap(config.extensions).to.deep.equal(objectExtensions);
110108
const someFieldConfig = config.fields.someField;
111109
expectObjMap(someFieldConfig.extensions).to.deep.equal(fieldExtensions);
112-
invariant(someFieldConfig.args != null);
110+
assert(someFieldConfig.args != null);
113111
const someArgConfig = someFieldConfig.args.someArg;
114112
expectObjMap(someArgConfig.extensions).to.deep.equal(argExtensions);
115113
});
@@ -141,7 +139,7 @@ describe('Type System: Extensions', () => {
141139
expect(config.extensions).to.deep.equal({});
142140
const someFieldConfig = config.fields.someField;
143141
expect(someFieldConfig.extensions).to.deep.equal({});
144-
invariant(someFieldConfig.args != null);
142+
assert(someFieldConfig.args != null);
145143
const someArgConfig = someFieldConfig.args.someArg;
146144
expect(someArgConfig.extensions).to.deep.equal({});
147145
});
@@ -180,7 +178,7 @@ describe('Type System: Extensions', () => {
180178
expectObjMap(config.extensions).to.deep.equal(interfaceExtensions);
181179
const someFieldConfig = config.fields.someField;
182180
expectObjMap(someFieldConfig.extensions).to.deep.equal(fieldExtensions);
183-
invariant(someFieldConfig.args != null);
181+
assert(someFieldConfig.args != null);
184182
const someArgConfig = someFieldConfig.args.someArg;
185183
expectObjMap(someArgConfig.extensions).to.deep.equal(argExtensions);
186184
});

src/utilities/__tests__/TypeInfo-test.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import { expect } from 'chai';
1+
import { assert, expect } from 'chai';
22
import { describe, it } from 'mocha';
33

4-
import { invariant } from '../../jsutils/invariant';
5-
64
import { parse, parseValue } from '../../language/parser';
75
import { print } from '../../language/printer';
86
import { visit } from '../../language/visitor';
@@ -356,7 +354,7 @@ describe('visitWithTypeInfo', () => {
356354
`);
357355
const ast = parseValue('{ stringListField: ["foo"] }');
358356
const complexInputType = schema.getType('ComplexInput');
359-
invariant(complexInputType != null);
357+
assert(complexInputType != null);
360358

361359
const typeInfo = new TypeInfo(schema, complexInputType);
362360

@@ -401,13 +399,13 @@ describe('visitWithTypeInfo', () => {
401399

402400
it('supports traversals of selection sets', () => {
403401
const humanType = testSchema.getType('Human');
404-
invariant(humanType != null);
402+
assert(humanType != null);
405403

406404
const typeInfo = new TypeInfo(testSchema, humanType);
407405

408406
const ast = parse('{ name, pets { name } }');
409407
const operationNode = ast.definitions[0];
410-
invariant(operationNode.kind === 'OperationDefinition');
408+
assert(operationNode.kind === 'OperationDefinition');
411409

412410
const visited: Array<any> = [];
413411
visit(

src/utilities/__tests__/buildASTSchema-test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { expect } from 'chai';
1+
import { assert, expect } from 'chai';
22
import { describe, it } from 'mocha';
33

44
import { dedent } from '../../__testUtils__/dedent';
55

6-
import { invariant } from '../../jsutils/invariant';
76
import type { Maybe } from '../../jsutils/Maybe';
87

98
import type { ASTNode } from '../../language/ast';
@@ -52,7 +51,7 @@ function cycleSDL(sdl: string): string {
5251
}
5352

5453
function expectASTNode(obj: Maybe<{ readonly astNode: Maybe<ASTNode> }>) {
55-
invariant(obj?.astNode != null);
54+
assert(obj?.astNode != null);
5655
return expect(print(obj.astNode));
5756
}
5857

0 commit comments

Comments
 (0)