Initial Commit
This commit is contained in:
94
packages/foundation/core/env/tests/tree-shake.test.ts
vendored
Normal file
94
packages/foundation/core/env/tests/tree-shake.test.ts
vendored
Normal file
@@ -0,0 +1,94 @@
|
||||
import { readFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { describe, expect, it } from "@suaveplan/testing/runner";
|
||||
|
||||
describe("tree-shake", () => {
|
||||
it("sideEffects is false", { timeout: 5000 }, () => {
|
||||
const pkgPath = join(__dirname, "..", "package.json");
|
||||
const pkg = JSON.parse(readFileSync(pkgPath, "utf8")) as {
|
||||
sideEffects?: boolean;
|
||||
};
|
||||
expect(pkg.sideEffects).toBe(false);
|
||||
});
|
||||
|
||||
it("exposes expected subpath exports", { timeout: 5000 }, () => {
|
||||
const pkgPath = join(__dirname, "..", "package.json");
|
||||
const pkg = JSON.parse(readFileSync(pkgPath, "utf8")) as {
|
||||
exports?: Record<string, unknown>;
|
||||
};
|
||||
const keys = Object.keys(pkg.exports ?? {});
|
||||
expect(keys).toContain(".");
|
||||
expect(keys).toContain("./core");
|
||||
expect(keys).toContain("./fields");
|
||||
expect(keys).toContain("./loaders");
|
||||
expect(keys).toContain("./errors");
|
||||
expect(keys).toContain("./schemas");
|
||||
expect(keys).toContain("./package.json");
|
||||
});
|
||||
|
||||
it("main export works — createEnv is callable", {
|
||||
timeout: 30000,
|
||||
}, async () => {
|
||||
const { createEnv, str, num, bool } = await import("../src/index.js");
|
||||
const env = createEnv({
|
||||
schema: {
|
||||
HOST: str().default("localhost"),
|
||||
PORT: num().default(3000),
|
||||
DEBUG: bool().default(false),
|
||||
},
|
||||
source: {},
|
||||
});
|
||||
expect(env.HOST).toBe("localhost");
|
||||
expect(env.PORT).toBe(3000);
|
||||
expect(env.DEBUG).toBe(false);
|
||||
});
|
||||
|
||||
it("errors subpath works", { timeout: 30000 }, async () => {
|
||||
const { EnvValidationError, CoercionError } = await import(
|
||||
"../src/errors/env-error.js"
|
||||
);
|
||||
const { ENV_CODES } = await import("../src/errors/error-codes.js");
|
||||
expect(ENV_CODES.VALIDATION_FAILED).toBe("ENV.VALIDATION_FAILED");
|
||||
expect(ENV_CODES.COERCE_ERROR).toBe("ENV.COERCE_ERROR");
|
||||
const err = new CoercionError("KEY", "bad", "number");
|
||||
expect(err).toBeInstanceOf(CoercionError);
|
||||
const validErr = new EnvValidationError([
|
||||
{ key: "PORT", message: "Required variable is missing" },
|
||||
]);
|
||||
expect(validErr).toBeInstanceOf(EnvValidationError);
|
||||
});
|
||||
|
||||
it("loaders subpath works", { timeout: 30000 }, async () => {
|
||||
const { objectLoader, parseDotenv } = await import(
|
||||
"../src/loaders/dotenv/dotenv.js"
|
||||
);
|
||||
const result = parseDotenv("KEY=value\nOTHER=foo");
|
||||
expect(result).toEqual({ KEY: "value", OTHER: "foo" });
|
||||
const { objectLoader: ol } = await import(
|
||||
"../src/loaders/object/object.js"
|
||||
);
|
||||
const loader = ol({ source: { A: "1" } });
|
||||
expect(loader.load().values).toEqual({ A: "1" });
|
||||
// suppress unused warning
|
||||
void objectLoader;
|
||||
});
|
||||
|
||||
it("schemas subpath works", { timeout: 30000 }, async () => {
|
||||
const { FieldConfigSchema, ValidationResultSchema } = await import(
|
||||
"../src/schemas/env.schemas.js"
|
||||
);
|
||||
const result = FieldConfigSchema.safeParse({
|
||||
isRequired: true,
|
||||
hasDefault: false,
|
||||
isSensitive: false,
|
||||
rules: [],
|
||||
});
|
||||
expect(result.success).toBe(true);
|
||||
const vr = ValidationResultSchema.safeParse({
|
||||
success: true,
|
||||
data: {},
|
||||
errors: [],
|
||||
});
|
||||
expect(vr.success).toBe(true);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user