generated from unjs/template
-
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.
fix: made some options optional test: added test
- Loading branch information
Showing
5 changed files
with
107 additions
and
17 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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
export interface LokiOptions { | ||
baseUrl: string; | ||
user: string; | ||
token: string; | ||
labels: Record<string, string>; | ||
baseURL: string; | ||
user?: string; | ||
token?: string; | ||
labels?: Record<string, string>; | ||
} |
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,8 +1,78 @@ | ||
import { expect, it, describe } from "vitest"; | ||
import {} from "../src"; | ||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
import { createConsola } from "consola"; | ||
import { $fetch } from "ofetch"; | ||
import { LokiReporter } from "../src"; | ||
|
||
describe("packageName", () => { | ||
it.todo("pass", () => { | ||
expect(true).toBe(true); | ||
const mocks = vi.hoisted(() => { | ||
return { | ||
$fetch: vi.fn(), | ||
}; | ||
}); | ||
|
||
vi.mock("ofetch", () => { | ||
return { | ||
$fetch: mocks.$fetch, | ||
}; | ||
}); | ||
|
||
describe("consola-loki", () => { | ||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
vi.useFakeTimers(); | ||
}); | ||
|
||
afterEach(() => { | ||
vi.useRealTimers(); | ||
}); | ||
|
||
it("should log to loki", async () => { | ||
const date = new Date(2024, 3, 8, 14, 38); | ||
vi.setSystemTime(date); | ||
|
||
vi.mocked($fetch).mockResolvedValue(() => {}); | ||
|
||
const baseURL = "https://loki-endpoint.example"; | ||
|
||
const lokiReporter = new LokiReporter({ | ||
baseURL, | ||
user: "123456", | ||
token: "glc_", | ||
}); | ||
|
||
const consola = createConsola({ | ||
reporters: [lokiReporter], | ||
}); | ||
|
||
consola.warn("Test Warn"); | ||
consola.error("Test Error"); | ||
|
||
// flush manually | ||
await lokiReporter.flush(); | ||
|
||
const timestmap = (date.getTime() * 1000 * 1000).toString(); | ||
|
||
expect($fetch).toHaveBeenCalledWith("/loki/api/v1/push", { | ||
baseURL, | ||
body: { | ||
streams: [ | ||
{ | ||
stream: { | ||
level: "warn", | ||
}, | ||
values: [[timestmap, "Test Warn"]], | ||
}, | ||
{ | ||
stream: { | ||
level: "error", | ||
}, | ||
values: [[timestmap, "Test Error"]], | ||
}, | ||
], | ||
}, | ||
headers: { | ||
Authorization: "Basic MTIzNDU2OmdsY18=", | ||
}, | ||
method: "POST", | ||
}); | ||
}); | ||
}); |