Skip to content

Commit 798bbe1

Browse files
committed
test(eslint-config-graphql): introduce unit test suit
1 parent 8cfac6b commit 798bbe1

6 files changed

Lines changed: 144 additions & 1 deletion

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
type Query {
2+
user: UnknownType
3+
}
4+
5+
type User {
6+
id: ID
7+
name: String
8+
profile: NonExistentProfile
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
type Query {
2+
user: User
3+
}
4+
5+
type User {
6+
id: ID
7+
name: String
8+
email: String
9+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import assert from 'node:assert/strict';
2+
import fs from 'node:fs';
3+
import { beforeEach, describe, it } from 'node:test';
4+
5+
import { ESLint } from 'eslint';
6+
7+
import config from '../index.js';
8+
9+
describe('@alma-oss/eslint-config-graphql', () => {
10+
describe('config structure', () => {
11+
it('exports a flat config array', () => {
12+
assert.ok(Array.isArray(config));
13+
});
14+
15+
it('exports exactly 2 config entries', () => {
16+
assert.equal(config.length, 2);
17+
});
18+
19+
it('first entry has correct name', () => {
20+
assert.equal(config[0].name, '@alma-oss/eslint-config-graphql');
21+
});
22+
23+
it('first entry registers the graphql-eslint processor', () => {
24+
assert.ok(config[0].processor != null);
25+
});
26+
27+
it('second entry targets *.graphql files', () => {
28+
assert.deepEqual(config[1].files, ['*.graphql']);
29+
});
30+
31+
it('second entry registers the @graphql-eslint plugin', () => {
32+
assert.ok(config[1].plugins['@graphql-eslint'] != null);
33+
});
34+
35+
it('second entry enables known-type-names as error', () => {
36+
assert.equal(config[1].rules['@graphql-eslint/known-type-names'], 'error');
37+
});
38+
});
39+
40+
describe('valid GraphQL schema', () => {
41+
const code = fs.readFileSync('./__tests__/__fixtures__/schema-valid.graphql', 'utf-8');
42+
let result;
43+
44+
beforeEach(async () => {
45+
const overrideConfig = [
46+
config[0],
47+
{
48+
...config[1],
49+
languageOptions: {
50+
...config[1].languageOptions,
51+
parserOptions: {
52+
schemaSdl: code,
53+
},
54+
},
55+
},
56+
];
57+
58+
const eslint = new ESLint({
59+
overrideConfigFile: true,
60+
overrideConfig,
61+
});
62+
const [lintResult] = await eslint.lintText(code, { filePath: 'schema-valid.graphql' });
63+
result = lintResult;
64+
});
65+
66+
it('flags no warnings', () => {
67+
assert.equal(result.messages.length, 0);
68+
});
69+
});
70+
71+
describe('invalid GraphQL schema (unknown type names)', () => {
72+
const validCode = fs.readFileSync('./__tests__/__fixtures__/schema-valid.graphql', 'utf-8');
73+
const invalidCode = fs.readFileSync('./__tests__/__fixtures__/schema-invalid.graphql', 'utf-8');
74+
let result;
75+
76+
beforeEach(async () => {
77+
const overrideConfig = [
78+
config[0],
79+
{
80+
...config[1],
81+
languageOptions: {
82+
...config[1].languageOptions,
83+
parserOptions: {
84+
// Use valid schema as the universe of known types
85+
schemaSdl: validCode,
86+
},
87+
},
88+
},
89+
];
90+
91+
const eslint = new ESLint({
92+
overrideConfigFile: true,
93+
overrideConfig,
94+
});
95+
// Lint the invalid schema against the valid schema's type universe
96+
const [lintResult] = await eslint.lintText(invalidCode, { filePath: 'schema-invalid.graphql' });
97+
result = lintResult;
98+
});
99+
100+
it('flags warnings', () => {
101+
assert.ok(result.messages.length > 0);
102+
});
103+
104+
it('flags the known-type-names rule', () => {
105+
assert.ok(result.messages.some((m) => m.ruleId === '@graphql-eslint/known-type-names'));
106+
});
107+
108+
it('reports error severity', () => {
109+
const errors = result.messages.filter((m) => m.ruleId === '@graphql-eslint/known-type-names');
110+
assert.ok(errors.every((m) => m.severity === 2));
111+
});
112+
113+
it('flags exactly 2 unknown type references', () => {
114+
const errors = result.messages.filter((m) => m.ruleId === '@graphql-eslint/known-type-names');
115+
assert.equal(errors.length, 2);
116+
});
117+
});
118+
});

packages/eslint-config-graphql/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import graphqlPlugin from '@graphql-eslint/eslint-plugin';
55
export default [
66
{
77
name: '@alma-oss/eslint-config-graphql',
8-
files: [...globs.configs, ...globs, globs.typescripts],
8+
files: [...globs.configs, ...globs.javascripts, ...globs.typescripts],
99
processor: graphqlPlugin.processor,
1010
},
1111
{

packages/eslint-config-graphql/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
"publishConfig": {
2727
"access": "public"
2828
},
29+
"scripts": {
30+
"test:unit": "node --test"
31+
},
2932
"exports": {
3033
".": "./index.js"
3134
},
@@ -36,5 +39,8 @@
3639
},
3740
"peerDependencies": {
3841
"eslint": "^9"
42+
},
43+
"devDependencies": {
44+
"eslint": "9.39.4"
3945
}
4046
}

yarn.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ __metadata:
3636
dependencies:
3737
"@alma-oss/eslint-config-base": "npm:^4.0.0-alpha.2"
3838
"@graphql-eslint/eslint-plugin": "npm:^4.0.0"
39+
eslint: "npm:9.39.4"
3940
graphql: "npm:^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0"
4041
peerDependencies:
4142
eslint: ^9

0 commit comments

Comments
 (0)