Skip to content

Commit 7ddd8dd

Browse files
authored
Merge pull request #839 from graphql/no-name-warning
[RFC] Environment var to silence warning about invalid names.
2 parents 9033685 + 04e368b commit 7ddd8dd

File tree

5 files changed

+78
-4
lines changed

5 files changed

+78
-4
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"babel-plugin-transform-regenerator": "6.24.1",
6666
"chai": "3.5.0",
6767
"chai-json-equal": "0.0.1",
68+
"chai-spies": "0.7.1",
6869
"chai-subset": "1.5.0",
6970
"coveralls": "2.13.1",
7071
"eslint": "3.19.0",

resources/mocha-bootload.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
*/
99

1010
var chai = require('chai');
11-
chai.use(require('chai-subset'));
1211
chai.use(require('chai-json-equal'));
12+
chai.use(require('chai-spies'));
13+
chai.use(require('chai-subset'));
1314

1415
process.on('unhandledRejection', function (error) {
1516
console.error('Unhandled Promise Rejection:');

src/utilities/__tests__/assertValidName-test.js

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
* of patent rights can be found in the PATENTS file in the same directory.
88
*/
99

10-
import { describe, it } from 'mocha';
11-
import { expect } from 'chai';
10+
import { afterEach, beforeEach, describe, it } from 'mocha';
11+
import { default as chai, expect } from 'chai';
1212
import { formatWarning } from '../assertValidName';
1313

14+
/* eslint-disable no-console */
15+
1416
/**
1517
* Helper for dedenting indented template literals. This helps us to
1618
* keep the tests pretty.
@@ -41,6 +43,63 @@ function createErrorObject(message, stack) {
4143
return error;
4244
}
4345

46+
describe('assertValidName()', () => {
47+
let assertValidName;
48+
let noNameWarning;
49+
let warn;
50+
51+
beforeEach(() => {
52+
noNameWarning = process.env.GRAPHQL_NO_NAME_WARNING;
53+
delete process.env.GRAPHQL_NO_NAME_WARNING;
54+
warn = console.warn;
55+
console.warn = chai.spy();
56+
57+
// Make sure module-internal state is reset for each test.
58+
delete require.cache[require.resolve('../assertValidName')];
59+
assertValidName = require('../assertValidName').assertValidName;
60+
});
61+
62+
afterEach(() => {
63+
console.warn = warn;
64+
if (noNameWarning === undefined) {
65+
delete process.env.GRAPHQL_NO_NAME_WARNING;
66+
} else {
67+
process.env.GRAPHQL_NO_NAME_WARNING = noNameWarning;
68+
}
69+
});
70+
71+
it('warns against use of leading double underscores', () => {
72+
assertValidName('__bad');
73+
expect(console.warn).to.have.been.called.once();
74+
expect(console.warn.__spy.calls[0][0]).to.match(/must not begin with/);
75+
});
76+
77+
it('warns exactly once even in the presence of multiple violations', () => {
78+
assertValidName('__bad');
79+
assertValidName('__alsoBad');
80+
expect(console.warn).to.have.been.called.once();
81+
});
82+
83+
it('throws for non-strings', () => {
84+
expect(() => assertValidName({})).to.throw(/Must be named/);
85+
});
86+
87+
it('throws for names with invalid characters', () => {
88+
expect(() => assertValidName('>--()-->')).to.throw(/Names must match/);
89+
});
90+
91+
it('does not warn during introspection', () => {
92+
assertValidName('__bad', true);
93+
expect(console.warn).not.to.have.been.called();
94+
});
95+
96+
it('does not warn when GRAPHQL_NO_NAME_WARNING is in effect', () => {
97+
process.env.GRAPHQL_NO_NAME_WARNING = '1';
98+
assertValidName('__bad', true);
99+
expect(console.warn).not.to.have.been.called();
100+
});
101+
});
102+
44103
describe('formatWarning()', () => {
45104
it('formats given a Chrome-style stack property', () => {
46105
const chromeStack = dedent(`

src/utilities/assertValidName.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
const NAME_RX = /^[_a-zA-Z][_a-zA-Z0-9]*$/;
1212
const ERROR_PREFIX_RX = /^Error: /;
1313

14+
// Silences warnings if an environment flag is enabled
15+
const noNameWarning =
16+
Boolean(process && process.env && process.env.GRAPHQL_NO_NAME_WARNING);
17+
1418
// Ensures console warnings are only issued once.
1519
let hasWarnedAboutDunder = false;
1620

@@ -26,7 +30,12 @@ export function assertValidName(
2630
`Must be named. Unexpected name: ${name}.`
2731
);
2832
}
29-
if (!isIntrospection && name.slice(0, 2) === '__' && !hasWarnedAboutDunder) {
33+
if (
34+
!isIntrospection &&
35+
!hasWarnedAboutDunder &&
36+
!noNameWarning &&
37+
name.slice(0, 2) === '__'
38+
) {
3039
hasWarnedAboutDunder = true;
3140
/* eslint-disable no-console */
3241
if (console && console.warn) {

yarn.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,10 @@ [email protected]:
627627
version "0.0.1"
628628
resolved "https://registry.yarnpkg.com/chai-json-equal/-/chai-json-equal-0.0.1.tgz#338fcbbdaec63349379c7c4278c8a28da3b141a1"
629629

630+
chai-spies@^0.7.1:
631+
version "0.7.1"
632+
resolved "https://registry.yarnpkg.com/chai-spies/-/chai-spies-0.7.1.tgz#343d99f51244212e8b17e64b93996ff7b2c2a9b1"
633+
630634
631635
version "1.5.0"
632636
resolved "https://registry.yarnpkg.com/chai-subset/-/chai-subset-1.5.0.tgz#d03dbcfa8c9daad848643bbde4e63376b7882427"

0 commit comments

Comments
 (0)