Skip to content

Commit

Permalink
deno tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Pinta365 committed Mar 4, 2024
1 parent 777c881 commit 23da24d
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 5 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
## **Flexible Environment Variable Management for Deno, Bun, and Node.js**
## Cross-runtime environment variable management for Deno, Bun, and Node.js

[![JSR Version](https://jsr.io/badges/@cross/env)](https://jsr.io/@cross/env)
[![JSR Score](https://jsr.io/badges/@cross/env/score)](https://jsr.io/@cross/env/score)

This library provides a consistent and simple interface for managing environment variables across multiple runtimes,
making it ideal for cross-platform development.

## **Features**
## Features

- **Cross-runtime support:** Works seamlessly within Deno, Bun, and Node.js environments.
- **Get and Set environment variables:** Retrieve and Modify environment variables in a consistent interface across
Expand All @@ -12,7 +15,7 @@ making it ideal for cross-platform development.
- **Error handling:** Provides clear error messages for unsupported runtimes or validation failures.
- **Optional environmental file loading:** Supports loading variables from custom .env files _(experimental)_

## **Installation**
## Installation

```bash
#For Deno
Expand Down Expand Up @@ -94,7 +97,7 @@ function isPositiveNumber(value: string): boolean {
const timeout = validateAndGetEnv("TIMEOUT", isPositiveNumber);
```

## **Configuration (optional):**
## Configuration (optional)

For more advanced use cases you can configure the behaviour of the library. The library defaults to showing console
warnings but not throwing errors.
Expand Down
4 changes: 3 additions & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"tasks": {
"publish": "deno publish --config jsr.json"
"test": "deno test --allow-env ./tests/deno.ts",
"publish": "deno publish --config jsr.jsonc"
},
"lock": false,
"fmt": {
"lineWidth": 120,
"indentWidth": 4
Expand Down
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
ValidatorFunction,
} from "./lib/helpers.ts";
import { loadEnvFile } from "./lib/filehandler.ts";
export { FileReadError, UnsupportedEnvironmentError, ValidationError } from "./lib/helpers.ts";
export type { ValidatorFunction } from "./lib/helpers.ts";

/**
Expand Down
160 changes: 160 additions & 0 deletions tests/deno.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
import { assert, assertEquals, assertThrows } from "https://deno.land/[email protected]/assert/mod.ts";

import {
getAllEnv,
getEnv,
hasEnv,
setEnv,
setupEnv,
validateAndGetEnv,
validateEnv,
ValidationError,
ValidatorFunction,
} from "../mod.ts";

/** ==== getEnv() ==== */
Deno.test({
name: "getEnv() retrieves an existing environment variable",
fn() {
Deno.env.set("TEST_VARIABLE", "hello");

const value = getEnv("TEST_VARIABLE");
assertEquals(value, "hello");

Deno.env.delete("TEST_VARIABLE");
},
});

Deno.test({
name: "getEnv() undefined for nonexistant keys",
fn: () => {
const result = getEnv("TEST_KEY_NONEXISTANT");
assertEquals(result, undefined);
},
});

/** ==== getAllEnv() ==== */
Deno.test({
name: "getAllEnv() with prefix filters variables",
fn: () => {
Deno.env.set("API_KEY", "12345");
Deno.env.set("API_VERSION", "v2");
Deno.env.set("OTHER_VAR", "test");

const apiVariables = getAllEnv("API_");

assertEquals(apiVariables, {
API_KEY: "12345",
API_VERSION: "v2",
});

Deno.env.delete("API_KEY");
Deno.env.delete("API_VERSION");
Deno.env.delete("OTHER_VAR");
},
});

/** ==== setEnv() ==== */
Deno.test({
name: "setEnv() creates a new environment variable",
fn: () => {
const key = "TEST_VARIABLE";
const value = "test_value";

setEnv(key, value);
assertEquals(Deno.env.get(key), value);

Deno.env.delete(key);
},
});

Deno.test({
name: "setEnv() overwrites an existing environment variable",
fn: () => {
const key = "TEST_VARIABLE";
Deno.env.set(key, "old_value");

setEnv(key, "new_value");
assertEquals(Deno.env.get(key), "new_value");

Deno.env.delete(key);
},
});

/** ==== hasEnv() ==== */
Deno.test({
name: "hasEnv() returns true for an existing environment variable",
fn: () => {
const key = "TEST_VARIABLE";
Deno.env.set(key, "value");

assert(hasEnv(key));

Deno.env.delete(key);
},
});

Deno.test({
name: "hasEnv() returns false for a non-existent environment variable",
fn: () => {
const key = "NON_EXISTENT_VARIABLE";
assertEquals(hasEnv(key), false);
},
});

/** ==== validateEnv() ==== */
const isValidPort: ValidatorFunction = (value: string): boolean => /^\d+$/.test(value);

Deno.test({
name: "validateEnv() returns true for valid values",
fn: () => {
Deno.env.set("TEST_VALUE", "1234"); // Valid as per isValidPort
assert(validateEnv("TEST_VALUE", isValidPort));
Deno.env.delete("TEST_VALUE");
},
});

Deno.test({
name: "validateEnv() false for nonexistant keys",
fn: () => {
const result = validateEnv("TEST_VALUE_NONEXISTANT", isValidPort);
assertEquals(result, false);
},
});

/** ==== validateAndGetEnv() ==== */
Deno.test({
name: "validateAndGetEnv() returns the value for valid values",
fn: () => {
Deno.env.set("TEST_PORT", "8080");
const port = validateAndGetEnv("TEST_PORT", isValidPort);
assertEquals(port, "8080");
Deno.env.delete("TEST_PORT");
},
});

Deno.test({
name: "validateAndGetEnv() returns undefined for invalid values",
fn: () => {
Deno.env.set("PORT", "http"); // Invalid as per isValidPort

// should return undefined
const result = validateAndGetEnv("PORT", isValidPort);
assertEquals(result, undefined);

Deno.env.delete("PORT");
},
});

Deno.test({
name: "validateAndGetEnv() throws for invalid values",
fn: () => {
Deno.env.set("PORT", "http"); // Invalid as per isValidPort

setupEnv({ throwErrors: true });
//it should throw an error:
assertThrows(() => validateAndGetEnv("PORT", isValidPort), ValidationError);

Deno.env.delete("PORT");
},
});

0 comments on commit 23da24d

Please sign in to comment.