0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00

test(core): improve the coverage of koa-log (#1683)

This commit is contained in:
IceHe.Life 2022-07-27 15:09:19 +08:00 committed by GitHub
parent 17e5476955
commit 77250c6135
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -44,9 +44,11 @@ describe('koaLog middleware', () => {
log,
};
ctx.request.ip = ip;
const additionalMockPayload: LogPayload = { foo: 'bar' };
const next = async () => {
ctx.log(type, mockPayload);
ctx.log(type, additionalMockPayload);
};
await koaLog()(ctx, next);
@ -55,6 +57,7 @@ describe('koaLog middleware', () => {
type,
payload: {
...mockPayload,
...additionalMockPayload,
result: LogResult.Success,
ip,
userAgent,
@ -62,6 +65,21 @@ describe('koaLog middleware', () => {
});
});
it('should not insert a log when there is no log type', async () => {
const ctx: WithLogContext<ReturnType<typeof createContextWithRouteParameters>> = {
...createContextWithRouteParameters({ headers: { 'user-agent': userAgent } }),
// Bypass middleware context type assert
addLogContext,
log,
};
ctx.request.ip = ip;
// eslint-disable-next-line unicorn/consistent-function-scoping
const next = async () => Promise.resolve();
await koaLog()(ctx, next);
expect(insertLogMock).not.toBeCalled();
});
describe('should insert an error log with the error message when next() throws an error', () => {
it('should log with error message when next throws a normal Error', async () => {
const ctx: WithLogContext<ReturnType<typeof createContextWithRouteParameters>> = {