diff options
| author | 2023-09-18 19:29:56 -0400 | |
|---|---|---|
| committer | 2023-09-18 16:29:56 -0700 | |
| commit | 333e217c320fdf5a6b62fa31df18389f01887bdd (patch) | |
| tree | 2959dfcf1fe6c3b8390390913f9ec42a7a811870 /test | |
| parent | f77df12894e7952ea58605432bf9f6d252fff273 (diff) | |
| download | bun-333e217c320fdf5a6b62fa31df18389f01887bdd.tar.gz bun-333e217c320fdf5a6b62fa31df18389f01887bdd.tar.zst bun-333e217c320fdf5a6b62fa31df18389f01887bdd.zip | |
feat(runtime): Implement `console.Console` (#5448)
* prototype
* asdfg
* It works!
* okay its done now fr
* self review
* ok
* fix
* fix test
---------
Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
Diffstat (limited to 'test')
| -rw-r--r-- | test/js/node/console/console-constructor.test.ts | 66 |
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"); + }); +}); |
