|
| 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 | +}); |
0 commit comments