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

Allow for the easy creation of mocking requests #788

Open
erunion opened this issue Oct 25, 2023 Discussed in #544 · 0 comments
Open

Allow for the easy creation of mocking requests #788

erunion opened this issue Oct 25, 2023 Discussed in #544 · 0 comments
Labels
area:core Issues related to `core`, which is the package that powers the SDKs at runtime enhancement New feature or request exploration A tag to show off your experiments

Comments

@erunion
Copy link
Member

erunion commented Oct 25, 2023

Discussed in #544

Originally posted by erunion May 21, 2020
When working with an API, it's helpful to be able to mock out requests when unit testing client functionality. Since we've got the OpenAPI definition on hand for the API in question, we know everything about the API and should be able to accept and return mock data according to the contract.

Nock, the library that we'd likely use for supporting mocks, has an environmental flag that lets you completely disable Nock.

Along these lines, the two ideas that we have for mocking API requests out of SDKs are:

  • Expose an API_MOCKS=true environmental variable that when enabled, causes the SDK to disable network traffic and mock out all requests.
  • Alternatively, if you just want to mock out a single endpoint we should have a .mock() method that you could chain to your API call as such:
const sdk = require('api')('petstore.json');
sdk.mock().listPets().then(res => {
  // `res` is a 200 response and `res.json()` contains mock data based on the API
});

Unanswered questions right now are:

  • How would we mock out failing requests? Would that entail you supplying .mock() with the status code you wish to cause .listPets() to return?
@erunion erunion added enhancement New feature or request exploration A tag to show off your experiments area:core Issues related to `core`, which is the package that powers the SDKs at runtime labels Oct 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:core Issues related to `core`, which is the package that powers the SDKs at runtime enhancement New feature or request exploration A tag to show off your experiments
Projects
None yet
Development

No branches or pull requests

1 participant