diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/bun.js/bun-test/nested-describes.test.ts | 38 | ||||
-rw-r--r-- | test/bun.js/bun-write.test.js | 40 | ||||
-rw-r--r-- | test/bun.js/child_process-node.test.js | 34 | ||||
-rw-r--r-- | test/bun.js/esbuild-child_process.test.ts | 63 | ||||
-rw-r--r-- | test/bun.js/esbuild-test.js | 37 | ||||
-rw-r--r-- | test/bun.js/fetch.test.js | 44 | ||||
-rw-r--r-- | test/bun.js/inspect.test.js | 37 | ||||
-rw-r--r-- | test/bun.js/process.test.js | 8 | ||||
-rw-r--r-- | test/bun.js/socket/socket.test.ts | 7 | ||||
-rw-r--r-- | test/bun.js/transpiler.test.js | 24 | ||||
-rw-r--r-- | test/bun.js/websocket-server.test.ts | 52 |
11 files changed, 248 insertions, 136 deletions
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); + }) + }) + }) +}) 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("<blink>it worked!</blink>", { html: true }); -// }, -// }); -// globalThis["HTMLRewriter.a"] = Bun.write( -// "/tmp/html-rewriter.txt.js", -// "<div>hello</div>", -// ); -// 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( -// "<div><blink>it worked!</blink></div>", -// ); -// done(); -// }); + rewriter.on("div", { + element(element) { + element.setInnerContent("<blink>it worked!</blink>", { html: true }); + }, + }); + await Bun.write("/tmp/html-rewriter.txt.js", "<div>hello</div>"); + 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( + "<div><blink>it worked!</blink></div>", + ); + done(); +}); 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()); 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); 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", () => { diff --git a/test/bun.js/inspect.test.js b/test/bun.js/inspect.test.js index 8789b7aba..738442211 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" @@ -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"); 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()", () => { 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", () => { 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 @@ -915,6 +915,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( ` 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<void>((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"); }, |