Skip to content

Commit

Permalink
test: add cli and dotenv class unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marcocesarato committed Nov 22, 2022
1 parent fd27839 commit 745ad34
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/cli.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {runCli} from "./cli";
import {load} from "./index";

jest.mock("./index");

describe("runCli", () => {
it("should expose a function", () => {
expect(runCli).toBeDefined();
});

it("runCli should not throw errors", () => {
expect(() => runCli(load)).not.toThrow();
});
});
121 changes: 121 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import Dotenv, {dotenvLoad, dotenvConfig} from "./index";
import mockFs from "mock-fs";

describe("Dotenv Mono", () => {
let instance: Dotenv;
const originalEnv = process.env;

const rootEnv = "TEST_ROOT_ENV: 1";
const defaultsEnv = "TEST_DEFAULT_ENV: 1";
const webTestEnv = "TEST_WEB_ENV: 1";

beforeEach(() => {
// Mocks
process.env = {...originalEnv, NODE_ENV: "test"};
mockFs({
"/root": {
".env": rootEnv,
".env.defaults": defaultsEnv,
"apps": {
"web": {
".env.test": webTestEnv,
},
},
},
});
// Setup
instance = new Dotenv();
});

afterEach(() => {
// Reset
jest.resetModules();
mockFs.restore();
});

it("should have a method parse() and parse a dotenv string", () => {
expect(instance.parse).toBeDefined();
const output = instance.parse("TEST_PARSE_1: 1\nTEST_PARSE_2: 2");
expect(output).toEqual({
"TEST_PARSE_1": "1",
"TEST_PARSE_2": "2",
});
});

it("should have a method load()", () => {
expect(instance.load).toBeDefined();
});

it("should load the expected environment variables from apps directory", () => {
expect(instance.load).toBeDefined();
jest.spyOn(process, "cwd").mockReturnValue("/root/apps");
expect(() => instance.load()).not.toThrow();
const expected = {
"TEST_ROOT_ENV": "1",
"TEST_DEFAULT_ENV": "1",
};
expect(instance.plain).toEqual(rootEnv);
expect(instance.env).toEqual(expected);
Object.keys(expected).forEach((key) => {
expect(process.env).toHaveProperty(key);
});
});

it("should load the expected environment variables from web directory", () => {
expect(instance.load).toBeDefined();
jest.spyOn(process, "cwd").mockReturnValue("/root/apps/web");
expect(() => instance.load()).not.toThrow();
const expected = {
"TEST_WEB_ENV": "1",
"TEST_DEFAULT_ENV": "1",
};
expect(instance.plain).toEqual(webTestEnv);
expect(instance.env).toEqual(expected);
Object.keys(expected).forEach((key) => {
expect(process.env).toHaveProperty(key);
});
});

it("should have a method loadFile() and load file without change the process env", () => {
expect(instance.loadFile).toBeDefined();
jest.spyOn(process, "cwd").mockReturnValue("/root/apps");
expect(() => instance.loadFile()).not.toThrow();
const expected = {
"TEST_ROOT_ENV": "1",
"TEST_DEFAULT_ENV": "1",
};
expect(instance.plain).toEqual(rootEnv);
expect(instance.env).not.toEqual(expected);
Object.keys(expected).forEach((key) => {
expect(process.env).not.toHaveProperty(key);
});
});

it("should have a method save() and save changes without throw errors", () => {
instance.loadFile();
expect(() =>
instance.save({
"TEST_CHANGES_ENV": "1",
}),
).not.toThrow();
expect(instance.save).toBeDefined();
});

it("should expose a function", () => {
expect(dotenvLoad).toBeDefined();
});

it("dotenvLoad should return expected output", () => {
const retValue = dotenvLoad();
expect(retValue).toBeDefined();
});

it("should expose a function", () => {
expect(dotenvConfig).toBeDefined();
});

it("dotenvConfig should return expected output", () => {
const retValue = dotenvConfig();
expect(retValue).toBeDefined();
});
});

0 comments on commit 745ad34

Please sign in to comment.