diff options
Diffstat (limited to 'test/js')
-rw-r--r-- | test/js/bun/test/__snapshots__/test-test.test.ts.snap | 44 | ||||
-rw-r--r-- | test/js/bun/test/expect.test.ts | 179 | ||||
-rw-r--r-- | test/js/bun/test/skip-test-fixture.js | 67 | ||||
-rw-r--r-- | test/js/bun/test/test-test.test.ts | 34 | ||||
-rw-r--r-- | test/js/bun/test/timeout-test-fixture.js | 11 | ||||
-rw-r--r-- | test/js/node/child_process/child_process-node.test.js | 22 | ||||
-rw-r--r-- | test/js/node/dns/node-dns.test.js | 5 |
7 files changed, 342 insertions, 20 deletions
diff --git a/test/js/bun/test/__snapshots__/test-test.test.ts.snap b/test/js/bun/test/__snapshots__/test-test.test.ts.snap index 31c5d310b..7fea0e8d3 100644 --- a/test/js/bun/test/__snapshots__/test-test.test.ts.snap +++ b/test/js/bun/test/__snapshots__/test-test.test.ts.snap @@ -1,3 +1,45 @@ // Bun Snapshot v1, https://goo.gl/fbAQLP -exports[`expect().toEqual() on objects with property indices doesn't print undefined 1`] = `"expect(received).toEqual(expected)\n\n {\n+ \"0\": 0,\n+ \"1\": 1,\n+ \"10\": 10,\n+ \"11\": 11,\n+ \"12\": 12,\n+ \"13\": 13,\n+ \"14\": 14,\n+ \"15\": 15,\n+ \"2\": 2,\n+ \"3\": 3,\n+ \"4\": 4,\n+ \"5\": 5,\n+ \"6\": 6,\n+ \"7\": 7,\n+ \"8\": 8,\n+ \"9\": 9\n- \"0\": 123,\n- \"1\": 123,\n- \"10\": 123,\n- \"11\": 123,\n- \"12\": 123,\n- \"13\": 123,\n- \"14\": 123,\n- \"15\": 123,\n- \"2\": 123,\n- \"3\": 123,\n- \"4\": 123,\n- \"5\": 123,\n- \"6\": 123,\n- \"7\": 123,\n- \"8\": 123,\n- \"9\": 123\n }\n\n- Expected - 16\n+ Received + 16\n\n "`; +exports[`expect().toEqual() on objects with property indices doesn't print undefined 1`] = ` +"expect(received).toEqual(expected) + + { ++ "0": 0, ++ "1": 1, ++ "10": 10, ++ "11": 11, ++ "12": 12, ++ "13": 13, ++ "14": 14, ++ "15": 15, ++ "2": 2, ++ "3": 3, ++ "4": 4, ++ "5": 5, ++ "6": 6, ++ "7": 7, ++ "8": 8, ++ "9": 9 +- "0": 123, +- "1": 123, +- "10": 123, +- "11": 123, +- "12": 123, +- "13": 123, +- "14": 123, +- "15": 123, +- "2": 123, +- "3": 123, +- "4": 123, +- "5": 123, +- "6": 123, +- "7": 123, +- "8": 123, +- "9": 123 + } + +- Expected - 16 ++ Received + 16 + + " +`; diff --git a/test/js/bun/test/expect.test.ts b/test/js/bun/test/expect.test.ts index 96896013b..2afb9726c 100644 --- a/test/js/bun/test/expect.test.ts +++ b/test/js/bun/test/expect.test.ts @@ -192,6 +192,185 @@ describe("expect()", () => { }); } }); + + test("toBeNil()", () => { + expect(null).toBeNil(); + expect(undefined).toBeNil(); + expect(false).not.toBeNil(); + expect(0).not.toBeNil(); + expect("").not.toBeNil(); + expect([]).not.toBeNil(); + expect(true).not.toBeNil(); + expect({}).not.toBeNil(); + }); + + test("toBeBoolean()", () => { + expect(true).toBeBoolean(); + expect(false).toBeBoolean(); + expect(0).not.toBeBoolean(); + expect(1).not.toBeBoolean(); + expect("").not.toBeBoolean(); + expect({}).not.toBeBoolean(); + }); + + test("toBeTrue()", () => { + expect(true).toBeTrue(); + expect(false).not.toBeTrue(); + expect(0).not.toBeTrue(); + expect(1).not.toBeTrue(); + expect("").not.toBeTrue(); + expect({}).not.toBeTrue(); + }); + + test("toBeFalse()", () => { + expect(false).toBeFalse(); + expect(true).not.toBeFalse(); + expect(0).not.toBeFalse(); + expect(1).not.toBeFalse(); + expect("").not.toBeFalse(); + expect({}).not.toBeFalse(); + }); + + test("toBeNumber()", () => { + expect(0).toBeNumber(); + expect(1).toBeNumber(); + expect(1.23).toBeNumber(); + expect(Infinity).toBeNumber(); + expect(-Infinity).toBeNumber(); + expect(NaN).toBeNumber(); + expect("").not.toBeNumber(); + expect({}).not.toBeNumber(); + }); + + test("toBeInteger()", () => { + expect(0).toBeInteger(); + expect(1).toBeInteger(); + expect(1.23).not.toBeInteger(); + expect(Infinity).not.toBeInteger(); + expect(-Infinity).not.toBeInteger(); + expect(NaN).not.toBeInteger(); + expect("").not.toBeInteger(); + expect({}).not.toBeInteger(); + }); + + test("toBeFinite()", () => { + expect(0).toBeFinite(); + expect(1).toBeFinite(); + expect(1.23).toBeFinite(); + expect(Infinity).not.toBeFinite(); + expect(-Infinity).not.toBeFinite(); + expect(NaN).not.toBeFinite(); + expect("").not.toBeFinite(); + expect({}).not.toBeFinite(); + }); + + test("toBePositive()", () => { + expect(1).toBePositive(); + expect(1.23).toBePositive(); + expect(Infinity).not.toBePositive(); + expect(0).not.toBePositive(); + expect(-Infinity).not.toBePositive(); + expect(NaN).not.toBePositive(); + expect("").not.toBePositive(); + expect({}).not.toBePositive(); + }); + + test("toBeNegative()", () => { + expect(-1).toBeNegative(); + expect(-1.23).toBeNegative(); + expect(-Infinity).not.toBeNegative(); + expect(0).not.toBeNegative(); + expect(Infinity).not.toBeNegative(); + expect(NaN).not.toBeNegative(); + expect("").not.toBeNegative(); + expect({}).not.toBeNegative(); + }); + + test("toBeWithin()", () => { + expect(0).toBeWithin(0, 1); + expect(3.14).toBeWithin(3, 3.141); + expect(-25).toBeWithin(-100, 0); + expect(0).not.toBeWithin(1, 2); + expect(3.14).not.toBeWithin(3.1, 3.14); + expect(99).not.toBeWithin(99, 99); + expect(100).not.toBeWithin(99, 100); + expect(NaN).not.toBeWithin(0, 1); + expect("").not.toBeWithin(0, 1); + expect({}).not.toBeWithin(0, 1); + expect(Infinity).not.toBeWithin(-Infinity, Infinity); + }); + + test("toBeSymbol()", () => { + expect(Symbol()).toBeSymbol(); + expect(Symbol("")).toBeSymbol(); + expect(Symbol.iterator).toBeSymbol(); + expect("").not.toBeSymbol(); + expect({}).not.toBeSymbol(); + }); + + test("toBeFunction()", () => { + expect(() => {}).toBeFunction(); + expect(function () {}).toBeFunction(); + expect(async function () {}).toBeFunction(); + expect(async () => {}).toBeFunction(); + expect(function* () {}).toBeFunction(); + expect(async function* () {}).toBeFunction(); + expect("").not.toBeFunction(); + expect({}).not.toBeFunction(); + expect(null).not.toBeFunction(); + }); + + test("toBeDate()", () => { + expect(new Date()).toBeDate(); + expect(new Date(0)).toBeDate(); + expect(new Date("2021-01-01")).toBeDate(); + expect("2021-01-01").not.toBeDate(); + expect({}).not.toBeDate(); + expect(null).not.toBeDate(); + }); + + test.todo("toBeValidDate()", () => { + expect(new Date()).toBeValidDate(); + expect(new Date(-1)).not.toBeValidDate(); + expect("2021-01-01").not.toBeValidDate(); + expect({}).not.toBeValidDate(); + expect(null).not.toBeValidDate(); + }); + + test("toBeString()", () => { + expect("").toBeString(); + expect("123").toBeString(); + expect(new String()).toBeString(); + expect(new String("123")).toBeString(); + expect(123).not.toBeString(); + expect({}).not.toBeString(); + }); + + test("toInclude()", () => { + expect("123").toInclude("1"); + expect("abc").toInclude("abc"); + expect(" 123 ").toInclude(" "); + expect("").toInclude(""); + expect("bob").not.toInclude("alice"); + }); + + test("toStartWith()", () => { + expect("123").toStartWith("1"); + expect("abc").toStartWith("abc"); + expect(" 123 ").toStartWith(" "); + expect(" ").toStartWith(""); + expect("").toStartWith(""); + expect("bob").not.toStartWith("alice"); + }); + + test("toEndWith()", () => { + expect("123").toEndWith("3"); + expect("abc").toEndWith("abc"); + expect(" 123 ").toEndWith(" "); + expect(" ").toEndWith(""); + expect("").toEndWith(""); + expect("bob").not.toEndWith("alice"); + }); }); function label(value: unknown): string { diff --git a/test/js/bun/test/skip-test-fixture.js b/test/js/bun/test/skip-test-fixture.js new file mode 100644 index 000000000..acb5f0748 --- /dev/null +++ b/test/js/bun/test/skip-test-fixture.js @@ -0,0 +1,67 @@ +import { test, describe } from "bun:test"; + +test.skip("test #1", () => { + console.log("unreachable"); +}); + +test.skipIf(true)("test #2", () => { + console.log("unreachable"); +}); + +test.skipIf(1)("test #3", () => { + console.log("unreachable"); +}); + +test.skipIf(false)("test #4", () => { + console.log("reachable"); +}); + +test.skipIf(null)("test #5", () => { + console.log("reachable"); +}); + +describe.skip("describe #1", () => { + test("test #6", () => { + console.log("unreachable"); + }); +}); + +describe.skipIf(true)("describe #2", () => { + test("test #7", () => { + console.log("unreachable"); + }); +}); + +describe.skipIf(1)("describe #3", () => { + test("test #8", () => { + console.log("unreachable"); + }); +}); + +describe.skipIf(false)("describe #4", () => { + test("test #9", () => { + console.log("reachable"); + }); +}); + +describe.skipIf(null)("describe #5", () => { + test("test #10", () => { + console.log("reachable"); + }); +}); + +test.if(false)("test #11", () => { + console.log("unreachable"); +}); + +test.if(null)("test #12", () => { + console.log("unreachable"); +}); + +test.if(true)("test #13", () => { + console.log("reachable"); +}); + +test.if(1)("test #14", () => { + console.log("reachable"); +}); diff --git a/test/js/bun/test/test-test.test.ts b/test/js/bun/test/test-test.test.ts index 4d31c9cb4..ed356aa50 100644 --- a/test/js/bun/test/test-test.test.ts +++ b/test/js/bun/test/test-test.test.ts @@ -1725,6 +1725,11 @@ test("toHaveProperty() - all", () => { expect({ a: new String("a") }).not.toHaveProperty("a", "a"); }); +test("toHaveProperty() - null or undefined", () => { + expect(null).not.toHaveProperty("length"); + expect(undefined).not.toHaveProperty("length"); +}); + test("toBe()", () => { const a = 1; const b = 1; @@ -2530,14 +2535,7 @@ describe("throw in describe scope doesn't enqueue tests after thrown", () => { throw new Error("This test failed"); }); - class TestPass extends Error { - constructor(message) { - super(message); - this.name = "TestPass"; - } - } - - throw new TestPass("This test passed. Ignore the error message"); + throw "This test passed. Ignore the error message"; it("test enqueued after a describe scope throws is never run", () => { throw new Error("This test failed"); @@ -2812,7 +2810,7 @@ it("test.todo", () => { const path = join(tmp, "todo-test.test.js"); copyFileSync(join(import.meta.dir, "todo-test-fixture.js"), path); const { stdout, stderr, exitCode } = spawnSync({ - cmd: [bunExe(), "test", path], + cmd: [bunExe(), "test", path, "--todo"], stdout: "pipe", stderr: "pipe", env: bunEnv, @@ -2833,7 +2831,7 @@ it("test.todo doesnt cause exit code 1", () => { const path = join(tmp, "todo-test.test.js"); copyFileSync(join(import.meta.dir, "todo-test-fixture-2.js"), path); const { stdout, stderr, exitCode } = spawnSync({ - cmd: [bunExe(), "test", path], + cmd: [bunExe(), "test", path, "--todo"], stdout: "pipe", stderr: "pipe", env: bunEnv, @@ -2919,3 +2917,19 @@ afterAll: #2 `.trim(), ); }); + +it("skip() and skipIf()", () => { + const path = join(tmp, "skip-test-fixture.test.js"); + copyFileSync(join(import.meta.dir, "skip-test-fixture.js"), path); + const { stdout } = spawnSync({ + cmd: [bunExe(), "test", path], + stdout: "pipe", + stderr: "pipe", + env: bunEnv, + cwd: realpathSync(dirname(path)), + }); + const result = stdout!.toString(); + expect(result).not.toContain("unreachable"); + expect(result).toMatch(/reachable/); + expect(result.match(/reachable/g)).toHaveLength(6); +}); diff --git a/test/js/bun/test/timeout-test-fixture.js b/test/js/bun/test/timeout-test-fixture.js index 82e887fb8..36a4ee2c6 100644 --- a/test/js/bun/test/timeout-test-fixture.js +++ b/test/js/bun/test/timeout-test-fixture.js @@ -7,6 +7,17 @@ test("test timeouts when expected", async () => { console.error("unreachable code"); }, 10); +test( + "test timeouts when expected 2", + async () => { + for (let i = 0; i < 100; i++) { + await Bun.sleep(1); + } + console.error("unreachable code"); + }, + { timeout: 10 }, +); + test("process doesn't hang on test with ref'd value", async () => { Bun.serve({ port: 0, diff --git a/test/js/node/child_process/child_process-node.test.js b/test/js/node/child_process/child_process-node.test.js index f69b8668f..b845beb1e 100644 --- a/test/js/node/child_process/child_process-node.test.js +++ b/test/js/node/child_process/child_process-node.test.js @@ -1,6 +1,7 @@ import { ChildProcess, spawn, exec } from "node:child_process"; import { createTest } from "node-harness"; import { tmpdir } from "node:os"; +import { bunExe } from "harness"; const { beforeAll, describe, expect, it, throws, assert, createCallCheckCtx, createDoneDotAll } = createTest( import.meta.path, ); @@ -176,14 +177,14 @@ describe("ChildProcess spawn bad stdio", () => { }; const { mustCall } = createCallCheckCtx(done); - let cmd = `bun ${import.meta.dir}/spawned-child.js`; + let cmd = `${bunExe()} ${import.meta.dir}/spawned-child.js`; if (target) cmd += " " + target; const child = exec(cmd, options, mustCall(callback)); ChildProcess.prototype.spawn = __originalSpawn; return child; } - it("should handle normal execution of child process", done => { + it.skip("should handle normal execution of child process", done => { createChild( {}, (err, stdout, stderr) => { @@ -195,21 +196,21 @@ describe("ChildProcess spawn bad stdio", () => { ); }); - it("should handle error event of child process", done => { + it.skip("should handle error event of child process", done => { const error = new Error(`Command failed: bun ${import.meta.dir}/spawned-child.js ERROR`); createChild( {}, (err, stdout, stderr) => { - strictEqual(err.message, error.message); strictEqual(stdout, ""); strictEqual(stderr, ""); + strictEqual(err?.message, error.message); }, done, "ERROR", ); }); - it("should handle killed process", done => { + it.skip("should handle killed process", done => { createChild( { timeout: 1 }, (err, stdout, stderr) => { @@ -352,9 +353,10 @@ describe("child_process cwd", () => { describe("child_process default options", () => { it("should use process.env as default env", done => { + const origTmpDir = globalThis.process.env.TMPDIR; globalThis.process.env.TMPDIR = platformTmpDir; - let child = spawn("printenv", [], {}); + globalThis.process.env.TMPDIR = origTmpDir; let response = ""; child.stdout.setEncoding("utf8"); @@ -366,8 +368,12 @@ describe("child_process default options", () => { // NOTE: Original test used child.on("exit"), but this is unreliable // because the process can exit before the stream is closed and the data is read child.stdout.on("close", () => { - expect(response.includes(`TMPDIR=${platformTmpDir}`)).toBe(true); - done(); + try { + expect(response).toContain(`TMPDIR=${platformTmpDir}`); + done(); + } catch (e) { + done(e); + } }); }); }); diff --git a/test/js/node/dns/node-dns.test.js b/test/js/node/dns/node-dns.test.js index 754ed2ffc..5fb8e0739 100644 --- a/test/js/node/dns/node-dns.test.js +++ b/test/js/node/dns/node-dns.test.js @@ -57,7 +57,10 @@ test("dns.resolveSoa (bun.sh)", done => { expect(result.refresh).toBe(10000); expect(result.retry).toBe(2400); expect(result.expire).toBe(604800); - expect(result.minttl).toBe(3600); + + // Cloudflare might randomly change min TTL + expect(result.minttl).toBeNumber(); + expect(result.nsname).toBe("hans.ns.cloudflare.com"); expect(result.hostmaster).toBe("dns.cloudflare.com"); done(err); |