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

feat: add context.query() to nock back #2553

Merged
merged 2 commits into from Nov 27, 2023

Conversation

stephenprater
Copy link
Contributor

@stephenprater stephenprater commented Nov 3, 2023

When TDDing using Nock Back you're kind of in a Chicken-and-Egg situation. You want to be able to verify the external calls you need to make - but you don't know the exact nature of those external calls. This lets you list the interceptors created in a given fixture to make sure all the ones that you expect are there.

By itself, this functions as a negative expectation - like I can verify that certain calls do NOT happen in the fixture. When paired with assertScopesFinished this can be used to verify the complete set of calls in the fixture. For instance I have a fairly complex HTTP interactor that synchronizes a local data store with a remote one. I know how many calls this takes - and I know that if the scopes in the fixture complete then my interaction worked.

But if I need to re-record my interaction, I need to go over to the fixture and check it manually. This lets me make assertions about what is in the fixture - so that I can re-record them and verify that they still do what I expect.

Example Code Simplified and Anonymized to the Point Of Inaccuracy:

it('#synchronize - synchronize with the external API', async (localState) => {
  const { nockDone, context } = await back('http-interaction.json')
  
  const syncronizer = new Synchronizer(localState); 
  
  sycnronizer.syncronize();
  
  nockDone();
  
  context.assertScopesFinished();
  
  expect(context.query()).toEqual(
    expect.arrayContaining([
       expect.objectContaining({
           method: "POST",
           path: "/create/thing"
       }),
       expect.objectContaining({
          method: "POST",
          path: "create/thing"
      })
    ])
  );
})

@stephenprater stephenprater force-pushed the interrogate-contexxt branch 2 times, most recently from ab2cc06 to bc9d975 Compare November 3, 2023 15:58
@gr2m gr2m changed the title List interceptors recorded in context feat: add context.query() to nock back Nov 5, 2023
Copy link
Member

@gr2m gr2m left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me 👍🏼 could you please update the README as well to mention the explain the new .query() feature and its use case?

@gr2m
Copy link
Member

gr2m commented Nov 27, 2023

Thank you!

@gr2m gr2m merged commit 617511f into nock:main Nov 27, 2023
19 checks passed
Copy link

🎉 This PR is included in version 13.4.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging this pull request may close these issues.

None yet

2 participants