Express 如何使用jest测试passport LocalStrategy

Express 如何使用jest测试passport LocalStrategy,express,testing,jestjs,passport.js,Express,Testing,Jestjs,Passport.js,使用express passport LocalStrategy测试进行库存,可能是根据模拟请求 test('should Test password Login auth', async (done) => { const response = jest.fn((arg)=> console.log('args', arg)); const next = jest.fn(); let mockReq = {body: { username: "test@gmail.co

使用express passport LocalStrategy测试进行库存,可能是根据模拟请求

test('should Test password Login auth', async (done) => {
  const response = jest.fn((arg)=> console.log('args', arg));
  const next = jest.fn();
  let mockReq = {body: { username: "test@gmail.com", password: "tets"}}
  let mockRes = {send: function(ats){console.log("mockResFunc", ats), response()}};

  passport.authenticate('local', ()=> response())(mockReq, mockRes);

  expect(next).not.toHaveBeenCalled();
  expect(response).toHaveBeenCalled();
但回调函数从未被调用过,因为我并没有找到密码,用户名也并没有转到passport函数。有人知道如何用笑话来模仿凭证吗(我想这是个线索)


不要为本地策略之类的东西编写模拟函数,而是编写实际函数

const request = require('supertest')
const app = require('../server/app')
describe('Login', () => {
  it('should fail with incorrect credentials', async () => {
    const res = await request(app)
      .post('/auth/login')
      .send({
        email: 'dummy',
        password: 'demo'
      })
    expect(res.statusCode).toEqual(401)
    expect(res.body).toHaveProperty('message')
  })

  it('should succeed with correct credentials', async () => {
    const res = await request(app)
      .post('/auth/login')
      .send({
        email: 'demo',
        password: 'demo'
      })
    expect(res.statusCode).toEqual(200)
    expect(res.body).toEqual({ email: 'demo' })
  })
})

describe('Logout', () => {
  it('should logout successfully', async () => {
    const res = await request(app).post('/auth/logout')
    expect(res.statusCode).toEqual(200)
    expect(res.body).toEqual({ ok: true })
  })
})

如果有人得到类似的任务,calback不会因为findOne模型而找我,我用mockingoose解决了我对这个方法的模仿。
const request = require('supertest')
const app = require('../server/app')
describe('Login', () => {
  it('should fail with incorrect credentials', async () => {
    const res = await request(app)
      .post('/auth/login')
      .send({
        email: 'dummy',
        password: 'demo'
      })
    expect(res.statusCode).toEqual(401)
    expect(res.body).toHaveProperty('message')
  })

  it('should succeed with correct credentials', async () => {
    const res = await request(app)
      .post('/auth/login')
      .send({
        email: 'demo',
        password: 'demo'
      })
    expect(res.statusCode).toEqual(200)
    expect(res.body).toEqual({ email: 'demo' })
  })
})

describe('Logout', () => {
  it('should logout successfully', async () => {
    const res = await request(app).post('/auth/logout')
    expect(res.statusCode).toEqual(200)
    expect(res.body).toEqual({ ok: true })
  })
})