sinon-express-mock icon indicating copy to clipboard operation
sinon-express-mock copied to clipboard

Persist mutations to locals

Open alexandradeas opened this issue 6 years ago • 1 comments

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.

alexandradeas avatar Jan 29 '19 20:01 alexandradeas

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

danawoodman avatar Feb 02 '19 00:02 danawoodman