Replies: 3 comments 2 replies
-
Hey! Idea 1Assuming you have Node.js installed, you could copy over our rule tester, which we designed to be somewhat similar to the ESLint's rule tester. https://github.com/stoplightio/spectral/blob/develop/packages/rulesets/src/__tests__/__helpers__/tester.ts#L6#L9 should be changed to reflect the location of your actual ruleset, i.e. import myRuleset from './myRuleset';
type Ruleset = typeof myRuleset; The caveat here is that the Alright, now that we have the basic setup, let's evaluate the actual tests. {
name: 'a directly self-referencing document from the filesystem', // some pretty name that'll displayed by the test runner
document: {}, // the fixture you want to use as an input,
errors: [], // no errors = empty array
},
{
name: 'an indirectly self-referencing document from the filesystem',
document:{},
errors: [
{ // this is a partial of the ISpectralDiagnostic
message: 'Potentially unused component has been detected.',
path: ['components', 'schemas', 'Unhooked'],
severity: DiagnosticSeverity.Warning,
},
],
},
];
Alright, now once we have some tests in place, let's pick some test-runner. Here's how the example project could look like Idea 2Copy test-harness. Idea 3If you are unable to install Node.js, then creating an own script that spawns spectral binary can be taken into the consideration.
And then you could write a script that iterates over the directory, and runs Spectral.
and then you'd need to use something to compare the output against $input-expected-output
import myRuleset from '../ruleset';
export default {
extends: [[myRuleset, 'off']],
rules: {
'my-rule': true,
}
}; LMK if that makes sense and if there's anything I could help you with. |
Beta Was this translation helpful? Give feedback.
-
Sorry for bumping an old thread with a bit of self-promotion, but I've written about how I did it myself on my blog and to make things a little easier have created a test harness package off the back of it for others to use - would be happy to hear any feedback you have! A sample repo of this in use is https://gitlab.com/jamietanna/spectral-jest and https://github.com/co-cddo/api-standards-linting |
Beta Was this translation helpful? Give feedback.
-
Also coming back to this to say that APIs You Won't Hate have built a much nicer alternative to mine from the other comment, which can be found documented here |
Beta Was this translation helpful? Give feedback.
-
Hi all,
We just started working with spectral and started on writing our own OpenAPI specification rules. So far things are going fine, but some of the rules are hard to define and need quite a few manual tests to verify. Given that we want to share these rules among a large number of teams we want to be sure that our rules work as expected, and keep working as expected.
Is there any way of setting up automatic tests that test your rules? For instance run against a spec and check that a rule is/isn't triggered and do this for a number of specs.
If there isn't are there any plans for providing such a feature?
Beta Was this translation helpful? Give feedback.
All reactions