Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properties res.request.path and res.request.method are missing #228

Closed
lightzephyr opened this issue Apr 1, 2021 · 3 comments
Closed

Properties res.request.path and res.request.method are missing #228

lightzephyr opened this issue Apr 1, 2021 · 3 comments

Comments

@lightzephyr
Copy link

jest-openapi v0.13.0
Server: Node / Express with CORS enabled
Client: Axios (v0.21.1) via Jest (v26.6.3) configured (in Typescript) as described in use case #1 on the jest-openapi Github page

Error:
`GET /version › should satisfy OpenAPI spec

TypeError: The "url" argument must be of type string. Received undefinedTypeError [ERR_INVALID_ARG_TYPE]: The "url" argument must be of type string. Received undefined

  at getPathname (node_modules/openapi-validator/lib/utils/common.utils.js:8:38)
  at OpenApi3Spec.findExpectedPathItem (node_modules/openapi-validator/lib/classes/AbstractOpenApiSpec.js:71:28)
  at OpenApi3Spec.findExpectedResponseOperation (node_modules/openapi-validator/lib/classes/AbstractOpenApiSpec.js:83:33)
  at OpenApi3Spec.findExpectedResponse (node_modules/openapi-validator/lib/classes/AbstractOpenApiSpec.js:40:44)
  at OpenApi3Spec.validateResponse (node_modules/openapi-validator/lib/classes/AbstractOpenApiSpec.js:91:31)
  at Object.<anonymous>.module.exports (node_modules/jest-openapi/src/matchers/toSatisfyApiSpec.js:13:39)`

Cause:
The res.request object does not contain the url property.

This problem, and a subsequent one, can be fixed with the following hack:

const hackResObject = (res: AxiosResponse<any>) => { res.request.path = res.config.url; res.request.method = 'GET'; };

After doing this, the test works correctly and passes and fails when it should!

Why doesn't the use case work out of the box in this environment?
Is there anything that can be done to avoid this hack?

@rwalle61
Copy link
Collaborator

rwalle61 commented Apr 1, 2021

Thanks @lightzephyr for raising this! I'm not sure why the request property is undefined - we're expecting it to have a path that is your url. How are you making the axios request, and is your jest env node or jsdom? (Our tests do it like this and use node). Perhaps we're not handling all the ways one can use axios

@lightzephyr
Copy link
Author

lightzephyr commented Apr 6, 2021

Axios was used as follows:
const res = await axios.get('http://localhost:8080/api/version');

The jest environment was set to the default "jsdom". Changing it to "node" fixed the problem.

Thank you very much for your help.

@rwalle61
Copy link
Collaborator

rwalle61 commented Apr 6, 2021

You're welcome, cheers for raising this here. I'll link it to an open issue we have to track this #217

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants