Skip to content

Commit

Permalink
feat: add test for required env
Browse files Browse the repository at this point in the history
  • Loading branch information
frytg committed Nov 27, 2024
1 parent 1e8ef3a commit 3ef0acd
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
run: deno run check

- name: Test
run: deno test
run: deno run test

- name: Publish dry run
run: deno publish --dry-run
Expand Down
45 changes: 45 additions & 0 deletions check-required-env/check-required-env.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import process from 'node:process'
import { test } from '@cross/test'
import { assertEquals, assertExists } from '@std/assert'

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'

// Test
checkRequiredEnv(testVarName)

// Verify
assertExists(process.env[testVarName])

// Cleanup
delete process.env[testVarName]
})

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

// Test
checkRequiredEnv(testVarName)

// Verify
assertEquals(exitCalled, true)
assertEquals(exitCode, 1)

// Cleanup
process.exit = originalExit
})
1 change: 1 addition & 0 deletions dates/dates.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { test } from '@cross/test'
import { assert, assertEquals } from '@std/assert'

import {
DateTime,
getDateHourMinutes,
Expand Down
3 changes: 2 additions & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"exclude": ["**/*.md", "**/*.yml", "**/*.yaml"]
},
"tasks": {
"check": "deno fmt --check && deno lint && biome lint"
"check": "deno fmt --check && deno lint && biome lint",
"test": "deno test --allow-sys --allow-env"
},
"lint": {
"rules": {
Expand Down

0 comments on commit 3ef0acd

Please sign in to comment.