Hello! I'm writing a unit test for a controller with a route that uses @Sesson() param decorator. I found a thread similar to what I was searching for, but it ended on declaring a separate variable. https://discord.com/channels/520622812742811698/1137991620491489290
I did so:
import { createParamDecorator, type ExecutionContext } from "@nestjs/common";
export const sessionFactory = (data: unknown, ctx: ExecutionContext) => {
const request = ctx.switchToHttp().getRequest();
return request.session;
};
export const Session = createParamDecorator(sessionFactory);
And then I use it in the params:
@TsRestHandler(c.quizzes.createQuiz)
@UseGuards(new AuthGuard())
async createQuiz(@Session() session: SessionContainer) {
return tsRestHandler(c.quizzes.createQuiz, async ({ body }) => {
const userId = session.getUserId();
const quiz = await this.quizzesService.create(body, userId);
return {
status: 201,
body: quiz,
};
});
}
I assume that in order to unit test the controller I have to mock the @Session decorator, but how do I do so. Should I mock the module using jest.mock? If yes, then what should I mock - the decorator itself or rather the session factory?