diff --git a/resources/inline-invariant.js b/resources/inline-invariant.js index 90ce1ff469..682d24e276 100644 --- a/resources/inline-invariant.js +++ b/resources/inline-invariant.js @@ -60,6 +60,7 @@ module.exports = function inlineInvariant(context) { const previousStatement = parentStatement.container[parentStatement.key - 1]; if ( + previousStatement != null && previousStatement.type === 'IfStatement' && previousStatement.alternate == null ) { diff --git a/src/jsutils/__tests__/inspect-test.js b/src/jsutils/__tests__/inspect-test.js index 66b0609b58..252afeb78e 100644 --- a/src/jsutils/__tests__/inspect-test.js +++ b/src/jsutils/__tests__/inspect-test.js @@ -4,6 +4,7 @@ import { expect } from 'chai'; import { describe, it } from 'mocha'; import inspect from '../inspect'; +import invariant from '../invariant'; import nodejsCustomInspectSymbol from '../nodejsCustomInspectSymbol'; describe('inspect', () => { @@ -35,10 +36,11 @@ describe('inspect', () => { }); it('function', () => { - expect(inspect(/* istanbul ignore next */ () => 0)).to.equal('[function]'); + expect(inspect(() => invariant(false))).to.equal('[function]'); - /* istanbul ignore next */ - function testFunc() {} + function testFunc() { + invariant(false); + } expect(inspect(testFunc)).to.equal('[function testFunc]'); }); @@ -102,7 +104,7 @@ describe('inspect', () => { it('custom symbol inspect is take precedence', () => { const object = { inspect() { - return ''; + invariant(false); }, [String(nodejsCustomInspectSymbol)]() { return ''; diff --git a/src/jsutils/devAssert.js b/src/jsutils/devAssert.js index 813ea7ad55..28554eb627 100644 --- a/src/jsutils/devAssert.js +++ b/src/jsutils/devAssert.js @@ -1,9 +1,7 @@ // @flow strict -/* istanbul ignore file */ export default function devAssert(condition: mixed, message: string): void { const booleanCondition = Boolean(condition); - /* istanbul ignore else */ if (!booleanCondition) { throw new Error(message); } diff --git a/src/jsutils/invariant.js b/src/jsutils/invariant.js index 31aacd5a6a..d82cd87a19 100644 --- a/src/jsutils/invariant.js +++ b/src/jsutils/invariant.js @@ -1,6 +1,5 @@ // @flow strict -/* istanbul ignore file */ export default function invariant(condition: mixed, message?: string): void { const booleanCondition = Boolean(condition); if (!booleanCondition) { diff --git a/src/type/__tests__/introspection-test.js b/src/type/__tests__/introspection-test.js index 592d6efdca..fd5473a65d 100644 --- a/src/type/__tests__/introspection-test.js +++ b/src/type/__tests__/introspection-test.js @@ -3,6 +3,8 @@ import { expect } from 'chai'; import { describe, it } from 'mocha'; +import invariant from '../../jsutils/invariant'; + import { missingFieldArgMessage } from '../../validation/rules/ProvidedRequiredArguments'; import { graphqlSync } from '../../graphql'; @@ -1400,14 +1402,10 @@ describe('Introspection', () => { const schema = new GraphQLSchema({ query: QueryRoot }); const source = getIntrospectionQuery(); - const calledForFields = {}; - /* istanbul ignore next */ - function fieldResolver(value, _1, _2, info) { - calledForFields[`${info.parentType.name}::${info.fieldName}`] = true; - return value; + function fieldResolver(_1, _2, _3, info) { + invariant(false, `Called on ${info.parentType.name}::${info.fieldName}`); } graphqlSync({ schema, source, fieldResolver }); - expect(calledForFields).to.deep.equal({}); }); }); diff --git a/src/utilities/__tests__/stripIgnoredCharacters-test.js b/src/utilities/__tests__/stripIgnoredCharacters-test.js index 7b96a2d7cd..95093d993d 100644 --- a/src/utilities/__tests__/stripIgnoredCharacters-test.js +++ b/src/utilities/__tests__/stripIgnoredCharacters-test.js @@ -4,6 +4,7 @@ import { expect } from 'chai'; import { describe, it } from 'mocha'; import dedent from '../../jsutils/dedent'; +import invariant from '../../jsutils/invariant'; import { parse } from '../../language/parser'; import { Source } from '../../language/source'; @@ -61,10 +62,7 @@ function lexValue(str) { const lexer = createLexer(new Source(str)); const value = lexer.advance().value; - /* istanbul ignore if */ - if (lexer.advance().kind !== '') { - throw new Error('Expected EOF'); - } + invariant(lexer.advance().kind === '', 'Expected EOF'); return value; } @@ -81,25 +79,25 @@ function expectStripped(docString) { toEqual(expected) { const stripped = stripIgnoredCharacters(docString); - /* istanbul ignore if */ - if (stripped !== expected) { - throw new Error(dedent` + invariant( + stripped === expected, + dedent` Expected stripIgnoredCharacters(${inspectStr(docString)}) to equal ${inspectStr(expected)} but got ${inspectStr(stripped)} - `); - } + `, + ); const strippedTwice = stripIgnoredCharacters(stripped); - /* istanbul ignore if */ - if (stripped !== strippedTwice) { - throw new Error(dedent` + invariant( + stripped === strippedTwice, + dedent` Expected stripIgnoredCharacters(${inspectStr(stripped)}) to equal ${inspectStr(stripped)} but got ${inspectStr(strippedTwice)} - `); - } + `, + ); }, toStayTheSame() { this.toEqual(docString); @@ -414,14 +412,14 @@ describe('stripIgnoredCharacters', () => { const strippedStr = stripIgnoredCharacters(blockStr); const strippedValue = lexValue(strippedStr); - /* istanbul ignore if */ - if (originalValue !== strippedValue) { - throw new Error(dedent` - Expected lextOne(stripIgnoredCharacters(${inspectStr(blockStr)})) - to equal ${inspectStr(originalValue)} - but got ${inspectStr(strippedValue)} - `); - } + invariant( + originalValue === strippedValue, + dedent` + Expected lextOne(stripIgnoredCharacters(${inspectStr(blockStr)})) + to equal ${inspectStr(originalValue)} + but got ${inspectStr(strippedValue)} + `, + ); return expectStripped(blockStr); } @@ -476,14 +474,14 @@ describe('stripIgnoredCharacters', () => { const strippedStr = stripIgnoredCharacters(testStr); const strippedValue = lexValue(strippedStr); - /* istanbul ignore if */ - if (testValue !== strippedValue) { - throw new Error(dedent` + invariant( + testValue === strippedValue, + dedent` Expected lextOne(stripIgnoredCharacters(${inspectStr(testStr)})) to equal ${inspectStr(testValue)} but got ${inspectStr(strippedValue)} - `); - } + `, + ); } } });