forked from apollographql/eslint-plugin-graphql
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmakeProcessors.js
125 lines (113 loc) · 3.67 KB
/
makeProcessors.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import assert from 'assert';
import { CLIEngine } from 'eslint';
import { includes, keys } from 'lodash';
import path from 'path';
import schemaJson from './schema.json';
import plugin, { processors } from '../src';
function execute(file) {
const cli = new CLIEngine({
extensions: ['.gql', '.graphql'],
baseConfig: {
rules: {
'graphql/required-fields': [
'error',
{
schemaJson,
env: 'literal',
requiredFields: ['id']
}
]
}
},
ignore: false,
useEslintrc: false,
parserOptions: {
ecmaVersion: 6,
sourceType: 'module'
}
});
cli.addPlugin('eslint-plugin-graphql', plugin);
return cli.executeOnFiles([
path.join(__dirname, '__fixtures__', `${file}.graphql`)
]);
}
describe('processors', () => {
it('should define processors', () => {
const extensions = keys(processors);
assert(includes(extensions, '.gql'));
assert(includes(extensions, '.graphql'));
});
it('should wrap with backticks, escape properly and prepend internalTag', () => {
const query = 'query { search(q: "` \\n ${}") { title } }';
const expected = 'ESLintPluginGraphQLFile`query { search(q: "\\` \\\\n \\${}") { title } }`';
const preprocess = processors['.gql'].preprocess;
const result = preprocess(query);
assert.equal(result, expected);
});
it('should filter only graphql/* rules ', () => {
const messages = [
{ ruleId: 'no-undef' },
{ ruleId: 'semi' },
{ ruleId: 'graphql/randomString' },
{ ruleId: 'graphql/template-strings' }
];
const expected = { ruleId: 'graphql/template-strings' };
const postprocess = processors['.gql'].postprocess;
const result = postprocess(messages);
assert.equal(result.length, 1);
assert.equal(result[0].ruleId, expected.ruleId);
});
describe('graphql/required-fields', () => {
describe('valid', () => {
[
'required-fields-valid-no-id',
'required-fields-valid-id',
'required-fields-valid-array'
].forEach(filename => {
it(`does not warn on file ${filename}`, () => {
const results = execute(filename);
assert.equal(results.errorCount, 0);
});
});
});
describe('invalid', () => {
[
'required-fields-invalid-no-id',
'required-fields-invalid-array'
].forEach(filename => {
it(`warns on file ${filename}`, () => {
const results = execute(filename);
assert.equal(results.errorCount, 1);
const message = results.results[0].messages[0].message;
assert.ok(new RegExp("'id' field required").test(message));
});
});
});
describe('error line/column locations', () => {
it('populates correctly for a single-line document', () => {
const results = execute('required-fields-invalid-array');
assert.equal(results.errorCount, 1);
assert.deepEqual(results.results[0].messages[0], {
column: 9,
line: 1,
message: "'id' field required on 'stories'",
nodeType: 'TaggedTemplateExpression',
ruleId: 'graphql/required-fields',
severity: 2,
});
});
it('populates correctly for a multi-line document', () => {
const results = execute('required-fields-invalid-no-id');
assert.equal(results.errorCount, 1);
assert.deepEqual(results.results[0].messages[0], {
column: 3,
line: 2,
message: "'id' field required on 'greetings'",
nodeType: 'TaggedTemplateExpression',
ruleId: 'graphql/required-fields',
severity: 2,
});
});
});
});
});