more functional design principles for tests
Some checks failed
ci/woodpecker/pr/pr Pipeline failed

This commit is contained in:
Madison Grubb
2026-02-17 11:21:02 -05:00
parent c8d37c98f4
commit 82bd51c3a4
3 changed files with 42 additions and 34 deletions

View File

@@ -8,10 +8,12 @@ const wait = (ms = 10) => new Promise(resolve => setTimeout(resolve, ms))
describe('app/utils/logger', () => {
const consoleMocks = {}
const originalConsole = {}
let serverCalls
const testState = {
serverCalls: [],
}
beforeEach(() => {
serverCalls = []
testState.serverCalls = []
const calls = { log: [], error: [], warn: [], debug: [] }
Object.keys(calls).forEach((key) => {
@@ -22,7 +24,7 @@ describe('app/utils/logger', () => {
registerEndpoint('/api/log', async (event) => {
const body = event.body || (await readBody(event).catch(() => ({})))
serverCalls.push(body)
testState.serverCalls.push(body)
return { ok: true }
}, { method: 'POST' })
})
@@ -40,7 +42,7 @@ describe('app/utils/logger', () => {
logError('Test message')
await wait()
expect(serverCalls[0]).toMatchObject({
expect(testState.serverCalls[0]).toMatchObject({
sessionId: 'session-123',
userId: 'user-456',
})
@@ -59,7 +61,7 @@ describe('app/utils/logger', () => {
await wait()
expect(consoleMocks[consoleKey]).toHaveBeenCalledWith(`[Test message]`, { key: 'value' })
expect(serverCalls[0]).toMatchObject({
expect(testState.serverCalls[0]).toMatchObject({
level,
message: 'Test message',
data: { key: 'value' },
@@ -84,7 +86,7 @@ describe('app/utils/logger', () => {
logError('Test message')
await wait()
expect(serverCalls[0].timestamp).toMatch(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/)
expect(testState.serverCalls[0].timestamp).toMatch(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/)
})
it('handles null sessionId and userId', async () => {
@@ -92,7 +94,7 @@ describe('app/utils/logger', () => {
logError('Test message')
await wait()
const { sessionId, userId } = serverCalls[0]
const { sessionId, userId } = testState.serverCalls[0]
expect(sessionId === null || sessionId === undefined).toBe(true)
expect(userId === null || userId === undefined).toBe(true)
})