Mocking Passport.js (local strategy) in a unit tes

2020-06-03 02:31发布

I am using the local strategy of Passport.js to authenticate users for my API. I am writing tests for this API, and I need a way of mocking Passport.js in the tests to simulate an authenticated/unauthenticated user hitting the API.

What would be the most efficient way of doing this?

3条回答
可以哭但决不认输i
2楼-- · 2020-06-03 02:54

A bit late, but I did mock passport.js in a project and created a gist with a basic howto. Should work fine (I used passport version ~0.1.16).

查看更多
够拽才男人
3楼-- · 2020-06-03 02:54

you can use the http request objects in node and automate rest requests against your API the same way as it would occur in the real world. Look at node supertest module.

查看更多
做个烂人
4楼-- · 2020-06-03 03:04

One possible option: in your test setup, you can passport.use a mock strategy under the local name:

passport.use('local', new MockStrategy());

Implement MockStrategy to pass the request, or whatever your test needs.

查看更多
登录 后发表回答