'How to test `verify` of an express middleware in Jest

I have a function which returns a middleware as such:

const jsonParser = () => {
  return express.json({
    limit: '5mb',
    verify: (req, res, buf) => {
      // If the incoming request is a stripe event,
      if (req.headers['some-header']) {
        httpContext.set('raw-body', buf.toString());
      }
    },
  });
};

I would like to test that the httpContext.setis indeed called when the some-header header is present.

My test:

describe('jsonParser middleware', () => {
  it('sets the http context', async () => {
    const req = {
      headers: {
        'some-header': 'some-sig',
        'content-type': 'application/json',
      },
      body: JSON.stringify({
        some: 'thing',
      }),
    };

    const res = {};
    const middleware = jsonParser();

    middleware(req, res, () => {});

    expect(httpContext.set).toHaveBeenCalled();
  });
});

I have no idea how to make the test run the function passed to verify. Express docs state that the content type should be json, but nothing more. Anyone that can point me in the right direction is highly appreciated.

Thank you.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source