Skip to content

Commit

Permalink
test(logger): add filter function
Browse files Browse the repository at this point in the history
  • Loading branch information
PunGrumpy committed May 22, 2024
1 parent 97b7d2c commit e844afe
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions test/logger/filter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { describe, expect, it } from 'bun:test'

import { filterLog } from '~/logger/filter'
import { LogLevel, Options } from '~/types'

describe('filterLog', () => {
const logLevel: LogLevel = 'info'
const status = 200
const method = 'GET'

it('should return true if no filter is provided', () => {
expect(filterLog(logLevel, status, method)).toBe(true)
})

it('should filter by log level (single value)', () => {
const options: Options = { config: { logFilter: { level: 'error' } } }
expect(filterLog(logLevel, status, method, options)).toBe(false)
})

it('should filter by log level (array)', () => {
const options: Options = {
config: { logFilter: { level: ['error', 'info'] } }
}
expect(filterLog(logLevel, status, method, options)).toBe(true)
})

it('should filter by status (single value)', () => {
const options: Options = { config: { logFilter: { status: 404 } } }
expect(filterLog(logLevel, status, method, options)).toBe(false)
})

it('should filter by status (array)', () => {
const options: Options = { config: { logFilter: { status: [200, 404] } } }
expect(filterLog(logLevel, status, method, options)).toBe(true)
})

it('should filter by method (single value)', () => {
const options: Options = { config: { logFilter: { method: 'POST' } } }
expect(filterLog(logLevel, status, method, options)).toBe(false)
})

it('should filter by method (array)', () => {
const options: Options = {
config: { logFilter: { method: ['GET', 'POST'] } }
}
expect(filterLog(logLevel, status, method, options)).toBe(true)
})

it('should return false if any filter condition is not met', () => {
const options: Options = {
config: { logFilter: { level: 'info', status: 404, method: 'POST' } }
}
expect(filterLog(logLevel, status, method, options)).toBe(false)
})

it('should return true if all filter conditions are met', () => {
const options: Options = {
config: { logFilter: { level: 'info', status: 200, method: 'GET' } }
}
expect(filterLog(logLevel, status, method, options)).toBe(true)
})
})

0 comments on commit e844afe

Please sign in to comment.