Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

Pass ctx to nock callbacks #36

Open
blake-mealey opened this issue Jan 24, 2020 · 1 comment
Open

Pass ctx to nock callbacks #36

blake-mealey opened this issue Jan 24, 2020 · 1 comment

Comments

@blake-mealey
Copy link

It would be quite useful to have access to the context object when setting up nock responses:

fancy
  .add('resource', () => ({ ok: true }))
  .nock('https://example.com', (api, ctx) => api
    .get('/api/resource')
    .reply(ctx.resource)
@vlukashov
Copy link

Another use case for this - asserting on request properties in the test body:

fancy
  .nock('https://example.com', (api, ctx) => api
    .get('/api/resource')
    .reply(function (uri, requestBody) {
      ctx.req = this.req;
      return {};
    })
  .it('should include auth in the header', ctx => {
    expect(ctx.req.headers).to.have.property('authorization').that.matches(/Bearer/i)
  })

I know that I could write the same assertion using the nock's reqheaders property:

fancy
  .nock('https://example.com',
    { reqheaders: { 'authorization': /Bearer/i } },
    api => api.get('/api/resource').reply(200, {})
  )
  .it('should include auth in the header')

But that makes the assertion implicit - when the test has more code before the .it() line it's not immediately obvious that the request headers are the main thing this test is designed to verify.

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

No branches or pull requests

2 participants