title: mocking the default middleware function in koa-jwt using jest
body: <p>I am using <code>koa-jwt</code> which in turn uses <code>jsonwebtoken</code>. Here's my router implementation:</p>
  
  <p><code>index.ts</code></p>
  
  <pre><code>const jwt = require('koa-jwt');
  ...
  // some other code
  ...
  
  export const publicEndpoints = ['/', '/openapi.json', '/healthcheck'];
  
  export default new Router&lt;ApplicationState, ApplicationContext&gt;()
    .use(configure)
    .use((ctx,next) =&gt; {
      console.log("REACH 02 opts",ctx,next);
      console.log("REACH 02 jwt", jwt);
    })
    .use(
      jwt({
        secret: customSecretLoader,
        audience: (jwksConfig.audience as unknown) as string,
        algorithms: ['RS256'],
      }).unless({ path: publicEndpoints })
    )
    // Public endpoints
    .use('/openapi.json', swagger)
    .use('/', helloworld)
    .use('/healthcheck', healthcheck)
  
    // Secure endpoints
    .get('/secure', helloworld)
    .middleware();
  </code></pre>
  
  <p>calling <code>/secure</code> should pass through the middleware that calls <code>jwt</code> and passes the token</p>
  
  <p>I want to test every secure route to make sure that it does not pass any requests that don't have the right token and passes the ones that do.</p>
  
  <p>The prior was easy, I just needed to call a secure route:</p>
  
  <p><code>index.test.ts</code></p>
  
  <pre><code>  it('Unauthenticated secure request returns 401', async () =&gt; {
      const response = await request(server).get('/secure');
      expect(response.status).toEqual(401);
    });
  </code></pre>
  
  <p>The latter however, to get it working I need to mock the <code>jwt()</code> function call and have it return <code>200</code> but the <strong>problem</strong> is that regardless of what I write in my test I am still calling the original implementation of <code>koa-jwt</code>.</p>
  
  <p>*To have some context, this is the <code>koa-js</code> library that I am trying to mock <a href="https://github.com/koajs/jwt/blob/master/lib/index.js" rel="nofollow noreferrer">https://github.com/koajs/jwt/blob/master/lib/index.js</a>. It returns <code>middleware()</code> which calls <code>verify</code> which in turn is using <code>jsonwebtoken</code> 
  <a href="https://i.stack.imgur.com/z2TyP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/z2TyP.png" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/XBY1R.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XBY1R.png" alt="enter image description here"></a></p>
  
  <p><strong>Mocking the entire exported function</strong></p>
  
  <p><code>index.test.ts</code></p>
  
  <pre><code>`var jwt = require('koa-jwt');`
  
  ...
  // some code
  ...
  
    it('Authenticated secure request returns 200', async () =&gt; {
  
      jwt = jest.fn(() =&gt; {
        Promise.resolve({
          status: 200,
          success: 'Token is valid'
        });
      });
  
      console.log("REACH 01 jwt", jwt);
  
      const response = await request(server).get('/secure');
      console.log("REACH RESPONSE",response);
      expect(response.status).toEqual(200);
    });
  </code></pre>
  
  <p>The output I get from that in the console log is:</p>
  
  <pre><code>REACH 01 jwt function mockConstructor() {
          return fn.apply(this, arguments);
          }
  </code></pre>
  
  <p>which is what I expect but when the <code>jwt()</code> is hit in <code>index.ts</code> the output I get is:</p>
  
  <pre><code>    REACH 02 jwt (opts = {}) =&gt; {
          const { debug, getToken, isRevoked, key = 'user', passthrough, tokenKey } = opts;
          const tokenResolvers = [resolveCookies, resolveAuthHeader];
  
          if (getToken &amp;&amp; typeof getToken === 'function') {
              tokenResolvers.unshift(getToken);
          }
  
          const middleware = async function jwt(ctx, next) {
              let token;
              tokenResolvers.find(resolver =&gt; token = resolver(ctx, opts));
  .....
  </code></pre>
  
  <p>I expect that having mocked <code>koa-jwt</code> that I would see the same output in both console logs.</p>
  
  <p>I tried a few different things that gave me the same result:
  - mocking the middleware function in the exported default function in <code>koa-js</code>
  - mocking the dependency of <code>koa-js</code> which is <code>jsonwebtoken</code></p>
  
  <p>What am I missing?</p>