Skip to content

Commit

Permalink
Merge pull request #839 from graphql/no-name-warning
Browse files Browse the repository at this point in the history
[RFC] Environment var to silence warning about invalid names.
  • Loading branch information
wincent committed May 1, 2017
2 parents 9033685 + 04e368b commit 7ddd8dd
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -65,6 +65,7 @@
"babel-plugin-transform-regenerator": "6.24.1",
"chai": "3.5.0",
"chai-json-equal": "0.0.1",
"chai-spies": "0.7.1",
"chai-subset": "1.5.0",
"coveralls": "2.13.1",
"eslint": "3.19.0",
Expand Down
3 changes: 2 additions & 1 deletion resources/mocha-bootload.js
Expand Up @@ -8,8 +8,9 @@
*/

var chai = require('chai');
chai.use(require('chai-subset'));
chai.use(require('chai-json-equal'));
chai.use(require('chai-spies'));
chai.use(require('chai-subset'));

process.on('unhandledRejection', function (error) {
console.error('Unhandled Promise Rejection:');
Expand Down
63 changes: 61 additions & 2 deletions src/utilities/__tests__/assertValidName-test.js
Expand Up @@ -7,10 +7,12 @@
* of patent rights can be found in the PATENTS file in the same directory.
*/

import { describe, it } from 'mocha';
import { expect } from 'chai';
import { afterEach, beforeEach, describe, it } from 'mocha';
import { default as chai, expect } from 'chai';
import { formatWarning } from '../assertValidName';

/* eslint-disable no-console */

/**
* Helper for dedenting indented template literals. This helps us to
* keep the tests pretty.
Expand Down Expand Up @@ -41,6 +43,63 @@ function createErrorObject(message, stack) {
return error;
}

describe('assertValidName()', () => {
let assertValidName;
let noNameWarning;
let warn;

beforeEach(() => {
noNameWarning = process.env.GRAPHQL_NO_NAME_WARNING;
delete process.env.GRAPHQL_NO_NAME_WARNING;
warn = console.warn;
console.warn = chai.spy();

// Make sure module-internal state is reset for each test.
delete require.cache[require.resolve('../assertValidName')];
assertValidName = require('../assertValidName').assertValidName;
});

afterEach(() => {
console.warn = warn;
if (noNameWarning === undefined) {
delete process.env.GRAPHQL_NO_NAME_WARNING;
} else {
process.env.GRAPHQL_NO_NAME_WARNING = noNameWarning;
}
});

it('warns against use of leading double underscores', () => {
assertValidName('__bad');
expect(console.warn).to.have.been.called.once();
expect(console.warn.__spy.calls[0][0]).to.match(/must not begin with/);
});

it('warns exactly once even in the presence of multiple violations', () => {
assertValidName('__bad');
assertValidName('__alsoBad');
expect(console.warn).to.have.been.called.once();
});

it('throws for non-strings', () => {
expect(() => assertValidName({})).to.throw(/Must be named/);
});

it('throws for names with invalid characters', () => {
expect(() => assertValidName('>--()-->')).to.throw(/Names must match/);
});

it('does not warn during introspection', () => {
assertValidName('__bad', true);
expect(console.warn).not.to.have.been.called();
});

it('does not warn when GRAPHQL_NO_NAME_WARNING is in effect', () => {
process.env.GRAPHQL_NO_NAME_WARNING = '1';
assertValidName('__bad', true);
expect(console.warn).not.to.have.been.called();
});
});

describe('formatWarning()', () => {
it('formats given a Chrome-style stack property', () => {
const chromeStack = dedent(`
Expand Down
11 changes: 10 additions & 1 deletion src/utilities/assertValidName.js
Expand Up @@ -11,6 +11,10 @@
const NAME_RX = /^[_a-zA-Z][_a-zA-Z0-9]*$/;
const ERROR_PREFIX_RX = /^Error: /;

// Silences warnings if an environment flag is enabled
const noNameWarning =
Boolean(process && process.env && process.env.GRAPHQL_NO_NAME_WARNING);

// Ensures console warnings are only issued once.
let hasWarnedAboutDunder = false;

Expand All @@ -26,7 +30,12 @@ export function assertValidName(
`Must be named. Unexpected name: ${name}.`
);
}
if (!isIntrospection && name.slice(0, 2) === '__' && !hasWarnedAboutDunder) {
if (
!isIntrospection &&
!hasWarnedAboutDunder &&
!noNameWarning &&
name.slice(0, 2) === '__'
) {
hasWarnedAboutDunder = true;
/* eslint-disable no-console */
if (console && console.warn) {
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Expand Up @@ -627,6 +627,10 @@ chai-json-equal@0.0.1:
version "0.0.1"
resolved "https://registry.yarnpkg.com/chai-json-equal/-/chai-json-equal-0.0.1.tgz#338fcbbdaec63349379c7c4278c8a28da3b141a1"

chai-spies@^0.7.1:
version "0.7.1"
resolved "https://registry.yarnpkg.com/chai-spies/-/chai-spies-0.7.1.tgz#343d99f51244212e8b17e64b93996ff7b2c2a9b1"

chai-subset@1.5.0:
version "1.5.0"
resolved "https://registry.yarnpkg.com/chai-subset/-/chai-subset-1.5.0.tgz#d03dbcfa8c9daad848643bbde4e63376b7882427"
Expand Down

0 comments on commit 7ddd8dd

Please sign in to comment.