Skip to content

Commit

Permalink
Remove 'invariant' calls from 'stripIgnoredCharacters' tests (#2065)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Jul 30, 2019
1 parent 91a4be2 commit ebcc754
Showing 1 changed file with 47 additions and 24 deletions.
71 changes: 47 additions & 24 deletions src/utilities/__tests__/stripIgnoredCharacters-test.js
Expand Up @@ -4,7 +4,6 @@ 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';
Expand Down Expand Up @@ -61,41 +60,51 @@ const nonPunctuatorTokens = [
function lexValue(str) {
const lexer = createLexer(new Source(str));
const value = lexer.advance().value;
invariant(lexer.advance().kind === '<EOF>');

/* istanbul ignore if */
if (lexer.advance().kind !== '<EOF>') {
throw new Error('Expected EOF');
}
return value;
}

// Called only to make error messages for failing tests
/* istanbul ignore next */
function inspectStr(str) {
return (JSON.stringify(str) || '')
.replace(/^"|"$/g, '`')
.replace(/\\"/g, '"');
}

function expectStripped(docString) {
return {
toEqual(expected) {
const stripped = stripIgnoredCharacters(docString);
invariant(
stripped === expected,
`Expected stripIgnoredCharacters(${inspectStr(docString)})\n` +
`\tto equal ${inspectStr(expected)}\n` +
`\tbut got ${inspectStr(stripped)}`,
);

/* istanbul ignore if */
if (stripped !== expected) {
throw new Error(dedent`
Expected stripIgnoredCharacters(${inspectStr(docString)})
to equal ${inspectStr(expected)}
but got ${inspectStr(stripped)}
`);
}

const strippedTwice = stripIgnoredCharacters(stripped);
invariant(
stripped === strippedTwice,
`Expected stripIgnoredCharacters(${inspectStr(stripped)})\n` +
`\tto equal ${inspectStr(stripped)}\n` +
`\tbut got ${inspectStr(strippedTwice)}`,
);

/* istanbul ignore if */
if (stripped !== strippedTwice) {
throw new Error(dedent`
Expected stripIgnoredCharacters(${inspectStr(stripped)})
to equal ${inspectStr(stripped)}
but got ${inspectStr(strippedTwice)}
`);
}
},
toStayTheSame() {
this.toEqual(docString);
},
};

function inspectStr(str) {
// Called only to make error messages for failing tests
/* istanbul ignore next */
return JSON.stringify(str)
.replace(/^"|"$/g, '`')
.replace(/\\"/g, '"');
}
}

describe('stripIgnoredCharacters', () => {
Expand Down Expand Up @@ -405,7 +414,14 @@ describe('stripIgnoredCharacters', () => {
const strippedStr = stripIgnoredCharacters(blockStr);
const strippedValue = lexValue(strippedStr);

invariant(originalValue === strippedValue);
/* istanbul ignore if */
if (originalValue !== strippedValue) {
throw new Error(dedent`
Expected lextOne(stripIgnoredCharacters(${inspectStr(blockStr)}))
to equal ${inspectStr(originalValue)}
but got ${inspectStr(strippedValue)}
`);
}
return expectStripped(blockStr);
}

Expand Down Expand Up @@ -460,7 +476,14 @@ describe('stripIgnoredCharacters', () => {
const strippedStr = stripIgnoredCharacters(testStr);
const strippedValue = lexValue(strippedStr);

invariant(testValue === strippedValue);
/* istanbul ignore if */
if (testValue !== strippedValue) {
throw new Error(dedent`
Expected lextOne(stripIgnoredCharacters(${inspectStr(testStr)}))
to equal ${inspectStr(testValue)}
but got ${inspectStr(strippedValue)}
`);
}
}
}
});
Expand Down

0 comments on commit ebcc754

Please sign in to comment.