mardi 20 mars 2018

Testing Middleware and get TypeError: is not a function

I'm doing tests for middleware function and get an error:

TypeError: *** is not a function

My test.js

describe('Login', () => {
    it('it must check that function create the token', () => {
      const req = {
        body: { email: 'user@mail.com', password: '12345' }
      }
      const res = { locals: sinon.spy() }
      return authMiddleware.login(req, res)  // authMiddleware.test.js:41
        .then(res => {
          expect(res.locals.token).to.not.be.undefined;
        })
        .catch(err => console.log(err));
    });
  });

and middleware.js

module.exports = User => ({

  login(req, res, next) {
    if (!req.body.email || !req.body.password) return res.sendStatus(401);
    return User.findOne({ email: req.body.email })
      .then(user => {
        if (!user) return res.sendStatus(401);
        if (!user.isValidPassword(req.body.password)) return 
res.sendStatus(401);
        let payload = { id: user.id };
        let token = jwt.encode(payload, config.auth.jwtSecret);
        res.locals.token = token;
        next();
      })
      .catch(next);
  },
 });

Error:

TypeError: authMiddleware.login is not a function
at Context.it (test/api/middleware/authMiddleware.test.js:41:35)

What am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire