danawoodman / sinon-express-mock

Simple request and response mock objects to pass into Express routes when testing using Sinon.
https://www.npmjs.com/package/sinon-express-mock
MIT License
47 stars 16 forks source link

Persist mutations to locals #17

Open alexandradeas opened 5 years ago

alexandradeas commented 5 years ago

Locals can not currently be mutated after their assignment. This causes issues when testing the following:

const middleware = (req, res, next) => {
  res.locals.myVar = "new";
  next();
};

const req = mockReq();
const res = mockRes({ locals: { myVar: "old" } });

middleware(req, res, spy());

res.locals.myVar // "old"

This is useful to test as some middleware can be reliant on previous middleware setting locals (such as decoding tokens).

@danawoodman I noticed from https://github.com/danawoodman/sinon-express-mock/issues/12 that you mentioned you're not actively maintaining this repo anymore, in which case I'll get around to this when I get a chance. Otherwise if anyone else notices this I'm happy for them to pick this up.

danawoodman commented 5 years ago

Yeah, the mocking is pretty "dumb" and doesn't handle locals well. Very happy for a PR 👍