Closed Xotabu4 closed 3 years ago
Thanks for this suggestion @Xotabu4. There may already be a workaround to do what you want, but let's check if it works. Even if it does, this enhancement may be nice as syntactic sugar.
I think currently expect(res).to.satisfyApiSpec
validates res
against the OpenAPI spec that was most recently loaded into the particular instance of chai. So although you can't currently use multiple OpenAPI specs at the same time for an instance of chai, you may be able to switch to different OpenAPI specs even in the same it
block:
const axios = require('axios');
describe('Some feature', () => {
it('complex flow', async () => {
const { data: loginOpenApiSpec } = await axios.get('http://loginservice:3000/api-json');
chai.use(chaiResponseValidator(loginOpenApiSpec));
const loginres = await axios.get('http://loginservice:3000/user/auth');
expect(loginres).to.satisfyApiSpec;
// request to another service, that also has own doc
const { data: searchOpenApiSpec } = await axios.get('http://searchservice:3000/api-json');
chai.use(chaiResponseValidator(searchOpenApiSpec));
const searchres = await axios.get('http://searchservice:3000/search/', {headers: loginres.data.token});
expect(searchres).to.satisfyApiSpec;
});
Does this work?
Even if it does, I agree it's a bit verbose and distracting.
If you don't need multiple OpenAPI specs in the same it
block, you can separate them in different describe
blocks (see how our test file uses many OpenAPI specs, each in a different describe
block)
But if you do need multiple OpenAPI specs in the same it
block, this enhancement may be nice as syntactic sugar
Closing as there hasn't been an update for a while, but feel free to reopen or upvote this issue if anyone would like action on it 😄
In my current tests i need to work with multiple servers with own docs in one test:
Potential solution that i see, is to create 2 chai instances:
But this looks pretty hard to support, and such approach is not possible to apply to jest version of this validatior.
Would be nice to get some api that will allow to assert against provided spec, something like:
Something like: