I have this demo function:
export async function myHandler(
param1: string,
param2: string,
req: Request,
next: NextFunction,
) {
const log = req.log.prefix(`[my=prefix]`);
let res;
If (param1 === 'param1') {
log("GOT PARAM 1");
} else {
res = await doSomething();
}
log("GOT HERE");
..
..
..
..
res.setReturnStatus(200)
}
next();
}
regarding the logger, this is the request that contains it:
const req: {
url: string;
params: {};
body: {
device: {
type: string;
p1: string;
p2: string;
p3: string;
};
data: {
sample: number;
};
};
metric: () => jest.Mock<any, any>;
log: {
(): jest.Mock<any, any>;
warn(): jest.Mock<...>;
error(): jest.Mock<...>;
prefix(): jest.Mock<...>;
};
timing: () => jest.Mock<...>;
}
When I wrote my unit test my line is checking the return status: expect(res.status).toBeCalledWith(200);
I want to cover the first 'if' statement, which logs 'GOT PARAM 1' but can't figure out how to intercept my call in the middle.
doSomething = jest.fn(() => Promise.resolve());
await myHandler('param1', 'param1', next); //HERE to check somehow that also was log ?
expect(res.status).toBeCalledWith(200);
tried to do something like :
const spy = jest.spyOn(req.log, 'log');
got the error:
Cannot spy the log property because it is not a function; undefined given instead
Aucun commentaire:
Enregistrer un commentaire