-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
62a8e2f
commit 8a7ffe0
Showing
12 changed files
with
205 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,44 @@ | ||
import { loadEnvFile, logger } from "../index"; | ||
import { | ||
describe, | ||
it, | ||
expect, | ||
beforeAll, | ||
afterAll, | ||
beforeEach, | ||
vi, | ||
afterEach, | ||
} from "vitest"; | ||
import { mkdir, rm, rmdir, writeFile } from "fs/promises"; | ||
|
||
describe("loadEnvFile", () => { | ||
describe("with valid .env file", () => { | ||
const openApiKeyPropName = "OPENAI_API_KEY"; | ||
|
||
afterEach(() => { | ||
delete process.env[openApiKeyPropName]; | ||
}); | ||
|
||
describe("no filePath argument", () => { | ||
const apiKey = "123"; | ||
const envFileName = ".env"; | ||
|
||
beforeAll(async () => { | ||
await writeFile(envFileName, `${openApiKeyPropName}=${apiKey}`); | ||
}); | ||
|
||
afterAll(async () => { | ||
await rm(envFileName); | ||
}); | ||
|
||
beforeEach(() => { | ||
loadEnvFile(); | ||
}); | ||
|
||
it(`loaded ${openApiKeyPropName}`, () => { | ||
expect(process.env[openApiKeyPropName]).toBe(apiKey); | ||
}); | ||
}); | ||
|
||
describe("filePath argument", () => { | ||
const apiKey = "456"; | ||
const envFileDir = "./dir"; | ||
const envFilePath = `./${envFileDir}/.env`; | ||
|
||
beforeAll(async () => { | ||
await mkdir(envFileDir); | ||
await writeFile(envFilePath, `${openApiKeyPropName}=${apiKey}`); | ||
}); | ||
|
||
afterAll(async () => { | ||
await rm(envFilePath); | ||
await rmdir(envFileDir); | ||
}); | ||
import { describe, expect, vi, it, beforeEach } from "vitest"; | ||
import { getLogger, setLogger } from "../logger"; | ||
import pino from "pino"; | ||
|
||
const defaultLogger = pino({ | ||
name: "dandori", | ||
level: "debug", | ||
transport: { | ||
target: "pino-pretty", | ||
options: { | ||
colorize: true, | ||
}, | ||
}, | ||
}); | ||
|
||
beforeEach(() => { | ||
loadEnvFile(envFilePath); | ||
}); | ||
vi.mock("pino", () => ({ | ||
default: vi.fn(), | ||
})); | ||
|
||
it(`loaded ${openApiKeyPropName}`, () => { | ||
expect(process.env[openApiKeyPropName]).toBe(apiKey); | ||
}); | ||
describe("logger", () => { | ||
describe("use default logger", () => { | ||
it("get default logger", () => { | ||
expect(getLogger()).toEqual(defaultLogger); | ||
}); | ||
}); | ||
|
||
describe("without valid .env file", () => { | ||
const mockErrorLog = vi.spyOn(logger, "error").mockImplementation(() => {}); | ||
const runErrorLoadEnvFile = () => loadEnvFile("./nodir/.env"); | ||
|
||
afterEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it("throw Error", async () => { | ||
expect(runErrorLoadEnvFile).toThrowError(); | ||
describe("use custom logger", () => { | ||
const customLogger = { | ||
debug: vi.fn(), | ||
info: vi.fn(), | ||
warn: vi.fn(), | ||
error: vi.fn(), | ||
customProp: vi.fn(), | ||
}; | ||
|
||
beforeEach(() => { | ||
setLogger(customLogger); | ||
}); | ||
|
||
it("called error log", async () => { | ||
try { | ||
runErrorLoadEnvFile(); | ||
} catch (e) { | ||
expect(mockErrorLog).toBeCalled(); | ||
} | ||
it("get custom logger", () => { | ||
expect(getLogger()).toEqual(customLogger); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { loadEnvFile } from "../index"; | ||
import { | ||
describe, | ||
it, | ||
expect, | ||
beforeAll, | ||
afterAll, | ||
beforeEach, | ||
vi, | ||
afterEach, | ||
} from "vitest"; | ||
import { mkdir, rm, rmdir, writeFile } from "fs/promises"; | ||
|
||
const mockErrorLog = vi.fn(); | ||
|
||
vi.mock("../logger", () => ({ | ||
getLogger: vi.fn(() => ({ | ||
error: mockErrorLog, | ||
})), | ||
})); | ||
|
||
describe("loadEnvFile", () => { | ||
describe("with valid .env file", () => { | ||
const openApiKeyPropName = "OPENAI_API_KEY"; | ||
|
||
afterEach(() => { | ||
delete process.env[openApiKeyPropName]; | ||
}); | ||
|
||
describe("no filePath argument", () => { | ||
const apiKey = "123"; | ||
const envFileName = ".env"; | ||
|
||
beforeAll(async () => { | ||
await writeFile(envFileName, `${openApiKeyPropName}=${apiKey}`); | ||
}); | ||
|
||
afterAll(async () => { | ||
await rm(envFileName); | ||
}); | ||
|
||
beforeEach(() => { | ||
loadEnvFile(); | ||
}); | ||
|
||
it(`loaded ${openApiKeyPropName}`, () => { | ||
expect(process.env[openApiKeyPropName]).toBe(apiKey); | ||
}); | ||
}); | ||
|
||
describe("filePath argument", () => { | ||
const apiKey = "456"; | ||
const envFileDir = "./dir"; | ||
const envFilePath = `./${envFileDir}/.env`; | ||
|
||
beforeAll(async () => { | ||
await mkdir(envFileDir); | ||
await writeFile(envFilePath, `${openApiKeyPropName}=${apiKey}`); | ||
}); | ||
|
||
afterAll(async () => { | ||
await rm(envFilePath); | ||
await rmdir(envFileDir); | ||
}); | ||
|
||
beforeEach(() => { | ||
loadEnvFile(envFilePath); | ||
}); | ||
|
||
it(`loaded ${openApiKeyPropName}`, () => { | ||
expect(process.env[openApiKeyPropName]).toBe(apiKey); | ||
}); | ||
}); | ||
}); | ||
|
||
describe("without valid .env file", () => { | ||
const runErrorLoadEnvFile = () => loadEnvFile("./nodir/.env"); | ||
|
||
afterEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it("throw Error", async () => { | ||
expect(runErrorLoadEnvFile).toThrowError(); | ||
}); | ||
|
||
it("called error log", async () => { | ||
try { | ||
runErrorLoadEnvFile(); | ||
} catch (e) { | ||
expect(mockErrorLog).toBeCalled(); | ||
} | ||
}); | ||
}); | ||
}); |
19 changes: 9 additions & 10 deletions
19
packages/libs/src/__tests__/runPromisesSequentially.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
import { configDotenv } from "dotenv"; | ||
import { loadFile } from "./loadFile"; | ||
import { logger } from "./logger"; | ||
import { getLogger } from "./logger"; | ||
|
||
export function loadEnvFile(filePath: string = ".env"): void { | ||
const loadEnvResult = configDotenv({ | ||
path: loadFile(filePath), | ||
}); | ||
if (loadEnvResult.error) { | ||
logger.error(loadEnvResult.error); | ||
getLogger().error(loadEnvResult.error); | ||
throw loadEnvResult.error; | ||
} | ||
} |
Oops, something went wrong.