|
7 | 7 | * of patent rights can be found in the PATENTS file in the same directory.
|
8 | 8 | */
|
9 | 9 |
|
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'; |
12 | 12 | import { formatWarning } from '../assertValidName';
|
13 | 13 |
|
| 14 | +/* eslint-disable no-console */ |
| 15 | + |
14 | 16 | /**
|
15 | 17 | * Helper for dedenting indented template literals. This helps us to
|
16 | 18 | * keep the tests pretty.
|
@@ -41,6 +43,63 @@ function createErrorObject(message, stack) {
|
41 | 43 | return error;
|
42 | 44 | }
|
43 | 45 |
|
| 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 | + |
44 | 103 | describe('formatWarning()', () => {
|
45 | 104 | it('formats given a Chrome-style stack property', () => {
|
46 | 105 | const chromeStack = dedent(`
|
|
0 commit comments