Skip to content

Commit d8d8119

Browse files
committed
chore: unit tests and code format
1 parent 6eac354 commit d8d8119

File tree

8 files changed

+404
-127
lines changed

8 files changed

+404
-127
lines changed

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
enable-pre-post-scripts=true
2+
package-manager-strict=false

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pinorama",
3-
"version": "0.1.0-alpha.0",
3+
"version": "0.1.0",
44
"description": "simple, log search for pino",
55
"license": "MIT",
66
"author": {
@@ -30,7 +30,7 @@
3030
"lint-staged": "^15.2.2",
3131
"turbo": "^1.13.3"
3232
},
33-
"packageManager": "pnpm@9.0.6",
33+
"packageManager": "pnpm@9",
3434
"engines": {
3535
"node": ">=20"
3636
}

packages/pino-pinorama-transport/package.json

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
"bin": {
99
"pino-pinorama": "./dist/cli.mjs"
1010
},
11-
"files": ["dist"],
11+
"files": [
12+
"dist"
13+
],
1214
"scripts": {
1315
"test": "vitest",
16+
"test:cov": "vitest run --coverage",
1417
"test:debug": "vitest --inspect-brk --no-file-parallelism",
1518
"clean": "rimraf ./dist",
1619
"build": "pnpm clean && tsc",
@@ -24,14 +27,15 @@
2427
"license": "MIT",
2528
"devDependencies": {
2629
"@types/minimist": "^1.2.5",
27-
"@types/node": "^20.12.7",
30+
"@types/node": "^20.12.11",
31+
"@vitest/coverage-v8": "^1.6.0",
2832
"pino": "^9.0.0",
2933
"pinorama-client": "workspace:*",
3034
"pinorama-server": "workspace:*",
31-
"rimraf": "^5.0.5",
32-
"tsx": "^4.7.3",
35+
"rimraf": "^5.0.6",
36+
"tsx": "^4.10.0",
3337
"typescript": "^5.4.5",
34-
"vitest": "^1.5.2"
38+
"vitest": "^1.6.0"
3539
},
3640
"publishConfig": {
3741
"access": "public"

packages/pino-pinorama-transport/src/lib.mts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ export default function pinoramaTransport(
4141
}
4242

4343
/* close */
44-
const closeFn = async () => flushFn && (await flushFn())
44+
const closeFn = async () => {
45+
return flushFn && (await flushFn())
46+
}
4547

4648
/* parseLine */
4749
const parseLineFn = (line: string) => {
@@ -64,7 +66,7 @@ export default function pinoramaTransport(
6466
/**
6567
* Filters and returns options specified by keys.
6668
*/
67-
function filterOptions<T extends object>(
69+
export function filterOptions<T extends object>(
6870
options: Partial<T>,
6971
keys: (keyof T)[]
7072
): Partial<T> | undefined {

packages/pino-pinorama-transport/tests/cli.test.mts

Whitespace-only changes.
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import { PinoramaClient } from "pinorama-client"
2+
import { afterEach, describe, expect, it, vi } from "vitest"
3+
import pinoramaTransport, { filterOptions } from "../src/lib.mts"
4+
5+
vi.mock("pinorama-client", async (importOriginal) => {
6+
const mod = await importOriginal<typeof import("pinorama-client")>()
7+
return {
8+
...mod,
9+
PinoramaClient: vi.fn(function () {
10+
this.isFlushed = false
11+
this.bulkInsert = vi.fn(() => {
12+
return {
13+
flush: () => {
14+
this.isFlushed = true
15+
}
16+
}
17+
})
18+
})
19+
}
20+
})
21+
22+
const exampleOptions = {
23+
url: "http://example.com",
24+
maxRetries: 3,
25+
backoff: 500,
26+
backoffFactor: 2,
27+
backoffMax: 10000,
28+
adminSecret: "secret123",
29+
batchSize: 50, // not a client option
30+
flushInterval: 2000 // not a client option
31+
}
32+
33+
describe("pinoramaTransport", () => {
34+
const mockedClient = vi.mocked(PinoramaClient)
35+
36+
afterEach(() => {
37+
vi.restoreAllMocks()
38+
})
39+
40+
it("should initialize client with correct filtered options", async () => {
41+
pinoramaTransport(exampleOptions)
42+
43+
expect(PinoramaClient).toHaveBeenCalledWith({
44+
url: "http://example.com",
45+
maxRetries: 3,
46+
backoff: 500,
47+
backoffFactor: 2,
48+
backoffMax: 10000,
49+
adminSecret: "secret123"
50+
})
51+
})
52+
53+
it("should properly call the bulkInsert client function", async () => {
54+
const stream = pinoramaTransport(exampleOptions)
55+
56+
expect(mockedClient.mock.instances[0].bulkInsert).toHaveBeenCalledWith(
57+
stream,
58+
{
59+
batchSize: exampleOptions.batchSize,
60+
flushInterval: exampleOptions.flushInterval
61+
}
62+
)
63+
})
64+
65+
it("should call the flush client function on destroy stream", async () => {
66+
const stream = pinoramaTransport(exampleOptions)
67+
68+
stream.write('{"msg":"hello world"}\n')
69+
stream.destroy()
70+
71+
expect(mockedClient.mock.instances[0].isFlushed).toBe(true)
72+
})
73+
})
74+
75+
describe("filterOptions", () => {
76+
it("should return an object with only specified keys", async () => {
77+
const filtered = filterOptions(exampleOptions, [
78+
"url",
79+
"maxRetries",
80+
"backoff"
81+
])
82+
83+
expect(filtered).toEqual({
84+
url: exampleOptions.url,
85+
maxRetries: exampleOptions.maxRetries,
86+
backoff: exampleOptions.backoff
87+
})
88+
})
89+
90+
it("should not modify the original options object", async () => {
91+
const originalOptions = { ...exampleOptions }
92+
filterOptions(exampleOptions, ["url"])
93+
94+
expect(exampleOptions).toEqual(originalOptions)
95+
})
96+
})

packages/pino-pinorama-transport/tests/unit.test.ts

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)