title: Jest Express testing middleware with arguments
body: <p>I'm pretty new to node and this is my first time unit testing an app. I'm doing well with Jest faking the request with Jest function as below</p>
  
  <pre><code>// Create a fake request
   const mockRequest = (sessionData, body) =&gt; ({
    session: { data: sessionData },
    body
  });
  
  // Create a fake response
   const mockResponse = () =&gt; {
    const res = {};
    res.status = jest.fn().mockReturnValue(res);
    res.json = jest.fn().mockReturnValue(res);
    return res;
  };
  
  const mockNext = () =&gt; {
    const next = jest.fn();
    return next;
  };
  </code></pre>
  
  <p>So I can use them like follows</p>
  
  <pre><code>doSomething(req, res, next);
  expect(res.status).toHaveBeenCalledWith(201);
  //or
  expect(next).toHaveBeenCalled();
  </code></pre>
  
  <p>That's enough for all the cases until I found that my authorisation middleware includes a couple of parameters so I can not pass the fake res and req as below</p>
  
  <pre><code>exports.isAllowedTo = (par1, par2) =&gt; {
      return async (req, res, next) =&gt; {
          try {
              //
              // Grant logic here that needs par1 and par2
              //
  
              if(granted)
                  next();
              else
                  return res.status(401).json({
                      error: "You don't have enough permission to perform this action"
                  });
  
          } catch (err) {
              res.status(406).json({
                  error: err.toString(),
              })
          }
      }
  }
  </code></pre>
  
  <p>If I test <code>isAllowTo(req, res, next)</code> with the mock req, res and next then I'm missing the 2 parameters needed by the function. Actually when I do this, the function <code>isAllowTo()</code> is not even called. I don't know how to deal with that. Any suggestion or approach?</p>