diff options
Diffstat (limited to 'tests/specs')
14 files changed, 681 insertions, 0 deletions
diff --git a/tests/specs/node_compat_tests/node_process_beforeexit_exit_events_emitted_without_listeners/__test__.jsonc b/tests/specs/node_compat_tests/node_process_beforeexit_exit_events_emitted_without_listeners/__test__.jsonc new file mode 100644 index 000000000..0f3b33300 --- /dev/null +++ b/tests/specs/node_compat_tests/node_process_beforeexit_exit_events_emitted_without_listeners/__test__.jsonc @@ -0,0 +1,5 @@ +{ + "args": "run process_beforeexit_exit_events.ts", + "output": "process_beforeexit_exit_events.out", + "exitCode": 0 +} diff --git a/tests/specs/node_compat_tests/node_process_beforeexit_exit_events_emitted_without_listeners/process_beforeexit_exit_events.out b/tests/specs/node_compat_tests/node_process_beforeexit_exit_events_emitted_without_listeners/process_beforeexit_exit_events.out new file mode 100644 index 000000000..740ef6ffb --- /dev/null +++ b/tests/specs/node_compat_tests/node_process_beforeexit_exit_events_emitted_without_listeners/process_beforeexit_exit_events.out @@ -0,0 +1,2 @@ +beforeExit emitted from processEmit +exit emitted from processEmit diff --git a/tests/specs/node_compat_tests/node_process_beforeexit_exit_events_emitted_without_listeners/process_beforeexit_exit_events.ts b/tests/specs/node_compat_tests/node_process_beforeexit_exit_events_emitted_without_listeners/process_beforeexit_exit_events.ts new file mode 100644 index 000000000..a4c87f27e --- /dev/null +++ b/tests/specs/node_compat_tests/node_process_beforeexit_exit_events_emitted_without_listeners/process_beforeexit_exit_events.ts @@ -0,0 +1,9 @@ +import process from "node:process"; + +const originalEmit = process.emit; +process.emit = function (event, ...args) { + if (event === "exit" || event === "beforeExit") { + console.log(`${event} emitted from processEmit`); + } + return originalEmit.call(this, event, ...args); +}; diff --git a/tests/specs/node_compat_tests/node_test_module/__test__.jsonc b/tests/specs/node_compat_tests/node_test_module/__test__.jsonc new file mode 100644 index 000000000..ef93b6673 --- /dev/null +++ b/tests/specs/node_compat_tests/node_test_module/__test__.jsonc @@ -0,0 +1,9 @@ +{ + "args": "test test.js", + "output": "test.out", + "envs": { + "NO_COLOR": "1", + "NPM_CONFIG_REGISTRY": "http://localhost:4260/" + }, + "exitCode": 1 +} diff --git a/tests/specs/node_compat_tests/node_test_module/test.js b/tests/specs/node_compat_tests/node_test_module/test.js new file mode 100644 index 000000000..0f0f9b6b6 --- /dev/null +++ b/tests/specs/node_compat_tests/node_test_module/test.js @@ -0,0 +1,390 @@ +// Copyright Joyent, Inc. and other Node contributors. + +// Ported from https://github.com/nodejs/node/blob/d396a041f71cc055ad60b0abc63ad81c0ee6a574/test/fixtures/test-runner/output/output.js + +// deno-lint-ignore-file + +import assert from "node:assert"; +import test from "node:test"; +import util from "node:util"; +import { setImmediate } from "node:timers"; + +test("sync pass todo", (t) => { + t.todo(); +}); + +test("sync pass todo with message", (t) => { + t.todo("this is a passing todo"); +}); + +test("sync fail todo", (t) => { + t.todo(); + throw new Error("thrown from sync fail todo"); +}); + +test("sync fail todo with message", (t) => { + t.todo("this is a failing todo"); + throw new Error("thrown from sync fail todo with message"); +}); + +test("sync skip pass", (t) => { + t.skip(); +}); + +test("sync skip pass with message", (t) => { + t.skip("this is skipped"); +}); + +test("sync pass", (t) => { + t.diagnostic("this test should pass"); +}); + +test("sync throw fail", () => { + throw new Error("thrown from sync throw fail"); +}); + +test("async skip pass", async (t) => { + t.skip(); +}); + +test("async pass", async () => { +}); + +test("async throw fail", async () => { + throw new Error("thrown from async throw fail"); +}); + +test("nested test", async (t) => { + await t.test("nested 1", async (t) => { + await t.test("nested 2", () => { + }); + }); +}); + +test("async skip fail", async (t) => { + t.skip(); + throw new Error("thrown from async throw fail"); +}); + +test("async assertion fail", async () => { + // Make sure the assert module is handled. + assert.strictEqual(true, false); +}); + +test("resolve pass", () => { + return Promise.resolve(); +}); + +test("reject fail", () => { + return Promise.reject(new Error("rejected from reject fail")); +}); + +test("unhandled rejection - passes but warns", () => { + Promise.reject(new Error("rejected from unhandled rejection fail")); +}); + +test("async unhandled rejection - passes but warns", async () => { + Promise.reject(new Error("rejected from async unhandled rejection fail")); +}); + +test("immediate throw - passes but warns", () => { + setImmediate(() => { + throw new Error("thrown from immediate throw fail"); + }); +}); + +test("immediate reject - passes but warns", () => { + setImmediate(() => { + Promise.reject(new Error("rejected from immediate reject fail")); + }); +}); + +test("immediate resolve pass", () => { + return new Promise((resolve) => { + setImmediate(() => { + resolve(); + }); + }); +}); + +test("subtest sync throw fail", async (t) => { + await t.test("+sync throw fail", (t) => { + t.diagnostic("this subtest should make its parent test fail"); + throw new Error("thrown from subtest sync throw fail"); + }); +}); + +test("sync throw non-error fail", async (t) => { + throw Symbol("thrown symbol from sync throw non-error fail"); +}); + +test("level 0a", { concurrency: 4 }, async (t) => { + t.test("level 1a", async (t) => { + const p1a = new Promise((resolve) => { + setTimeout(() => { + resolve(); + }, 100); + }); + + return p1a; + }); + + test("level 1b", async (t) => { + const p1b = new Promise((resolve) => { + resolve(); + }); + + return p1b; + }); + + t.test("level 1c", async (t) => { + const p1c = new Promise((resolve) => { + setTimeout(() => { + resolve(); + }, 200); + }); + + return p1c; + }); + + t.test("level 1d", async (t) => { + const p1c = new Promise((resolve) => { + setTimeout(() => { + resolve(); + }, 150); + }); + + return p1c; + }); + + const p0a = new Promise((resolve) => { + setTimeout(() => { + resolve(); + }, 300); + }); + + return p0a; +}); + +test("top level", { concurrency: 2 }, async (t) => { + t.test("+long running", async (t) => { + return new Promise((resolve, reject) => { + setTimeout(resolve, 300).unref(); + }); + }); + + t.test("+short running", async (t) => { + t.test("++short running", async (t) => {}); + }); +}); + +test("invalid subtest - pass but subtest fails", (t) => { + setImmediate(() => { + t.test("invalid subtest fail", () => { + throw new Error("this should not be thrown"); + }); + }); +}); + +test("sync skip option", { skip: true }, (t) => { + throw new Error("this should not be executed"); +}); + +test("sync skip option with message", { skip: "this is skipped" }, (t) => { + throw new Error("this should not be executed"); +}); + +test("sync skip option is false fail", { skip: false }, (t) => { + throw new Error("this should be executed"); +}); + +// A test with no arguments provided. +test(); + +// A test with only a named function provided. +test(function functionOnly() {}); + +// A test with only an anonymous function provided. +test(() => {}); + +// A test with only a name provided. +test("test with only a name provided"); + +// A test with an empty string name. +test(""); + +// A test with only options provided. +test({ skip: true }); + +// A test with only a name and options provided. +test("test with a name and options provided", { skip: true }); + +// A test with only options and a function provided. +test({ skip: true }, function functionAndOptions() {}); + +// A test whose description needs to be escaped. +// test("escaped description \\ # \\#\\ \n \t \f \v \b \r"); + +// A test whose skip message needs to be escaped. +test("escaped skip message", { skip: "#skip" }); + +// A test whose todo message needs to be escaped. +test("escaped todo message", { todo: "#todo" }); + +// A test with a diagnostic message that needs to be escaped. +test("escaped diagnostic", (t) => { + t.diagnostic("#diagnostic"); +}); + +test("callback pass", (t, done) => { + setImmediate(done); +}); + +test("callback fail", (t, done) => { + setImmediate(() => { + done(new Error("callback failure")); + }); +}); + +test("sync t is this in test", function (t) { + assert.strictEqual(this, t); +}); + +test("async t is this in test", async function (t) { + assert.strictEqual(this, t); +}); + +test("callback t is this in test", function (t, done) { + assert.strictEqual(this, t); + done(); +}); + +test("callback also returns a Promise", async (t, done) => { + throw new Error("thrown from callback also returns a Promise"); +}); + +test("callback throw", (t, done) => { + throw new Error("thrown from callback throw"); +}); + +test("callback called twice", (t, done) => { + done(); + done(); +}); + +test("callback called twice in different ticks", (t, done) => { + setImmediate(done); + done(); +}); + +test("callback called twice in future tick", (t, done) => { + setImmediate(() => { + done(); + done(); + }); +}); + +test("callback async throw", (t, done) => { + setImmediate(() => { + throw new Error("thrown from callback async throw"); + }); +}); + +test("callback async throw after done", (t, done) => { + setImmediate(() => { + throw new Error("thrown from callback async throw after done"); + }); + + done(); +}); + +test("custom inspect symbol fail", () => { + const obj = { + [util.inspect.custom]() { + return "customized"; + }, + foo: 1, + }; + + throw obj; +}); + +test("custom inspect symbol that throws fail", () => { + const obj = { + [util.inspect.custom]() { + throw new Error("bad-inspect"); + }, + foo: 1, + }; + + throw obj; +}); + +test("subtest sync throw fails", async (t) => { + await t.test("sync throw fails at first", (t) => { + throw new Error("thrown from subtest sync throw fails at first"); + }); + await t.test("sync throw fails at second", (t) => { + throw new Error("thrown from subtest sync throw fails at second"); + }); +}); + +test("timed out async test", { timeout: 5 }, async (t) => { + return new Promise((resolve) => { + setTimeout(resolve, 100); + }); +}); + +test("timed out callback test", { timeout: 5 }, (t, done) => { + setTimeout(done, 100); +}); + +test("large timeout async test is ok", { timeout: 30_000_000 }, async (t) => { + return new Promise((resolve) => { + setTimeout(resolve, 10); + }); +}); + +test( + "large timeout callback test is ok", + { timeout: 30_000_000 }, + (t, done) => { + setTimeout(done, 10); + }, +); + +test("successful thenable", () => { + let thenCalled = false; + return { + get then() { + if (thenCalled) throw new Error(); + thenCalled = true; + return (successHandler) => successHandler(); + }, + }; +}); + +test("rejected thenable", () => { + let thenCalled = false; + return { + get then() { + if (thenCalled) throw new Error(); + thenCalled = true; + return (_, errorHandler) => errorHandler("custom error"); + }, + }; +}); + +test("unfinished test with uncaughtException", async () => { + await new Promise(() => { + setTimeout(() => { + throw new Error("foo"); + }); + }); +}); + +test("unfinished test with unhandledRejection", async () => { + await new Promise(() => { + setTimeout(() => Promise.reject(new Error("bar"))); + }); +}); diff --git a/tests/specs/node_compat_tests/node_test_module/test.out b/tests/specs/node_compat_tests/node_test_module/test.out new file mode 100644 index 000000000..c37fb355f --- /dev/null +++ b/tests/specs/node_compat_tests/node_test_module/test.out @@ -0,0 +1,175 @@ +[WILDCARD] +running 63 tests from ./test.js +sync pass todo ... +------- output ------- +Warning: Not implemented: test.TestContext.todo +----- output end ----- +sync pass todo ... ok [WILDCARD] +sync pass todo with message ... +------- output ------- +Warning: Not implemented: test.TestContext.todo +----- output end ----- +sync pass todo with message ... ok [WILDCARD] +sync fail todo ... +------- output ------- +Warning: Not implemented: test.TestContext.todo +----- output end ----- +sync fail todo ... FAILED [WILDCARD] +sync fail todo with message ... +------- output ------- +Warning: Not implemented: test.TestContext.todo +----- output end ----- +sync fail todo with message ... FAILED [WILDCARD] +sync skip pass ... +------- output ------- +Warning: Not implemented: test.TestContext.skip +----- output end ----- +sync skip pass ... ok [WILDCARD] +sync skip pass with message ... +------- output ------- +Warning: Not implemented: test.TestContext.skip +----- output end ----- +sync skip pass with message ... ok [WILDCARD] +sync pass ... +------- output ------- +DIAGNOSTIC: this test should pass +----- output end ----- +sync pass ... ok [WILDCARD] +sync throw fail ... FAILED [WILDCARD] +async skip pass ... +------- output ------- +Warning: Not implemented: test.TestContext.skip +----- output end ----- +async skip pass ... ok [WILDCARD] +async pass ... ok [WILDCARD] +async throw fail ... FAILED [WILDCARD] +nested test ... + nested 1 ... + nested 2 ... ok [WILDCARD] + nested 1 ... ok [WILDCARD] +nested test ... ok [WILDCARD] +async skip fail ... +------- output ------- +Warning: Not implemented: test.TestContext.skip +----- output end ----- +async skip fail ... FAILED [WILDCARD] +async assertion fail ... FAILED [WILDCARD] +resolve pass ... ok [WILDCARD] +reject fail ... FAILED [WILDCARD] +unhandled rejection - passes but warns ... +Uncaught error from ./test.js FAILED +unhandled rejection - passes but warns ... cancelled ([WILDCARD]) +async unhandled rejection - passes but warns ... cancelled ([WILDCARD]) +immediate throw - passes but warns ... cancelled ([WILDCARD]) +immediate reject - passes but warns ... cancelled ([WILDCARD]) +immediate resolve pass ... cancelled ([WILDCARD]) +subtest sync throw fail ... cancelled ([WILDCARD]) +sync throw non-error fail ... cancelled ([WILDCARD]) +level 0a ... cancelled ([WILDCARD]) +top level ... cancelled ([WILDCARD]) +invalid subtest - pass but subtest fails ... cancelled ([WILDCARD]) +sync skip option ... ignored ([WILDCARD]) +sync skip option with message ... cancelled ([WILDCARD]) +sync skip option is false fail ... cancelled ([WILDCARD]) +noop ... cancelled ([WILDCARD]) +functionOnly ... cancelled ([WILDCARD]) +<anonymous> ... cancelled ([WILDCARD]) +test with only a name provided ... cancelled ([WILDCARD]) +noop ... cancelled ([WILDCARD]) +noop ... ignored ([WILDCARD]) +test with a name and options provided ... ignored ([WILDCARD]) +functionAndOptions ... ignored ([WILDCARD]) +escaped skip message ... cancelled ([WILDCARD]) +escaped todo message ... cancelled ([WILDCARD]) +escaped diagnostic ... cancelled ([WILDCARD]) +callback pass ... cancelled ([WILDCARD]) +callback fail ... cancelled ([WILDCARD]) +sync t is this in test ... cancelled ([WILDCARD]) +async t is this in test ... cancelled ([WILDCARD]) +callback t is this in test ... cancelled ([WILDCARD]) +callback also returns a Promise ... cancelled ([WILDCARD]) +callback throw ... cancelled ([WILDCARD]) +callback called twice ... cancelled ([WILDCARD]) +callback called twice in different ticks ... cancelled ([WILDCARD]) +callback called twice in future tick ... cancelled ([WILDCARD]) +callback async throw ... cancelled ([WILDCARD]) +callback async throw after done ... cancelled ([WILDCARD]) +custom inspect symbol fail ... cancelled ([WILDCARD]) +custom inspect symbol that throws fail ... cancelled ([WILDCARD]) +subtest sync throw fails ... cancelled ([WILDCARD]) +timed out async test ... cancelled ([WILDCARD]) +timed out callback test ... cancelled ([WILDCARD]) +large timeout async test is ok ... cancelled ([WILDCARD]) +large timeout callback test is ok ... cancelled ([WILDCARD]) +successful thenable ... cancelled ([WILDCARD]) +rejected thenable ... cancelled ([WILDCARD]) +unfinished test with uncaughtException ... cancelled ([WILDCARD]) +unfinished test with unhandledRejection ... cancelled ([WILDCARD]) + + ERRORS + +sync fail todo => ./test.js:20:1 +error: Error: thrown from sync fail todo + throw new Error("thrown from sync fail todo"); +[WILDCARD] + +sync fail todo with message => ./test.js:25:1 +error: Error: thrown from sync fail todo with message + throw new Error("thrown from sync fail todo with message"); +[WILDCARD] + +sync throw fail => ./test.js:42:1 +error: Error: thrown from sync throw fail + throw new Error("thrown from sync throw fail"); +[WILDCARD] + +async throw fail => ./test.js:53:1 +error: Error: thrown from async throw fail + throw new Error("thrown from async throw fail"); +[WILDCARD] + +async skip fail => ./test.js:64:1 +error: Error: thrown from async throw fail + throw new Error("thrown from async throw fail"); +[WILDCARD] + +async assertion fail => ./test.js:69:1 +error: AssertionError: Values are not strictly equal: + + + [Diff] Actual / Expected + + +- true ++ false + + at [WILDCARD] + +reject fail => ./test.js:78:1 +error: Error: rejected from reject fail + return Promise.reject(new Error("rejected from reject fail")); + ^ + at [WILDCARD] + +./test.js (uncaught error) +error: (in promise) Error: rejected from unhandled rejection fail + Promise.reject(new Error("rejected from unhandled rejection fail")); + ^ + at [WILDCARD] +This error was not caught from a test and caused the test runner to fail on the referenced module. +It most likely originated from a dangling promise, event/timeout handler or top-level code. + + FAILURES + +sync fail todo => ./test.js:20:1 +sync fail todo with message => ./test.js:25:1 +sync throw fail => ./test.js:42:1 +async throw fail => ./test.js:53:1 +async skip fail => ./test.js:64:1 +async assertion fail => ./test.js:69:1 +reject fail => ./test.js:78:1 +./test.js (uncaught error) + +FAILED | 9 passed (2 steps) | 51 failed | 4 ignored [WILDCARD] + +error: Test failed diff --git a/tests/specs/node_compat_tests/node_test_module_no_sanitizers/__test__.jsonc b/tests/specs/node_compat_tests/node_test_module_no_sanitizers/__test__.jsonc new file mode 100644 index 000000000..0a9a9524b --- /dev/null +++ b/tests/specs/node_compat_tests/node_test_module_no_sanitizers/__test__.jsonc @@ -0,0 +1,9 @@ +{ + "args": "test -A --no-check test_no_sanitizers/test.js", + "output": "test_no_sanitizers/test.out", + "envs": { + "NO_COLOR": "1", + "NPM_CONFIG_REGISTRY": "http://localhost:4260/" + }, + "exitCode": 0 +} diff --git a/tests/specs/node_compat_tests/node_test_module_no_sanitizers/test_no_sanitizers/cat.ts b/tests/specs/node_compat_tests/node_test_module_no_sanitizers/test_no_sanitizers/cat.ts new file mode 100644 index 000000000..62c82ebca --- /dev/null +++ b/tests/specs/node_compat_tests/node_test_module_no_sanitizers/test_no_sanitizers/cat.ts @@ -0,0 +1,4 @@ +const filename = Deno.args[0]; +using file = await Deno.open(filename); + +await file.readable.pipeTo(Deno.stdout.writable); diff --git a/tests/specs/node_compat_tests/node_test_module_no_sanitizers/test_no_sanitizers/test.js b/tests/specs/node_compat_tests/node_test_module_no_sanitizers/test_no_sanitizers/test.js new file mode 100644 index 000000000..52d0f1325 --- /dev/null +++ b/tests/specs/node_compat_tests/node_test_module_no_sanitizers/test_no_sanitizers/test.js @@ -0,0 +1,28 @@ +import test from "node:test"; +test("should not complain about resource and op sanitizers", async (t) => { + // resource + const _file1 = Deno.open("test_no_sanitizers/welcome.ts"); + + await t.test("nested test", () => { + // resource + const _file2 = Deno.open("test_no_sanitizers/cat.ts"); + + // op + crypto.subtle.digest( + "SHA-256", + new TextEncoder().encode("a".repeat(1_000_000)), + ); + }); + + // op + crypto.subtle.digest( + "SHA-256", + new TextEncoder().encode("a".repeat(1_000_000)), + ); +}); + +// TODO(mmastrac): This works, but we don't reliably flush stdout/stderr here, making this test flake +// test("should allow exit", () => { +// // no exit sanitizers +// Deno.exit(123); +// }); diff --git a/tests/specs/node_compat_tests/node_test_module_no_sanitizers/test_no_sanitizers/test.out b/tests/specs/node_compat_tests/node_test_module_no_sanitizers/test_no_sanitizers/test.out new file mode 100644 index 000000000..dc5ab7cfd --- /dev/null +++ b/tests/specs/node_compat_tests/node_test_module_no_sanitizers/test_no_sanitizers/test.out @@ -0,0 +1,7 @@ +running 1 test from ./test_no_sanitizers/test.js +should not complain about resource and op sanitizers ... + nested test ... ok ([WILDCARD]) +should not complain about resource and op sanitizers ... ok ([WILDCARD]) + +ok | 1 passed (1 step) | 0 failed ([WILDCARD]) + diff --git a/tests/specs/node_compat_tests/node_test_module_no_sanitizers/test_no_sanitizers/welcome.ts b/tests/specs/node_compat_tests/node_test_module_no_sanitizers/test_no_sanitizers/welcome.ts new file mode 100644 index 000000000..f983ca89b --- /dev/null +++ b/tests/specs/node_compat_tests/node_test_module_no_sanitizers/test_no_sanitizers/welcome.ts @@ -0,0 +1 @@ +console.log("Welcome to Deno!"); diff --git a/tests/specs/node_compat_tests/web_node_events_dispatched_in_correct_order/__test__.jsonc b/tests/specs/node_compat_tests/web_node_events_dispatched_in_correct_order/__test__.jsonc new file mode 100644 index 000000000..cb5e48878 --- /dev/null +++ b/tests/specs/node_compat_tests/web_node_events_dispatched_in_correct_order/__test__.jsonc @@ -0,0 +1,5 @@ +{ + "args": "run events_order.ts", + "output": "events_order.out", + "exitCode": 0 +} diff --git a/tests/specs/node_compat_tests/web_node_events_dispatched_in_correct_order/events_order.out b/tests/specs/node_compat_tests/web_node_events_dispatched_in_correct_order/events_order.out new file mode 100644 index 000000000..270384d8d --- /dev/null +++ b/tests/specs/node_compat_tests/web_node_events_dispatched_in_correct_order/events_order.out @@ -0,0 +1,12 @@ +beforeunload emitted from addEventListener +beforeunload emitted from addEventListener +beforeunload emitted from addEventListener +beforeExit emitted from process.on +more work done! 1 +beforeunload emitted from addEventListener +beforeExit emitted from process.on +more work done! 2 +beforeunload emitted from addEventListener +beforeExit emitted from process.on +unload emitted from addEventListener +exit emitted from process.on diff --git a/tests/specs/node_compat_tests/web_node_events_dispatched_in_correct_order/events_order.ts b/tests/specs/node_compat_tests/web_node_events_dispatched_in_correct_order/events_order.ts new file mode 100644 index 000000000..263f46b4c --- /dev/null +++ b/tests/specs/node_compat_tests/web_node_events_dispatched_in_correct_order/events_order.ts @@ -0,0 +1,25 @@ +import process from "node:process"; + +let count = 0; +process.on("beforeExit", () => { + if (count === 0 || count === 1) { + setTimeout(() => console.log("more work done!", count), 10); + } + count++; + console.log("beforeExit emitted from process.on"); +}); +process.on("exit", () => console.log("exit emitted from process.on")); + +let countWeb = 0; +addEventListener("beforeunload", (event) => { + if (countWeb == 0 || countWeb == 1) { + event.preventDefault(); + } + countWeb++; + console.log("beforeunload emitted from addEventListener"); +}); + +addEventListener( + "unload", + () => console.log("unload emitted from addEventListener"), +); |