0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-06 20:40:08 -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, log,
}; };
ctx.request.ip = ip; ctx.request.ip = ip;
const additionalMockPayload: LogPayload = { foo: 'bar' };
const next = async () => { const next = async () => {
ctx.log(type, mockPayload); ctx.log(type, mockPayload);
ctx.log(type, additionalMockPayload);
}; };
await koaLog()(ctx, next); await koaLog()(ctx, next);
@ -55,6 +57,7 @@ describe('koaLog middleware', () => {
type, type,
payload: { payload: {
...mockPayload, ...mockPayload,
...additionalMockPayload,
result: LogResult.Success, result: LogResult.Success,
ip, ip,
userAgent, 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', () => { 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 () => { it('should log with error message when next throws a normal Error', async () => {
const ctx: WithLogContext<ReturnType<typeof createContextWithRouteParameters>> = { const ctx: WithLogContext<ReturnType<typeof createContextWithRouteParameters>> = {