Skip to content

feat(MockInterceptor): add support for callbacks on reply#1200

Merged
mcollina merged 5 commits intonodejs:mainfrom
suneettipirneni:feat/callback-mocks
Feb 6, 2022
Merged

feat(MockInterceptor): add support for callbacks on reply#1200
mcollina merged 5 commits intonodejs:mainfrom
suneettipirneni:feat/callback-mocks

Conversation

@suneettipirneni
Copy link
Copy Markdown
Contributor

This adds the ability to have callbacks when replying to a mocked requests, similar to how nock enables callbacks for responses. This is especially helpful for situations where the response depends on certain request options.

Example of how it can be used:

mockPool
  .intercept({
    path: '/test',
    method: 'GET',
  })
  .reply(200, ({ headers }) => ({
    myHeader: headers.get('foo'),
  }));


const res = await fetch('http://localhost:3000/test', {
    headers: {
      foo: 'bar',
    },
  }).then((res) => res.json());

console.log(res); // Prints { myHeader: 'bar' }

Copy link
Copy Markdown
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

Can you please add a unit test?

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Feb 3, 2022

Codecov Report

Merging #1200 (16ddec5) into main (5c19579) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #1200   +/-   ##
=======================================
  Coverage   93.73%   93.73%           
=======================================
  Files          43       43           
  Lines        4023     4023           
=======================================
  Hits         3771     3771           
  Misses        252      252           
Impacted Files Coverage Δ
lib/mock/mock-utils.js 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5c19579...16ddec5. Read the comment docs.

Copy link
Copy Markdown
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

Good work!

Could you:

  1. add a test for the types? we use tsd!
  2. update the documentation

Copy link
Copy Markdown
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

Good work! one final question

Comment thread docs/api/MockPool.md
Copy link
Copy Markdown
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@mcollina mcollina merged commit 413b9ce into nodejs:main Feb 6, 2022
@suneettipirneni suneettipirneni deleted the feat/callback-mocks branch February 6, 2022 20:01
@delvedor delvedor mentioned this pull request Feb 11, 2022
KhafraDev pushed a commit to KhafraDev/undici that referenced this pull request Jun 23, 2022
metcoder95 pushed a commit to metcoder95/undici that referenced this pull request Dec 26, 2022
crysmags pushed a commit to crysmags/undici that referenced this pull request Feb 27, 2024
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

Successfully merging this pull request may close these issues.

3 participants