From 3ef0acd40036fa7cf8731142cbee57ebb7feb89e Mon Sep 17 00:00:00 2001 From: Daniel Freytag Date: Wed, 27 Nov 2024 21:24:15 +0100 Subject: [PATCH] feat: add test for required env --- .github/workflows/test.yml | 2 +- check-required-env/check-required-env.test.ts | 45 +++++++++++++++++++ dates/dates.test.ts | 1 + deno.jsonc | 3 +- 4 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 check-required-env/check-required-env.test.ts diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index dc6ff53..4534f1c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 diff --git a/check-required-env/check-required-env.test.ts b/check-required-env/check-required-env.test.ts new file mode 100644 index 0000000..f408b94 --- /dev/null +++ b/check-required-env/check-required-env.test.ts @@ -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 +}) diff --git a/dates/dates.test.ts b/dates/dates.test.ts index ddf7a69..2c581db 100644 --- a/dates/dates.test.ts +++ b/dates/dates.test.ts @@ -1,5 +1,6 @@ import { test } from '@cross/test' import { assert, assertEquals } from '@std/assert' + import { DateTime, getDateHourMinutes, diff --git a/deno.jsonc b/deno.jsonc index 20a6d35..74a9f66 100644 --- a/deno.jsonc +++ b/deno.jsonc @@ -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": {