Skip to content

Commit

Permalink
chore: add some unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmarqs committed Nov 2, 2024
1 parent c150809 commit 24c3e66
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
63 changes: 63 additions & 0 deletions tests/script-adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,66 @@ describe.each([
expect(config.PORT).toBe(expected.PORT);
});
});

describe("combining multiple script adapters", () => {
const testFilePath1 = path.join(__dirname, "test-multiple-script-adapter1.ts");
const testFilePath2 = path.join(__dirname, "test-multiple-script-adapter2.ts");

beforeAll(async () => {
const fileContent1 = `export default {
HOST: "app name",
PORT: "1111",
TEST_MAP: new Map([["key", "value"], ["key2", "value2"]]),
TEST_RECORD: { key: "key", value: "value1" }
}`;
const fileContent2 = `export default {
HOST: "app name2",
PORT: "1234",
TEST_MAP: new Map([["key", "value2"]]),
TEST_RECORD: { key: "key2" }
}`;

await Promise.all([
writeFile(testFilePath1, fileContent1),
writeFile(testFilePath2, fileContent2),
]);
});

afterAll(async () => {
await Promise.all([
unlink(testFilePath1),
unlink(testFilePath2),
]);
});

it("should successfully parse, merge and return type-safe data", async () => {
// given
const schema = z.object({
HOST: z.string(),
PORT: z.string().regex(/^\d+$/),
TEST_RECORD: z.object({
key: z.string(),
value: z.string()
}),
TEST_MAP: z.map(z.string(), z.string()).optional(),
});

// when
const config = await loadConfig({
schema,
adapters: [
scriptAdapter({ path: testFilePath1 }),
scriptAdapter({ path: testFilePath2 }),
],
});

// then
expect(config.HOST).toBe("app name2"); // second adapter overrides the first one
expect(config.PORT).toBe("1234"); // second adapter overrides the first one
expect(config.TEST_MAP).toEqual(new Map([["key", "value2"]])); // MAP is not mergeable so only the last one is loaded
expect(config.TEST_RECORD).toEqual({
key: "key2", // from second adapter
value: "value1" // preserved from first adapter
}); // records are merged between adapters
});
});
25 changes: 24 additions & 1 deletion tests/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { deepMerge, filterByPrefixKey } from "../src/lib/adapters/utils";
import { deepMerge, filterByPrefixKey, isMergeableObject } from "../src/lib/adapters/utils";
import { describe, it, expect } from "vitest";

describe("filterByPrefixKey", () => {
Expand Down Expand Up @@ -93,3 +93,26 @@ describe("deepMerge", () => {
expect(result).toEqual({ a: null, b: 2, c: [] });
});
});

describe("isMergeableObject", () => {
it("should return true for plain objects", () => {
expect(isMergeableObject({})).toBe(true);
expect(isMergeableObject({ a: 1 })).toBe(true);
});

it("should return false for non-mergeable values", () => {
// null and undefined
expect(isMergeableObject(null)).toBe(false);
expect(isMergeableObject(undefined)).toBe(false);

// primitives
expect(isMergeableObject(10)).toBe(false);
expect(isMergeableObject("string")).toBe(false);
expect(isMergeableObject(true)).toBe(false);

// built-in objects and collections
expect(isMergeableObject([])).toBe(false);
expect(isMergeableObject(new Map())).toBe(false);
expect(isMergeableObject(new Set())).toBe(false);
});
});

0 comments on commit 24c3e66

Please sign in to comment.