node.js - 使用 Jest 简单模拟 Passport 功能

标签 node.js express jestjs passport.js

我目前正在对我的所有路线进行单元测试,包括一些使用自定义 Passport 身份验证功能的路线。我试图模拟 Passport 功能来测试错误处理,但我不断收到错误:

TypeError: _passport.default.authenticate(...) is not a function

这是在 /controllers/users.js 中运行的实际代码:

export const persistentLogin = (req, res, next) => {
  // Authenicate the cookie sent on the req object.
  passport.authenticate('jwt', { session: false }, async (authErr, user) => {
    // If there is an system error, send 500 error
    if (authErr) return res.sendStatus(500);

    // If no user is returned, send response showing failure.
    if (!user) {
      return res.status(200).json({
        success: 'false',
      });
    }
  })(req, res, next);
};

以下是 /tests/controllers/users.js 中的测试代码:

import passport from 'passport';
import { persistentLogin } from '../../controllers/users';

beforeEach(() => {
  mockResponse = () => {
    const response = {};
    response.status = jest.fn().mockReturnValue(response);
    response.json = jest.fn().mockReturnValue(response);
    response.sendStatus = jest.fn().mockReturnValue(response);
    response.clearCookie = jest.fn().mockReturnValue(response);
    response.cookie = jest.fn().mockReturnValue(response);
    return response;
  };
});

/**
 * persistentLogin Tests
 */
describe('Persistent Login Controller', () => {
  beforeEach(() => {
    req = {};

    res = mockResponse();

    validateLoginForm.mockClear();
    bcrypt.compare.mockClear();
  });

  // Passport authenication error
  test('Should show passport authenication error', async () => {
    passport.authenticate = jest.fn((authType, options, callback) => callback('This is an error', null));

    await persistentLogin(req, res);

    expect(passport.authenticate).toHaveBeenCalledTimes(1);

    expect(res.sendStatus).toHaveBeenCalledWith(500);
  });
});

如果我不得不猜测,我会说这与 (req, res, next) 对象在事后如何传递到 live 函数有关。但由于我们只是模拟该函数,我不确定它是否真的需要访问这些对象。

编辑#1:

根据 @jakemingolla 的评论,我现在认为这可能是因为 Jest 没有运行定义我的自定义 JWT 策略的 app.js 文件。

以下是 /app.js 文件中的代码:

import passport from 'passport';
import passportJWTStrategy from './utils/auth/passport';

app.use(passport.initialize());
passportJWTStrategy(passport);

以及 /utils/auth/passport.js 文件中的代码:

import { Strategy } from 'passport-jwt';

/**
 * Verifies JWT payload
 *
 * @param passport The instance of passport module.
 */
export default (passport) => {
  const JWTStrategy = Strategy;

  // Setup Options Object
  const opts = {};
  opts.jwtFromRequest = req => req.cookies.jwt;
  opts.secretOrKey = process.env.PASSPORT_SECRET;

  passport.use(
    new JWTStrategy(opts, (jwtPayload, done) => {
      if (Date.now() > jwtPayload.expire_date) {
        return done('jwt expired');
      }

      return done(null, jwtPayload);
    }),
  );
};

最佳答案

您只需要一点点改变:

您的 passport.authenticate 模拟只需返回一个函数:

passport.authenticate = jest.fn((authType, options, callback) => () => { callback('This is an error', null); });

关于node.js - 使用 Jest 简单模拟 Passport 功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55123936/

相关文章:

android - 在 Android 应用程序中使用哪个 WebSocket 库?

node.js - 在 sequelize 中更新模型和关联

reactjs - 为什么在 Jest 测试中 async() 会转变为 async function()?

javascript - VS Code断点跳转到其他行

javascript - Node 使用 ObjectionJS 和 Knex,一对多,从多表中返回第一个相关行

node.js - 得到 npm 模块 : How to retry for POST requests?

javascript - 如何编写多线路径进行路由?

javascript - 如何在分配变量时使用 async/await?

node.js - 快速 session 未设置 session

node.js - NODE_ENV 与 Jest