[Question] Is it possible to use openapi-backend to validate that the response of an API match an OpenAPI spec?
Hi,
I'm currently using jest-openapi to validate if the responses of my API match my OpenAPI spec. It does not seem to be supported anymore and I'm looking to replace the tool I'm using for this use case. Is it a use case that openapi-backend supports? I see that openapi-backend is used to mock an API by using an OpenAPI spec but instead of doing that, I would like to use my API code and validate the responses it provides against my OpenAPI spec.
Thank you!
Sure! Would be great actually to document how to use validateResponse in tests 👍
https://openapistack.co/docs/openapi-backend/response-validation/
Wow, thank you for that very quick response! I believe the part I was most struggling with was to understand how I attach my Next.js API endpoints as handlers. Would you already have an example of how to do this?
Here’s a full stack example of using openapi-backend with nextjs. Doesn’t include tests unfortunately https://github.com/anttiviljami/openapi-stack-nextjs-starter
Thank you very much, I'll give it a try and see where that leads me!
I took the time to run a few tests and have hit some issues. I don't know if I'm doing it the right way but so far it seems to generally work so I'm wondering if I'm hitting a bug with the OpenAPI validator used.
https://github.com/davidlag0/todo-nextjs/pull/663/files
The test is code is in openapi2.test.js.
And the issue I've been trying to understand, and I'm no OpenAPI expert, is why the test fails with this error message when I use content with application/json in the spec:
> jest --testPathPattern='integration.openapi2' --runInBand
console.log
res: {"error":"No Task Found"}{"status":502,"err":[{"instancePath":"","schemaPath":"#/oneOf/0/type","keyword":"type","params":{"type":"array"},"message":"must be array"},{"instancePath":"","schemaPath":"#/oneOf","keyword":"oneOf","params":{"passingSchemas":null},"message":"must match exactly one schema in oneOf"}]}
at Object.log (__tests__/integration/openapi2.test.js:82:13)
console.log
res headers: { 'content-type': 'application/json' }
at Object.log (__tests__/integration/openapi2.test.js:83:13)
FAIL __tests__/integration/openapi2.test.js
Tests to satisfy OpenAPI spec with OpenAPIBackend
✕ GET /api/tasks with empty task list (38 ms)
● Tests to satisfy OpenAPI spec with OpenAPIBackend › GET /api/tasks with empty task list
expect(received).toBe(expected) // Object.is equality
Expected: 404
Received: 502
83 | console.log("res headers:", res._headers);
84 |
> 85 | expect(res._getStatusCode()).toBe(404);
| ^
86 | expect(JSON.parse(res._getData())).toEqual(
87 | expect.objectContaining({
88 | error: "No Task Found",
at Object.toBe (__tests__/integration/openapi2.test.js:85:34)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 1.215 s, estimated 2 s
Ran all test suites matching /integration.openapi2/i.
I removed the console.log lines in the code but you can see in the test response the error message and I don't understand what I need to change to get it to work.
And oddly enough, I don't even have to change the content of the response being tested (this response isn't being tested), which I find a bit strange.
Could you help me out with any pointer?
Thank you!
Hi @davidlag0, I'm also looking at alternatives for jest-openapi. Did you manage to use openapi-backend for asserting the HTTP responses in Jest?
Edit:
For anyone interested in replacing jest-openapi you can create a Jest custom matcher toSatisfyApiSpec plus validate the HTTP response as per example here https://github.com/openapistack/openapi-backend/blob/main/src/validation.test.ts#L761-L770
Hi @pablodenadai, I haven't found how to use openapi-backend for the purpose I want to use it for. I haven't tried to figure out what the issue is, if it is a bug with openapi-backend or not. There aren't a lot of projects in this area so I'm not sure how developers test APIs but I guess it is probably not against any OpenAPI spec and just independently of a spec. Would you have a full example available of what you were looking for and how you did what you wanted to do?