jest-express: Property 'header' is missing in type 'Request'

Hi.

This should already be fixed in 1.7.0 version however I am still getting this error with 1.9.0 version.

Issue: https://github.com/jameswlane/jest-express/issues/4 Pull: https://github.com/jameswlane/jest-express/pull/84

  • express version: 4.16.3
  • jest-express version: 1.9.0
  • node version: 10.9.0
  • npm version: 6.2.0

Relevant code or config

authorization.spec.ts

    import auth from './authorization';
    import { Request } from 'jest-express/lib/request';
  
    test('should let me pass', (done) => {
        propertiesToObject.mockReturnValue({
            'unsecured-default': ['/health'],
            'unsecured-specific': []
        });
        const middleware = auth();
        const next = jest.fn(() => done());
        const req = new Request('/health', {
            headers: {
                'Content-Type': 'application/json'
            }
        });
        middleware(req, null, next);
    });

authorization.ts

  import { Request, Response, NextFunction } from 'express';

  function authMiddleware(req: Request, res: Response, next: NextFunction) {
    ...
  }

What you did:

I have mocked the ExpressJs request and passed it in the middleware function.

What happened:

Error:

src/middleware/authorization.spec.ts:21:20 - error TS2345: Argument of type 'import(".../my-project/node_modules/jest-express/lib/req...' is not assignable to parameter of type 'e.Request'.
      Property 'header' is missing in type 'Request'.

    21         middleware(req, null, next);

Suggested solution:

Add property header in the type Request or even better use express typings.

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 4
  • Comments: 19

Most upvoted comments

I am still working on this, when I built jest-express it was a bare-bones testing utility for my project. Since then it has grown, it seems the underlying issue is I only built out the APIs for ExpressJS and not NodeJS http module that ExpressJS is built on. So I am building out a jest-node-http and jest-express will extend it like ExpressJS extends http. That should allow it to fully match the real interface of an express app. It taking me a bit to dig through all of the NodeJS http code and build a matching interface that will work. Then updatejest-express to extend it. I have about half of it complete already. So I am hoping to have it complete by the end of the month. 🤞

I started working on this issue I feel I have a solid solution to resolve the issue, it will take me a while to test and build this out. I am hoping to have it done by the end of the week.

I am hoping to start pushing up the new code in a draft PR this week 🤞, if you want to watch the progress:

@johannesschobel I have currently hit a brick wall on resolving the issue due to it involves not only mocking out Express but the underlying NodeJS layers.

On top of that, I have been traveling for work (Agile / DevSecOps Coach) since Feb and it will go till May. So most of my free time I have been spending with my wife and daughters.

I plan on jumping back on the issues when this engagement is done in May.

If anyone would like to take a stab at this issue, please do. Pull request are always welcomed.