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

MockAgent: MockResponseCallbackOptions given to replyOptionsCallback is a totally wrong type #1583

Closed
jonahsnider opened this issue Jul 28, 2022 · 7 comments
Labels
bug Something isn't working good first issue Good for newcomers Types Changes related to the TypeScript definitions

Comments

@jonahsnider
Copy link

Bug Description

Reproducible By

import * as undici from 'undici';

const mockAgent = new undici.MockAgent();
mockAgent.disableNetConnect();
undici.setGlobalDispatcher(mockAgent);

const mockPool = mockAgent.get('https://example.com');

mockPool
  .intercept({
    path: '/',
  })
  .reply((request) => {
    console.log(request);

    return { statusCode: 200 };
  });

await undici.request('https://example.com/', {
  headers: {
    authorization: 'Bearer 123',
  },
});

Expected Behavior

The types should be correct.

Logs & Screenshots

Actual value:

{
  headers: { authorization: 'Bearer 123' },
  origin: 'https://example.com',
  path: '/',
  method: 'GET'
}

Expected value:

reply<TData extends object = object>(replyOptionsCallback: MockInterceptor.MockReplyOptionsCallback<TData>): MockScope<TData>;

export type MockReplyOptionsCallback<TData extends object = object> = (
opts: MockResponseCallbackOptions
) => { statusCode: number, data?: TData | Buffer | string, responseOptions?: MockResponseOptions }

export interface MockResponseCallbackOptions {
path: string;
origin: string;
method: string;
body?: BodyInit | Dispatcher.DispatchOptions['body'];
headers: Headers | Record<string, string>;
maxRedirections: number;
}

Environment

Darwin 21.5.0 Darwin Kernel Version 21.5.0: Tue Apr 26 21:08:37 PDT 2022; root:xnu-8020.121.3~4/RELEASE_ARM64_T6000 arm64

{
  node: '16.15.1',
  v8: '9.4.146.24-node.21',
  uv: '1.43.0',
  zlib: '1.2.11',
  brotli: '1.0.9',
  ares: '1.18.1',
  modules: '93',
  nghttp2: '1.47.0',
  napi: '8',
  llhttp: '6.0.4',
  openssl: '1.1.1o+quic',
  cldr: '40.0',
  icu: '70.1',
  tz: '2021a3',
  unicode: '14.0',
  ngtcp2: '0.1.0-DEV',
  nghttp3: '0.1.0-DEV'
}

Additional context

@jonahsnider jonahsnider added the bug Something isn't working label Jul 28, 2022
@mcollina
Copy link
Member

Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests.

@mcollina mcollina added good first issue Good for newcomers Types Changes related to the TypeScript definitions labels Jul 29, 2022
@sidwebworks
Copy link
Contributor

@mcollina if you want I can take this issue?

@mcollina
Copy link
Member

go for it!

@merojosa
Copy link
Contributor

@mcollina, I'm currently trying to fix this issue. I reproduced the code and, indeed,reply is receiving the following:

{
  headers: { authorization: 'Bearer 123' },
  origin: 'https://example.com',
  path: '/',
  method: 'GET'
}

However, the type for the callback says that it can have these options:

export interface MockResponseCallbackOptions {
  path: string;
  origin: string;
  method: string;
  body?: BodyInit | Dispatcher.DispatchOptions['body'];
  headers: Headers | Record<string, string>;
  maxRedirections: number;
}

maxRedirections and body are not been returned. I already checked the documentation and don't see any particular example or explanation regarding if it's posible to get the body and maxRedirections.

So I want to check if this is a mismatch between the type and what reply can receive in its parameters or there is a bug with MockAgent.

@mcollina
Copy link
Member

in this library, the types come after JS, so the js code is the source of truth.

@merojosa
Copy link
Contributor

merojosa commented May 7, 2024

@mcollina you can close this issue 👍

@metcoder95
Copy link
Member

Closed by #2951

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers Types Changes related to the TypeScript definitions
Projects
None yet
Development

No branches or pull requests

5 participants