From 1c20e05d7011a302c04adc1f7aa6fc3ce3963799 Mon Sep 17 00:00:00 2001 From: Jarred SUmner Date: Wed, 11 Jan 2023 17:13:46 -0800 Subject: [Bun.serve] Introduce publishToSelf boolean on websocket: {} config object --- test/bun.js/websocket-server.test.ts | 52 ++++++++++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 5 deletions(-) (limited to 'test/bun.js') diff --git a/test/bun.js/websocket-server.test.ts b/test/bun.js/websocket-server.test.ts index 1d9c15341..0dc421eb6 100644 --- a/test/bun.js/websocket-server.test.ts +++ b/test/bun.js/websocket-server.test.ts @@ -1,6 +1,6 @@ -import { serve } from "bun"; import { describe, expect, it } from "bun:test"; import { gcTick } from "./gc"; +import { serve } from "bun"; var port = 4321; function getPort() { @@ -49,6 +49,44 @@ describe("websocket server", () => { done(); }); + it("can do publish() with publishToSelf: false", async (done) => { + var server = serve({ + port: getPort(), + websocket: { + open(ws) { + ws.subscribe("all"); + ws.publish("all", "hey"); + server.publish("all", "hello"); + }, + message(ws, msg) { + if (new TextDecoder().decode(msg) !== "hello") { + done(new Error("unexpected message")); + } + }, + close(ws) {}, + publishToSelf: false, + }, + fetch(req, server) { + if (server.upgrade(req)) { + return; + } + + return new Response("success"); + }, + }); + + await new Promise((resolve2, reject2) => { + var socket = new WebSocket(`ws://${server.hostname}:${server.port}`); + + socket.onmessage = (e) => { + expect(e.data).toBe("hello"); + resolve2(); + }; + }); + server.stop(); + done(); + }); + for (let method of ["publish", "publishText", "publishBinary"]) { describe(method, () => { it("in close() should work", async () => { @@ -463,7 +501,9 @@ describe("websocket server", () => { server.stop(); expect(() => { server.upgrade(req); - }).toThrow('To enable websocket support, set the "websocket" object in Bun.serve({})'); + }).toThrow( + 'To enable websocket support, set the "websocket" object in Bun.serve({})', + ); return new Response("success"); }, }); @@ -826,9 +866,11 @@ describe("websocket server", () => { fetch(req) { gcTick(); server.stop(); - if (server.upgrade(req, { - data: { count: 0 }, - })) + if ( + server.upgrade(req, { + data: { count: 0 }, + }) + ) return; return new Response("noooooo hello world"); }, -- cgit v1.2.3 From e65def0f82482a08521ec608efa6d8c14a700270 Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Wed, 11 Jan 2023 20:21:07 -0800 Subject: Remove extra wrapper --- test/bun.js/child_process-node.test.js | 34 ++-------------------------------- 1 file changed, 2 insertions(+), 32 deletions(-) (limited to 'test/bun.js') diff --git a/test/bun.js/child_process-node.test.js b/test/bun.js/child_process-node.test.js index 3cb8cd9e1..41e3e6afc 100644 --- a/test/bun.js/child_process-node.test.js +++ b/test/bun.js/child_process-node.test.js @@ -1,7 +1,6 @@ -import { beforeAll, describe, it as it_ } from "bun:test"; +import { beforeAll, describe, expect, it } from "bun:test"; import { ChildProcess, spawn, exec } from "node:child_process"; import { - strictEqual, throws, assert, createCallCheckCtx, @@ -9,36 +8,7 @@ import { } from "node-test-helpers"; import { tmpdir } from "node:os"; import { gcTick } from "gc"; - -const it = (label, fn) => { - const hasDone = fn.length === 1; - if (fn.constructor.name === "AsyncFunction" && hasDone) { - return it_(label, async (done) => { - gcTick(); - await fn(done); - gcTick(); - }); - } else if (hasDone) { - return it_(label, (done) => { - gcTick(); - fn(done); - gcTick(); - }); - } else if (fn.constructor.name === "AsyncFunction") { - return it_(label, async () => { - gcTick(); - await fn(); - gcTick(); - }); - } else { - return it_(label, () => { - gcTick(); - fn(); - gcTick(); - }); - } -}; - +const strictEqual = (a, b) => expect(a).toStrictEqual(b); const debug = process.env.DEBUG ? console.log : () => {}; const platformTmpDir = require("fs").realpathSync(tmpdir()); -- cgit v1.2.3 From edf9757650f8b498841d1a95269289b74d0b4023 Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Thu, 12 Jan 2023 10:14:35 -0800 Subject: Fixes #1772 --- src/js_parser.zig | 5 +++-- test/bun.js/transpiler.test.js | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) (limited to 'test/bun.js') diff --git a/src/js_parser.zig b/src/js_parser.zig index 1fa86041f..1dbf1ebf9 100644 --- a/src/js_parser.zig +++ b/src/js_parser.zig @@ -117,8 +117,9 @@ fn foldStringAddition(lhs: Expr, rhs: Expr) ?Expr { switch (lhs.data) { .e_string => |left| { if (rhs.data == .e_string and left.isUTF8() and rhs.data.e_string.isUTF8()) { - lhs.data.e_string.push(rhs.data.e_string); - return lhs; + var orig = lhs.data.e_string.*; + orig.push(rhs.data.e_string); + return Expr.init(E.String, orig, lhs.loc); } }, .e_binary => |bin| { diff --git a/test/bun.js/transpiler.test.js b/test/bun.js/transpiler.test.js index bc7102b95..94c3d52d2 100644 --- a/test/bun.js/transpiler.test.js +++ b/test/bun.js/transpiler.test.js @@ -914,6 +914,19 @@ export var ComponentThatHasSpreadCausesDeopt = $jsx(Hello, { }); it("fold string addition", () => { + expectPrinted_( + ` +const a = "[^aeiou]"; +const b = a + "[^aeiouy]*"; +console.log(a); + `, + ` +const a = "[^aeiou]"; +const b = a + "[^aeiouy]*"; +console.log(a) + `.trim(), + ); + expectPrinted_( `export const foo = "a" + "b";`, `export const foo = "ab"`, @@ -1734,6 +1747,17 @@ class Foo { `return "foobar";`, ); + check( + ` +const a = "[^aeiou]"; +const b = a + "[^aeiouy]*"; +console.log(a, b); + `, + ` +console.log("[^aeiou]", "[^aeiou][^aeiouy]*"); + `.trim(), + ); + // check that it doesn't inline after "var" check( ` -- cgit v1.2.3 From bb5efb67ab28849a28d0d41939734dc6336c40c0 Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Thu, 12 Jan 2023 12:08:11 -0800 Subject: Update socket.test.ts --- test/bun.js/socket/socket.test.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'test/bun.js') diff --git a/test/bun.js/socket/socket.test.ts b/test/bun.js/socket/socket.test.ts index 200f9528c..3c41c96f7 100644 --- a/test/bun.js/socket/socket.test.ts +++ b/test/bun.js/socket/socket.test.ts @@ -25,12 +25,7 @@ it("should keep process alive only when active", async () => { lines.filter(function (line) { return line.startsWith("[Client]"); }), - ).toEqual([ - "[Client] OPENED", - "[Client] GOT response", - "[Client] ENDED", - "[Client] CLOSED", - ]); + ).toEqual(["[Client] OPENED", "[Client] GOT response", "[Client] CLOSED"]); }); it("listen() should throw connection error for invalid host", () => { -- cgit v1.2.3 From 76e6a178e3a0f1f12f42cd9980349e18145258f2 Mon Sep 17 00:00:00 2001 From: Ethan Burrell <30448729+ethanburrell@users.noreply.github.com> Date: Thu, 12 Jan 2023 12:49:36 -0800 Subject: fix(bun-test): test title in results (#1753) * fix(bun-test): test title in results * missed case * clean up import * respond to reviews --- src/bun.js/test/jest.zig | 2 +- src/cli/test_command.zig | 8 ++++-- test/bun.js/bun-test/nested-describes.test.ts | 38 +++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 test/bun.js/bun-test/nested-describes.test.ts (limited to 'test/bun.js') diff --git a/src/bun.js/test/jest.zig b/src/bun.js/test/jest.zig index 00c8148b2..37c4c79cb 100644 --- a/src/bun.js/test/jest.zig +++ b/src/bun.js/test/jest.zig @@ -1737,7 +1737,7 @@ pub const DescribeScope = struct { var scope = allocator.create(DescribeScope) catch unreachable; scope.* = .{ .label = (label.toSlice(allocator).cloneIfNeeded(allocator) catch unreachable).slice(), - .parent = this, + .parent = active orelse this, .file_id = this.file_id, }; var new_this = DescribeScope.Class.make(ctx, scope); diff --git a/src/cli/test_command.zig b/src/cli/test_command.zig index a36561be0..8fb8bf01b 100644 --- a/src/cli/test_command.zig +++ b/src/cli/test_command.zig @@ -113,7 +113,9 @@ pub const CommandLineReporter = struct { const color_code = comptime if (skip) "" else ""; if (Output.enable_ansi_colors_stderr) { - for (scopes) |scope| { + for (scopes) |_, i| { + const index = (scopes.len - 1) - i; + const scope = scopes[index]; if (scope.label.len == 0) continue; writer.writeAll(" ") catch unreachable; @@ -123,7 +125,9 @@ pub const CommandLineReporter = struct { writer.writeAll(" >") catch unreachable; } } else { - for (scopes) |scope| { + for (scopes) |_, i| { + const index = (scopes.len - 1) - i; + const scope = scopes[index]; if (scope.label.len == 0) continue; writer.writeAll(" ") catch unreachable; writer.writeAll(scope.label) catch unreachable; diff --git a/test/bun.js/bun-test/nested-describes.test.ts b/test/bun.js/bun-test/nested-describes.test.ts new file mode 100644 index 000000000..de7ba194e --- /dev/null +++ b/test/bun.js/bun-test/nested-describes.test.ts @@ -0,0 +1,38 @@ +import { +describe, +expect, +test, +} from "bun:test"; + +/* +In this test we want the tests to print out the following on a success. +Each success / fail should show the path of describe and test scopes + +✓ outer most describe > mid describe 1 > inner most describe 1 > first +✓ outer most describe > mid describe 1 > inner most describe 2 > second +✓ outer most describe > mid describe 2 > inner most describe 3 > first + +@TODO add testing for this, would require to read the test console output +*/ + +describe("outer most describe", () => { + describe("mid describe 1", () => { + describe("inner most describe 1", () => { + test("first", () => { + expect(5).toEqual(5); + }) + }) + describe("inner most describe 2", () => { + test("second", () => { + expect(5).toEqual(5); + }) + }) + }) + describe("mid describe 2", () => { + describe("inner most describe 3", () => { + test("third", () => { + expect(5).toEqual(5); + }) + }) + }) +}) -- cgit v1.2.3 From 90c395bdac2b3e966e4d9f7022fd8f1c75fa24c2 Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Thu, 12 Jan 2023 12:57:27 -0800 Subject: Update inspect.test.js --- test/bun.js/inspect.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test/bun.js') diff --git a/test/bun.js/inspect.test.js b/test/bun.js/inspect.test.js index 8789b7aba..43f7536ef 100644 --- a/test/bun.js/inspect.test.js +++ b/test/bun.js/inspect.test.js @@ -2,7 +2,7 @@ import { it, expect, describe } from "bun:test"; it("Blob inspect", () => { expect(Bun.inspect(new Blob(["123"]))).toBe(`Blob (3 bytes)`); - expect(Bun.inspect(new Blob(["123".repeat(900)]))).toBe(`Blob (3 KB)`); + expect(Bun.inspect(new Blob(["123".repeat(900)]))).toBe(`Blob (2.70 KB)`); expect(Bun.inspect(Bun.file("/tmp/file.txt"))) .toBe(`FileRef ("/tmp/file.txt") { type: "text/plain;charset=utf-8" -- cgit v1.2.3 From bbd4504954b6eacb6cb5ee480a70b9cb65e6e700 Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Thu, 12 Jan 2023 13:09:10 -0800 Subject: Add a couple more tests for errors with Bun.file() --- test/bun.js/fetch.test.js | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) (limited to 'test/bun.js') diff --git a/test/bun.js/fetch.test.js b/test/bun.js/fetch.test.js index ca8e387bf..5d0ca4854 100644 --- a/test/bun.js/fetch.test.js +++ b/test/bun.js/fetch.test.js @@ -1,5 +1,5 @@ -import { it, describe, expect } from "bun:test"; -import fs, { unlinkSync } from "fs"; +import { afterAll, beforeAll, describe, expect, it, test } from "bun:test"; +import fs, { chmodSync, unlinkSync } from "fs"; import { mkfifo } from "mkfifo"; import { gc, withoutAggressiveGC } from "./gc"; @@ -393,6 +393,46 @@ describe("Bun.file", () => { const { size } = Bun.file("/tmp/test-fifo"); expect(size).toBe(Infinity); }); + + function forEachMethod(fn) { + const method = ["arrayBuffer", "text", "json"]; + for (const m of method) { + test(m, fn(m)); + } + } + + describe("bad permissions throws", () => { + beforeAll(async () => { + try { + unlinkSync("/tmp/my-new-file"); + } catch {} + await Bun.write("/tmp/my-new-file", "hey"); + chmodSync("/tmp/my-new-file", 0o000); + }); + afterAll(() => { + try { + unlinkSync("/tmp/my-new-file"); + } catch {} + }); + + forEachMethod((m) => () => { + const file = Bun.file("/tmp/my-new-file"); + expect(async () => await file[m]()).toThrow("Permission denied"); + }); + }); + + describe("non-existent file throws", () => { + beforeAll(() => { + try { + unlinkSync("/tmp/does-not-exist"); + } catch {} + }); + + forEachMethod((m) => async () => { + const file = Bun.file("/tmp/does-not-exist"); + expect(async () => await file[m]()).toThrow("No such file or directory"); + }); + }); }); describe("Blob", () => { -- cgit v1.2.3 From 35d0cf910da9899dc911568f07d37f667aff76fd Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Thu, 12 Jan 2023 13:10:32 -0800 Subject: Uncomment flaky test --- test/bun.js/bun-write.test.js | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) (limited to 'test/bun.js') diff --git a/test/bun.js/bun-write.test.js b/test/bun.js/bun-write.test.js index fdf31679f..864333ca6 100644 --- a/test/bun.js/bun-write.test.js +++ b/test/bun.js/bun-write.test.js @@ -281,27 +281,21 @@ it("Bun.write(Bun.stderr, 'new TextEncoder().encode(Bun.write STDERR TEST'))", a // FLAKY TEST // Since Bun.file is resolved lazily, this needs to specifically be checked -// it("Bun.write('output.html', HTMLRewriter.transform(Bun.file)))", async (done) => { -// var rewriter = new HTMLRewriter(); +it("Bun.write('output.html', HTMLRewriter.transform(Bun.file)))", async (done) => { + var rewriter = new HTMLRewriter(); -// rewriter.on("div", { -// element(element) { -// element.setInnerContent("it worked!", { html: true }); -// }, -// }); -// globalThis["HTMLRewriter.a"] = Bun.write( -// "/tmp/html-rewriter.txt.js", -// "
hello
", -// ); -// await globalThis["HTMLRewriter.a"]; -// var input = new Response(Bun.file("/tmp/html-rewriter.txt.js")); -// var output = rewriter.transform(input); -// const outpath = `/tmp/html-rewriter.${Date.now()}.html`; -// globalThis["HTMLRewriter.a"] = Bun.write(outpath, output); -// await globalThis["HTMLRewriter.a"]; -// console.log("HIERE"); -// expect(await Bun.file(outpath).text()).toBe( -// "
it worked!
", -// ); -// done(); -// }); + rewriter.on("div", { + element(element) { + element.setInnerContent("it worked!", { html: true }); + }, + }); + await Bun.write("/tmp/html-rewriter.txt.js", "
hello
"); + var input = new Response(Bun.file("/tmp/html-rewriter.txt.js")); + var output = rewriter.transform(input); + const outpath = `/tmp/html-rewriter.${Date.now()}.html`; + await Bun.write(outpath, output); + expect(await Bun.file(outpath).text()).toBe( + "
it worked!
", + ); + done(); +}); -- cgit v1.2.3 From 38f9bb96df7c0a253f5e26ad81c88a0a1179a0d3 Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Thu, 12 Jan 2023 13:20:10 -0800 Subject: use .skip() for failing test --- test/bun.js/inspect.test.js | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) (limited to 'test/bun.js') diff --git a/test/bun.js/inspect.test.js b/test/bun.js/inspect.test.js index 43f7536ef..738442211 100644 --- a/test/bun.js/inspect.test.js +++ b/test/bun.js/inspect.test.js @@ -30,26 +30,25 @@ it("Blob inspect", () => { }`); }); -// this test is currently failing! -// it("utf16 property name", () => { -// var { Database } = require("bun:sqlite"); -// const db = Database.open(":memory:"); -// expect("笑".codePointAt(0)).toBe(31505); +it.skip("utf16 property name", () => { + var { Database } = require("bun:sqlite"); + const db = Database.open(":memory:"); + expect("笑".codePointAt(0)).toBe(31505); -// // latin1 escaping identifier issue -// expect(Object.keys({ 笑: "hey" })[0].codePointAt(0)).toBe(31505); + // latin1 escaping identifier issue + expect(Object.keys({ 笑: "hey" })[0].codePointAt(0)).toBe(31505); -// const output = JSON.stringify( -// [ -// { -// 笑: "😀", -// }, -// ], -// null, -// 2, -// ); -// expect(Bun.inspect(db.prepare("select '😀' as 笑").all())).toBe(output); -// }); + const output = JSON.stringify( + [ + { + 笑: "😀", + }, + ], + null, + 2, + ); + expect(Bun.inspect(db.prepare("select '😀' as 笑").all())).toBe(output); +}); it("latin1", () => { expect(Bun.inspect("English")).toBe("English"); -- cgit v1.2.3 From 0384d3c558b3f8e879fd5292d1613cc618962542 Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Thu, 12 Jan 2023 15:37:03 -0800 Subject: less flaky --- test/bun.js/esbuild-child_process.test.ts | 63 +++++++++---------------------- test/bun.js/esbuild-test.js | 37 ++++++++++++++++++ 2 files changed, 54 insertions(+), 46 deletions(-) create mode 100644 test/bun.js/esbuild-test.js (limited to 'test/bun.js') diff --git a/test/bun.js/esbuild-child_process.test.ts b/test/bun.js/esbuild-child_process.test.ts index 511779d9f..d64786602 100644 --- a/test/bun.js/esbuild-child_process.test.ts +++ b/test/bun.js/esbuild-child_process.test.ts @@ -1,49 +1,20 @@ -import { transform, transformSync } from "esbuild"; -import { describe, it, expect } from "bun:test"; +import { spawnSync } from "bun"; +import { describe, it, expect, test } from "bun:test"; +import { bunExe } from "bunExe"; -describe("child_process.spawn - esbuild", () => { - it("should transform successfully", async () => { - const result = await transform("console.log('hello world')", { - loader: "js", - target: "node12", - }); - expect(result.code).toBe('console.log("hello world");\n'); - }); +test("esbuild", () => { + const { exitCode, stderr, stdout } = spawnSync( + [bunExe(), import.meta.dir + "/esbuild-test.js"], + { + env: { + BUN_DEBUG_QUIET_LOGS: "1", + }, + }, + ); + const out = "" + stderr?.toString() + stdout?.toString(); + if (exitCode !== 0 && out?.length) { + throw new Error(out); + } - it("works for input exceeding the pipe capacity", async () => { - const hugeString = `console.log(${JSON.stringify("a".repeat(1000000))});`; - - for (let i = 0; i < 2; i++) { - const result = await transform(hugeString, { - loader: "js", - target: "node12", - }); - expect(result.code).toBe(hugeString + "\n"); - } - }); -}); - -describe("child_process.spawnSync - esbuild", () => { - it("should transform successfully", () => { - const result = transformSync("console.log('hello world')", { - loader: "js", - target: "node12", - }); - expect(result.code).toBe('console.log("hello world");\n'); - }); - - // This test is failing with the following error: - // error: Error - // path: "/Users/jarred/Code/bun/test/bun.js/node_modules/esbuild-darwin-arm64/bin/esbuild" - // code: "13" - // syscall: "spawnSync" - // errno: -1 - // it("works for input exceeding the pipe capacity", () => { - // const hugeString = `console.log(${JSON.stringify("a".repeat(100000))});`; - // const result = transformSync(hugeString, { - // loader: "js", - // target: "node12", - // }); - // expect(result.code).toBe(hugeString + "\n"); - // }); + expect(exitCode).toBe(0); }); diff --git a/test/bun.js/esbuild-test.js b/test/bun.js/esbuild-test.js new file mode 100644 index 000000000..beb34b283 --- /dev/null +++ b/test/bun.js/esbuild-test.js @@ -0,0 +1,37 @@ +import { transform, transformSync } from "esbuild"; + +{ + const result = await transform("console.log('hello world')", { + loader: "js", + target: "node12", + }); + if (result.code !== 'console.log("hello world");\n') { + throw new Error("Test failed."); + } +} + +{ + const hugeString = `console.log(${JSON.stringify("a".repeat(1000000))});`; + + for (let i = 0; i < 2; i++) { + const result = await transform(hugeString, { + loader: "js", + target: "node12", + }); + if (result.code !== hugeString + "\n") { + throw new Error("Test failed."); + } + } +} + +{ + const result = transformSync("console.log('hello world')", { + loader: "js", + target: "node12", + }); + if (result.code !== 'console.log("hello world");\n') { + throw new Error("Test failed."); + } +} + +process.exit(0); -- cgit v1.2.3 From 034dd3d03d05d5052e7c2ad1f17702583959e5e2 Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Thu, 12 Jan 2023 16:06:28 -0800 Subject: make the test more resilient --- test/bun.js/process.test.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'test/bun.js') diff --git a/test/bun.js/process.test.js b/test/bun.js/process.test.js index be627b61c..8ea57a28b 100644 --- a/test/bun.js/process.test.js +++ b/test/bun.js/process.test.js @@ -1,6 +1,7 @@ -import { resolveSync } from "bun"; +import { resolveSync, which } from "bun"; import { describe, expect, it } from "bun:test"; import { readFileSync, realpathSync } from "fs"; +import { basename } from "path"; it("process", () => { // this property isn't implemented yet but it should at least return a string @@ -106,11 +107,12 @@ it("process.version starts with v", () => { }); it("process.argv0", () => { - expect(process.argv0).toBe(process.argv[0]); + expect(basename(process.argv0)).toBe(basename(process.argv[0])); }); it("process.execPath", () => { - expect(process.execPath).toBe(realpathSync(process.argv0)); + expect(process.execPath).not.toBe(basename(process.argv0)); + expect(which(process.execPath)).not.toBeNull(); }); it("process.uptime()", () => { -- cgit v1.2.3