bikk-uk / jest-mock-express

A lightweight Jest mock for unit testing Express
MIT License
49 stars 7 forks source link

Should automatically mock `header` #53

Closed Cretezy closed 1 year ago

Cretezy commented 2 years ago

header has the following type:

    header(name: 'set-cookie'): string[] | undefined;
    header(name: string): string | undefined;

When passing headers to this library, header should be mocked to return the header. It should also be case-insensitive.

Here's the implementation I used:

  header: (header: string) => {
    const headerKeys = Object.keys(headers);

    // Lookup exact header, then case-insensitive
    const headerKey =
      headerKeys.find((headerKey) => headerKey === header) ||
      headerKeys.find((headerKey) => headerKey.toLowerCase() === header.toLowerCase());

    if (!headerKey) {
      return;
    }

    return headers[headerKey];
  },
aboyce commented 2 years ago

Thank you for raising the issue and providing an implementation 😃

Currently the library doesn't do anything at all really with the default function mocks, just that they exist. Although I can see how this could cause problems for people and might be unexpected behaviour.

I will start working through the default function mocks so they behave more as would be expected and look to release it as a new major version soon 👍

aboyce commented 1 year ago

The justification for closing this ticket is available on #67.