aboutsummaryrefslogtreecommitdiff
path: root/test/js/node/console/console-constructor.test.ts
diff options
context:
space:
mode:
Diffstat (limited to 'test/js/node/console/console-constructor.test.ts')
-rw-r--r--test/js/node/console/console-constructor.test.ts66
1 files changed, 66 insertions, 0 deletions
diff --git a/test/js/node/console/console-constructor.test.ts b/test/js/node/console/console-constructor.test.ts
new file mode 100644
index 000000000..2e2b29832
--- /dev/null
+++ b/test/js/node/console/console-constructor.test.ts
@@ -0,0 +1,66 @@
+import { test, describe, expect } from "bun:test";
+import { Console } from "node:console";
+
+import { Writable } from "node:stream";
+
+function writable() {
+ let intoString = "";
+ const { promise, resolve } = Promise.withResolvers();
+ const stream = new Writable({
+ write(chunk) {
+ intoString += chunk.toString();
+ },
+ destroy() {
+ resolve(intoString);
+ },
+ autoDestroy: true,
+ });
+
+ (stream as any).write = (chunk: any) => {
+ intoString += Buffer.from(chunk).toString("utf-8");
+ };
+
+ return [stream, () => promise] as const;
+}
+
+describe("console.Console", () => {
+ test("global instanceof Console", () => {
+ expect(global.console).toBeInstanceOf(Console);
+ });
+
+ test("new Console instanceof Console", () => {
+ const c = new Console({ stdout: process.stdout, stderr: process.stderr });
+ expect(c).toBeInstanceOf(Console);
+ });
+
+ test("it can write to a stream", async () => {
+ console.log();
+ const [stream, value] = writable();
+ const c = new Console({ stdout: stream, stderr: stream, colorMode: false });
+ c.log("hello");
+ c.log({ foo: "bar" });
+ stream.end();
+ expect(await value()).toBe("hello\n{ foo: 'bar' }\n");
+ });
+
+ test("can enable colors", async () => {
+ const [stream, value] = writable();
+ const c = new Console({ stdout: stream, stderr: stream, colorMode: true });
+ c.log("hello");
+ c.log({ foo: "bar" });
+ stream.end();
+ expect(await value()).toBe("hello\n{ foo: \u001B[32m'bar'\u001B[39m }\n");
+ });
+
+ test("stderr and stdout are separate", async () => {
+ const [out, outValue] = writable();
+ const [err, errValue] = writable();
+ const c = new Console({ stdout: out, stderr: err });
+ c.log("hello world!");
+ c.error("uh oh!");
+ out.end();
+ err.end();
+ expect(await outValue()).toBe("hello world!\n");
+ expect(await errValue()).toBe("uh oh!\n");
+ });
+});