forked from joaolrpaulo/winston-newrelic-logs-transport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.ts
74 lines (58 loc) · 1.77 KB
/
index.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import WinstonNewrelicLogsTransport from "index";
import { beforeAll, expect, test, vi } from "vitest";
import { LEVEL, MESSAGE } from "triple-beam";
import axios, { AxiosInstance } from "axios";
vi.useFakeTimers();
const axiosCreateSpy = vi.spyOn(axios, "create");
const mockPost = vi.fn();
const mockAxiosClient = {
post: mockPost,
} as Partial<AxiosInstance> as AxiosInstance;
beforeAll(() => {
axiosCreateSpy.mockReturnValue(mockAxiosClient);
mockPost.mockImplementation(
() =>
new Promise<void>((resolve) => {
setImmediate(() => {
resolve();
});
})
);
});
test("should post and fire callback", async () => {
const transport = new WinstonNewrelicLogsTransport({
apiUrl: "logs.foo.com",
licenseKey: "000000",
axiosOptions: {
timeout: 123456,
},
});
expect(axiosCreateSpy).toHaveBeenCalledTimes(1);
expect(axiosCreateSpy).toHaveBeenCalledWith({
baseURL: "logs.foo.com",
headers: {
"Content-Type": "application/json",
"X-License-Key": "000000",
},
timeout: 123456,
});
const cb = vi.fn();
transport.log({ [MESSAGE]: "Some message", [LEVEL]: "info" }, cb);
await vi.runAllTimersAsync();
expect(cb).toHaveBeenCalledWith(null);
});
test("should handle errors", async () => {
const errorEventHandler = vi.fn();
const transport = new WinstonNewrelicLogsTransport({
apiUrl: "logs.foo.com",
licenseKey: "000000",
});
transport.on("error", errorEventHandler);
const cb = vi.fn();
const err = new Error("A timeout or something");
mockPost.mockRejectedValue(err);
transport.log({ [MESSAGE]: "Some message", [LEVEL]: "info" }, cb);
await vi.runAllTimersAsync();
expect(cb).toHaveBeenCalledWith(err);
expect(errorEventHandler).toHaveBeenCalledWith(err);
});