Skip to content

Commit

Permalink
test: update test
Browse files Browse the repository at this point in the history
  • Loading branch information
realyuyanan committed Dec 14, 2021
1 parent 8ed773b commit 3fa1d4d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/core/logger/__test__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as winston from "winston";
import * as TransportStream from "winston-transport";
import logger, { Logger } from "../index";
import currentContext from "../../context";

Expand Down Expand Up @@ -78,4 +80,17 @@ describe("logger test", () => {
Logger.clean();
expect(log.arr).toBe(null);
});

test("log could be logged by winstonLogger", async () => {
const transportLog = jest.fn();
const transport = new TransportStream({ log: transportLog });
logger.winstonLogger = winston.createLogger({
format: winston.format.simple(),
transports: [transport]
});

const logInfo = "LOG INFO";
logger.info(logInfo);
expect(transportLog.mock.calls[0][0].message).toContain(logInfo);
});
});
25 changes: 25 additions & 0 deletions lib/core/runtime/__test__/console.hack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,31 @@ describe("console hack test", () => {
(getCurrentContext as jest.Mock).mockClear();
});

test("console function should be the origin function", () => {
(getCurrentContext as jest.Mock).mockImplementation(() => null);
const debug = jest.fn();
console.originDebug = debug;
console.debug("debug");
expect(debug.mock.calls[0][0]).toBe("debug");
const log = jest.fn();
console.originLog = log;
console.log("log");
expect(log.mock.calls[0][0]).toBe("log");
const info = jest.fn();
console.originInfo = info;
console.info("info");
expect(info.mock.calls[0][0]).toBe("info");
const dir = jest.fn();
console.originDir = dir;
console.dir("dir");
expect(dir.mock.calls[0][0]).toBe("dir");
const warn = jest.fn();
console.originWarn = warn;
console.warn("warn");
expect(warn.mock.calls[0][0]).toBe("warn");
(getCurrentContext as jest.Mock).mockClear();
});

test("multi consoleRestore() should not have side effect", () => {
consoleRestore();
consoleRestore();
Expand Down
26 changes: 26 additions & 0 deletions lib/core/runtime/capture/__test__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as http from "http";
import { URL } from "url";
import { requestHack, requestRestore } from "../index";

/**
Expand Down Expand Up @@ -53,4 +54,29 @@ describe("capture(request hack) test", () => {
}).end();
});
});

test("http request while url is URL type", async () => {
const url = new URL(`http://127.0.0.1:${port}`);
url.username = "tsw";
url.password = "tsw";
await new Promise((resolve) => {
http.request(url, (res) => {
res.on("data", (d) => {
expect(d.toString("utf8")).toBe(RESPONSE_STRING);
resolve(0);
});
}).end();
});
});

test("http request while url is string type", async () => {
await new Promise((resolve) => {
http.request(`http://127.0.0.1:${port}`, {}, (res) => {
res.on("data", (d) => {
expect(d.toString("utf8")).toBe(RESPONSE_STRING);
resolve(0);
});
}).end();
});
});
});

0 comments on commit 3fa1d4d

Please sign in to comment.