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

fix: mock node fetch with options.body fn #1464

Merged
merged 4 commits into from May 22, 2022
Merged

Conversation

KhafraDev
Copy link
Member

@KhafraDev KhafraDev commented May 22, 2022

Fixes #1447

No tests but I could theoretically add them in (by calling npm run node:build and then using that file, but seems pretty hacky).

Verify the fix works:

test.mjs:

import * as undici from './index.js' // 5.2.0
import { fetch } from './undici-fetch.js'

const mockAgent = new undici.MockAgent()
mockAgent.disableNetConnect()
undici.setGlobalDispatcher(mockAgent)
const pool = mockAgent.get('https://example.com')

pool.intercept({
  path: '/path',
  method: 'POST',
  body (body) {
    console.log(body)
    return true
  }
}).reply(200, { ok: 1 }).times(2)

const url = new URL('https://example.com/path').href

// undici fetch v5.2.0 from node_modules
await undici.fetch(url, {
  method: 'POST',
  body: JSON.stringify({ foo: 'bar' })
})

// undici fetch v5.2.0 from node 18.2.0 global
await fetch(url, {
  method: 'POST',
  body: JSON.stringify({ foo: 'bar' })
})
npm run build:node && node test.mjs

Output:

{"foo":"bar"}
{"foo":"bar"}
{"foo":"bar"}

The issue of the function being called 3 times is not an issue with this fix (see comment in the issue)

Everything is done in the repo's root directory.

@codecov-commenter
Copy link

codecov-commenter commented May 22, 2022

Codecov Report

Merging #1464 (f78d34d) into main (84c8a02) will decrease coverage by 0.02%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main    #1464      +/-   ##
==========================================
- Coverage   94.54%   94.51%   -0.03%     
==========================================
  Files          49       49              
  Lines        4269     4269              
==========================================
- Hits         4036     4035       -1     
- Misses        233      234       +1     
Impacted Files Coverage Δ
lib/fetch/index.js 81.19% <ø> (-0.04%) ⬇️
lib/mock/mock-agent.js 100.00% <100.00%> (ø)
lib/mock/mock-utils.js 100.00% <100.00%> (ø)
lib/fetch/request.js 85.95% <0.00%> (-0.43%) ⬇️

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 84c8a02...f78d34d. Read the comment docs.

Copy link
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

This would really benefit from having a test, as it's a major use case of undici. Maybe open an issue?

lib/mock/mock-agent.js Outdated Show resolved Hide resolved
@mcollina
Copy link
Member

cc @ronag

@KhafraDev
Copy link
Member Author

@mcollina added a test in a not so terrible way

Copy link
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 requested a review from ronag May 22, 2022 21:33
@ronag ronag merged commit c4e6775 into nodejs:main May 22, 2022
@KhafraDev KhafraDev deleted the issue-1447 branch May 22, 2022 21:47
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.

unexpected behaviours between MockAgent and node's bundled fetch
4 participants