|
| 1 | +import * as fs from 'node:fs'; |
| 2 | +import * as path from 'node:path'; |
| 3 | +import { describe, expect, it } from '@jest/globals'; |
| 4 | +import { Spectral, Document } from '@stoplight/spectral-core'; |
| 5 | +import { httpAndFileResolver } from '@stoplight/spectral-ref-resolver'; |
| 6 | +import { bundleAndLoadRuleset } from '@stoplight/spectral-ruleset-bundler/with-loader'; |
| 7 | + |
| 8 | +const rulesetPath = path.join(__dirname, '../..', 'ipa-spectral.yaml'); |
| 9 | + |
| 10 | +export default (ruleName, tests) => { |
| 11 | + describe(`Rule ${ruleName}`, () => { |
| 12 | + for (const testCase of tests) { |
| 13 | + it.concurrent(testCase.name, async () => { |
| 14 | + const s = await createSpectral(); |
| 15 | + const doc = testCase.document instanceof Document ? testCase.document : JSON.stringify(testCase.document); |
| 16 | + const allErrors = await s.run(doc); |
| 17 | + |
| 18 | + const errors = allErrors.filter((e) => { |
| 19 | + if (testCase.errors[0]) { |
| 20 | + return e.code === testCase.errors[0].code; |
| 21 | + } |
| 22 | + return false; |
| 23 | + }); |
| 24 | + |
| 25 | + expect(errors.length).toEqual(testCase.errors.length); |
| 26 | + |
| 27 | + errors.forEach((error, index) => { |
| 28 | + expect(error.code).toEqual(testCase.errors[index].code); |
| 29 | + expect(error.message).toEqual(testCase.errors[index].message); |
| 30 | + expect(error.path).toEqual(testCase.errors[index].path); |
| 31 | + }); |
| 32 | + }); |
| 33 | + } |
| 34 | + }); |
| 35 | +}; |
| 36 | + |
| 37 | +async function createSpectral() { |
| 38 | + const s = new Spectral({ resolver: httpAndFileResolver }); |
| 39 | + |
| 40 | + s.setRuleset(await bundleAndLoadRuleset(rulesetPath, { fs, fetch })); |
| 41 | + |
| 42 | + return s; |
| 43 | +} |
0 commit comments