Open davidlag0 opened 1 month ago
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,
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 thatopenapi-backend
supports? I see thatopenapi-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!