Skip to content

Instantly share code, notes, and snippets.

View WaleedAshraf's full-sized avatar
:octocat:
Focusing

Waleed Ashraf WaleedAshraf

:octocat:
Focusing
View GitHub Profile
@WaleedAshraf
WaleedAshraf / cookies.md
Last active March 21, 2023 08:42
Mocking Express Session For Tests

How to mock express session?

sinon is being used to mock methods in tests. It is one of the most used package for testing. But Is there a way to mock session objects in tests for express?

I'm working on a project where I upload images to s3 in bulk (albums) and than further apply some processing like resizing/compression/editing on them.

It's a express app with middlewares for authorization, authentication, setting context and is working fine. Recently I started adding testcases for different routes using mocha. I'm using cookie-session for session handling in app.

For one route, I needed to mock req.session object. (Need to set it's value before calling the other route.)