-
-
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
3a9b95b
commit 3aad917
Showing
12 changed files
with
182 additions
and
141 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
declare module "process" { | ||
global { | ||
namespace NodeJS { | ||
interface ProcessEnv { | ||
LOG_LEVEL: "debug" | "info" | "warn" | "error"; | ||
} | ||
} | ||
} | ||
} |
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,44 +1,95 @@ | ||
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, | ||
}, | ||
}, | ||
}); | ||
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("pino", () => ({ | ||
default: vi.fn(), | ||
vi.mock("../logger", () => ({ | ||
getLogger: vi.fn(() => ({ | ||
error: mockErrorLog, | ||
})), | ||
})); | ||
|
||
describe("logger", () => { | ||
describe("use default logger", () => { | ||
it("get default logger", () => { | ||
expect(getLogger()).toEqual(defaultLogger); | ||
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("use custom logger", () => { | ||
const customLogger = { | ||
debug: vi.fn(), | ||
info: vi.fn(), | ||
warn: vi.fn(), | ||
error: vi.fn(), | ||
customProp: vi.fn(), | ||
}; | ||
|
||
beforeEach(() => { | ||
setLogger(customLogger); | ||
describe("without valid .env file", () => { | ||
const runErrorLoadEnvFile = () => loadEnvFile("./nodir/.env"); | ||
|
||
afterEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it("throw Error", async () => { | ||
expect(runErrorLoadEnvFile).toThrowError(); | ||
}); | ||
|
||
it("get custom logger", () => { | ||
expect(getLogger()).toEqual(customLogger); | ||
it("called error log", async () => { | ||
try { | ||
runErrorLoadEnvFile(); | ||
} catch (e) { | ||
expect(mockErrorLog).toBeCalled(); | ||
} | ||
}); | ||
}); | ||
}); |
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,95 +1,44 @@ | ||
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(); | ||
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, | ||
}, | ||
}, | ||
}); | ||
|
||
vi.mock("../logger", () => ({ | ||
getLogger: vi.fn(() => ({ | ||
error: mockErrorLog, | ||
})), | ||
vi.mock("pino", () => ({ | ||
default: vi.fn(), | ||
})); | ||
|
||
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("logger", () => { | ||
describe("use default logger", () => { | ||
it("get default logger", () => { | ||
expect(getLogger()).toEqual(defaultLogger); | ||
}); | ||
}); | ||
|
||
describe("without valid .env file", () => { | ||
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
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,3 +1,4 @@ | ||
{ | ||
"extends": "../../tsconfig.json" | ||
"extends": "../../tsconfig.json", | ||
"include": ["./src/**/*", "global.d.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
Oops, something went wrong.