-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from frytg/dev/logger-test
feat: add tests for logger
- Loading branch information
Showing
7 changed files
with
202 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,39 @@ | ||
import process from 'node:process' | ||
import { test } from '@cross/test' | ||
import { assertEquals, assertExists } from '@std/assert' | ||
import { assertExists } from '@std/assert' | ||
import sinon from 'sinon' | ||
|
||
import { checkRequiredEnv } from './check-required-env.ts' | ||
|
||
test('checkRequiredEnv - returns when env variable exists', () => { | ||
// Setup | ||
const testVarName = 'TEST_ENV_VAR' | ||
process.env[testVarName] = 'test-value' | ||
const envStub = sinon.stub(process, 'env').value({ | ||
TEST_ENV_VAR: 'test-value', | ||
}) | ||
|
||
// Test | ||
checkRequiredEnv(testVarName) | ||
checkRequiredEnv('TEST_ENV_VAR') | ||
|
||
// Verify | ||
assertExists(process.env[testVarName]) | ||
assertExists(process.env.TEST_ENV_VAR) | ||
|
||
// Cleanup | ||
delete process.env[testVarName] | ||
envStub.restore() | ||
}) | ||
|
||
test('checkRequiredEnv - exits when env variable is missing', () => { | ||
// Setup | ||
const testVarName = 'MISSING_ENV_VAR' | ||
const originalExit = process.exit | ||
let exitCalled = false | ||
let exitCode: number | undefined | ||
|
||
// Mock process.exit | ||
process.exit = ((code?: number) => { | ||
exitCalled = true | ||
exitCode = code | ||
// Don't actually exit | ||
}) as typeof process.exit | ||
const envStub = sinon.stub(process, 'env').value({}) | ||
const exitStub = sinon.stub(process, 'exit') | ||
|
||
// Test | ||
checkRequiredEnv(testVarName) | ||
checkRequiredEnv('MISSING_ENV_VAR') | ||
|
||
// Verify | ||
assertEquals(exitCalled, true) | ||
assertEquals(exitCode, 1) | ||
sinon.assert.calledOnce(exitStub) | ||
sinon.assert.calledWith(exitStub, 1) | ||
|
||
// Cleanup | ||
process.exit = originalExit | ||
exitStub.restore() | ||
envStub.restore() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import process from 'node:process' | ||
import { test } from '@cross/test' | ||
import { assertEquals, assertExists } from '@std/assert' | ||
import sinon from 'sinon' | ||
|
||
import logger from './logger.ts' | ||
|
||
test('logger - includes global context in log events', () => { | ||
// Setup | ||
const envStub = sinon.stub(process, 'env').value({ | ||
K_REVISION: 'test-revision', | ||
SERVICE_NAME: 'test-service', | ||
STAGE: 'test', | ||
npm_package_version: '1.0.0', | ||
}) | ||
|
||
const writeStub = sinon.stub(process.stdout, 'write') | ||
let loggedOutput = '' | ||
writeStub.callsFake((str: string) => { | ||
loggedOutput = str | ||
return true | ||
}) | ||
|
||
// Test | ||
logger.info('test message', { | ||
source: 'test-source', | ||
data: { test: 'data' }, | ||
}) | ||
|
||
// Verify | ||
const loggedData = JSON.parse(loggedOutput) | ||
assertEquals(loggedData.host, 'test-revision') | ||
assertEquals(loggedData.serviceName, 'test-service') | ||
assertEquals(loggedData.stage, 'test') | ||
assertEquals(loggedData.version, '1.0.0') | ||
assertExists(loggedData.runtime) | ||
|
||
// Cleanup | ||
writeStub.restore() | ||
envStub.restore() | ||
}) | ||
|
||
test('logger - sets debug level correctly', () => { | ||
// Test & Verify | ||
assertEquals(logger.level, process.env.STAGE === 'dev' ? 'debug' : 'info') | ||
}) | ||
|
||
test('logger - has correct syslog levels', () => { | ||
const expectedLevels = { | ||
emerg: 0, | ||
alert: 1, | ||
crit: 2, | ||
error: 3, | ||
warning: 4, | ||
notice: 5, | ||
info: 6, | ||
debug: 7, | ||
} | ||
|
||
assertEquals(logger.levels, expectedLevels) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import process from 'node:process' | ||
import { test } from '@cross/test' | ||
import { assertEquals, assertExists } from '@std/assert' | ||
import sinon from 'sinon' | ||
|
||
import logger from './logger.ts' | ||
|
||
test('logger - formats errors correctly', () => { | ||
// Setup | ||
const testError = new Error('test error') | ||
const writeStub = sinon.stub(process.stdout, 'write') | ||
let loggedOutput = '' | ||
writeStub.callsFake((str: string) => { | ||
loggedOutput = str | ||
return true | ||
}) | ||
|
||
// Test | ||
logger.error('error occurred', { | ||
source: 'test-source', | ||
error: testError, | ||
}) | ||
|
||
// Verify | ||
const loggedData = JSON.parse(loggedOutput) | ||
assertExists(loggedData.error.message) | ||
assertExists(loggedData.error.stack) | ||
assertEquals(loggedData.error.message, 'test error') | ||
assertExists(loggedData.runtime) | ||
|
||
// Cleanup | ||
writeStub.restore() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters