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

Do not disable mocks during recording #1747

Closed
dziamid opened this issue Oct 16, 2019 · 4 comments
Closed

Do not disable mocks during recording #1747

dziamid opened this issue Oct 16, 2019 · 4 comments
Labels

Comments

@dziamid
Copy link

dziamid commented Oct 16, 2019

Context
Recording requests in complex flows that issue multiple requests that depend on one another is a pain. Essentially as soon as the recorder is enabled, the first real HTTP request that fails or returns undesired response would take you in the wrong direction. It is always easy or possible to simulate a real HTTP request with the desired outcome in the test environment. The examples of such cases are:

  • simulating error outputs
  • covering time-specific logic
  • test credentials are limited in the scope of functionality (hello, Twillio)
  • etc..

With an option to enable mocks during the recording you can record any complex flow with one failing request at a time.

For example, suppose there are no mocks yet defined. Then here is the proposed flow to write a test case:

  1. noc.recorder.rec()
  2. run the test and notice the first request recorded
  3. implement the mock <- this is where you take the execution of the flow in the direction you need
  4. iterate 2 & 3 to complete the test case

Alternatives
Currently, the only alternative I see is to write small isolated scripts that simulate a specific application state and record from there.

Has the feature been requested before?
Nope

If the feature request is accepted, would you be willing to submit a PR?
Absolutely, with some guidance

@paulmelnikow
Copy link
Member

Hey @dziamid thanks for opening this issue. I think I'm not understanding some part of what you're asking for. How are you suggesting using the recorder in step one, and what makes that not possible today?

I don't use the recorder much myself, so please bear with me!

@dziamid
Copy link
Author

dziamid commented Oct 31, 2019

@paulmelnikow thanks for your reply.

Generally speaking, I'm proposing to rethink this (from readme):

ATTENTION!: when recording is enabled, nock does no validation, nor will any mocks be enabled. 

We should not disable mocks during recording. They are very helpful together with recording! With the help of mocks, we can guide the system to behave in the direction we want and hence record a specific sequence of requests.

Let me elaborate with an example.
Suppose we are writing a test case for a login flow of a user in an online shop.

When the user logs in (POST /login)
And they are eligible for a specifal offer (POST /offers)
And they haven't seen the offer page before (POST /user/history)
Then they are redirected to an offer page  

With the current implementation of the recorder, you are forced to ensure that all the above requirements are met, then do the recording. From my experience, this is highly impractical or not even possible. E.g. that fact that someone eligible for a special offer can be outside of your control (think 3rd party services).

But wait, we can use the mocks to our advantage! If we allow nock to use mocks when available and fallback to real network requests then developers can:

  1. write a test case, execute and see it's failing
  2. add mock.recorder.rec({mocksEnabled: true}), execute the test, copy-paste recording for POST /login and modify the response to {statusCode: 200} to simulate successful login
  3. execute the test again, copy-paste the recording for POST /offers, modify the response to {eligible: true}
  4. ... iterate to finish the test case

@stale
Copy link

stale bot commented Jan 30, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. We try to do our best, but nock is maintained by volunteers and there is only so much we can do at a time. Thank you for your contributions.

@stale stale bot added the stale label Jan 30, 2020
@stale stale bot closed this as completed Feb 6, 2020
@createthis
Copy link

This would indeed be useful.

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

No branches or pull requests

3 participants