2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-08-01 19:17:38 -04:00
|
|
|
|
|
|
|
// TODO(petamoriken): enable prefer-primordials for node polyfills
|
|
|
|
// deno-lint-ignore-file prefer-primordials
|
|
|
|
|
|
|
|
import { notImplemented, warnNotImplemented } from "ext:deno_node/_utils.ts";
|
|
|
|
|
|
|
|
export function run() {
|
|
|
|
notImplemented("test.run");
|
|
|
|
}
|
|
|
|
|
|
|
|
function noop() {}
|
|
|
|
|
|
|
|
class NodeTestContext {
|
|
|
|
#denoContext: Deno.TestContext;
|
|
|
|
|
|
|
|
constructor(t: Deno.TestContext) {
|
|
|
|
this.#denoContext = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
get signal() {
|
|
|
|
notImplemented("test.TestContext.signal");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
get name() {
|
|
|
|
notImplemented("test.TestContext.name");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
diagnostic(message) {
|
|
|
|
console.log("DIAGNOSTIC:", message);
|
|
|
|
}
|
|
|
|
|
|
|
|
get mock() {
|
|
|
|
notImplemented("test.TestContext.mock");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
runOnly() {
|
|
|
|
notImplemented("test.TestContext.runOnly");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
skip() {
|
|
|
|
warnNotImplemented("test.TestContext.skip");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
todo() {
|
|
|
|
warnNotImplemented("test.TestContext.todo");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
test(name, options, fn) {
|
|
|
|
const prepared = prepareOptions(name, options, fn, {});
|
|
|
|
return this.#denoContext.step({
|
|
|
|
name: prepared.name,
|
2023-12-29 10:18:08 -05:00
|
|
|
fn: async (denoTestContext) => {
|
|
|
|
const newNodeTextContext = new NodeTestContext(denoTestContext);
|
|
|
|
await prepared.fn(newNodeTextContext);
|
|
|
|
},
|
2023-08-01 19:17:38 -04:00
|
|
|
ignore: prepared.options.todo || prepared.options.skip,
|
2024-02-20 23:22:07 -05:00
|
|
|
sanitizeExit: false,
|
|
|
|
sanitizeOps: false,
|
|
|
|
sanitizeResources: false,
|
2023-08-01 19:17:38 -04:00
|
|
|
}).then(() => undefined);
|
|
|
|
}
|
|
|
|
|
|
|
|
before(_fn, _options) {
|
|
|
|
notImplemented("test.TestContext.before");
|
|
|
|
}
|
|
|
|
|
|
|
|
after(_fn, _options) {
|
|
|
|
notImplemented("test.TestContext.after");
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(_fn, _options) {
|
|
|
|
notImplemented("test.TestContext.beforeEach");
|
|
|
|
}
|
|
|
|
|
|
|
|
afterEach(_fn, _options) {
|
|
|
|
notImplemented("test.TestContext.afterEach");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function prepareOptions(name, options, fn, overrides) {
|
|
|
|
if (typeof name === "function") {
|
|
|
|
fn = name;
|
|
|
|
} else if (name !== null && typeof name === "object") {
|
|
|
|
fn = options;
|
|
|
|
options = name;
|
|
|
|
} else if (typeof options === "function") {
|
|
|
|
fn = options;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options === null || typeof options !== "object") {
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
const finalOptions = { ...options, ...overrides };
|
2023-08-02 11:11:04 -04:00
|
|
|
// TODO(bartlomieju): these options are currently not handled
|
|
|
|
// const { concurrency, timeout, signal } = finalOptions;
|
2023-08-01 19:17:38 -04:00
|
|
|
|
|
|
|
if (typeof fn !== "function") {
|
|
|
|
fn = noop;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof name !== "string" || name === "") {
|
|
|
|
name = fn.name || "<anonymous>";
|
|
|
|
}
|
|
|
|
|
|
|
|
return { fn, options: finalOptions, name };
|
|
|
|
}
|
|
|
|
|
2023-11-22 06:11:20 -05:00
|
|
|
function wrapTestFn(fn, resolve) {
|
2023-08-01 19:17:38 -04:00
|
|
|
return async function (t) {
|
|
|
|
const nodeTestContext = new NodeTestContext(t);
|
|
|
|
try {
|
|
|
|
await fn(nodeTestContext);
|
|
|
|
} finally {
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve();
|
2023-08-01 19:17:38 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function prepareDenoTest(name, options, fn, overrides) {
|
|
|
|
const prepared = prepareOptions(name, options, fn, overrides);
|
|
|
|
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers();
|
2023-08-01 19:17:38 -04:00
|
|
|
|
|
|
|
const denoTestOptions = {
|
|
|
|
name: prepared.name,
|
2023-11-22 06:11:20 -05:00
|
|
|
fn: wrapTestFn(prepared.fn, resolve),
|
2023-08-01 19:17:38 -04:00
|
|
|
only: prepared.options.only,
|
|
|
|
ignore: prepared.options.todo || prepared.options.skip,
|
2024-02-20 23:22:07 -05:00
|
|
|
sanitizeExit: false,
|
|
|
|
sanitizeOps: false,
|
|
|
|
sanitizeResources: false,
|
2023-08-01 19:17:38 -04:00
|
|
|
};
|
|
|
|
Deno.test(denoTestOptions);
|
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function test(name, options, fn) {
|
|
|
|
return prepareDenoTest(name, options, fn, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
test.skip = function skip(name, options, fn) {
|
|
|
|
return prepareDenoTest(name, options, fn, { skip: true });
|
|
|
|
};
|
|
|
|
|
|
|
|
test.todo = function todo(name, options, fn) {
|
|
|
|
return prepareDenoTest(name, options, fn, { todo: true });
|
|
|
|
};
|
|
|
|
|
|
|
|
test.only = function only(name, options, fn) {
|
|
|
|
return prepareDenoTest(name, options, fn, { only: true });
|
|
|
|
};
|
|
|
|
|
|
|
|
export function describe() {
|
|
|
|
notImplemented("test.describe");
|
|
|
|
}
|
|
|
|
|
|
|
|
export function it() {
|
|
|
|
notImplemented("test.it");
|
|
|
|
}
|
|
|
|
|
|
|
|
export function before() {
|
|
|
|
notImplemented("test.before");
|
|
|
|
}
|
|
|
|
|
|
|
|
export function after() {
|
|
|
|
notImplemented("test.after");
|
|
|
|
}
|
|
|
|
|
|
|
|
export function beforeEach() {
|
|
|
|
notImplemented("test.beforeEach");
|
|
|
|
}
|
|
|
|
|
|
|
|
export function afterEach() {
|
|
|
|
notImplemented("test.afterEach");
|
|
|
|
}
|
|
|
|
|
|
|
|
export const mock = {
|
|
|
|
fn: () => {
|
|
|
|
notImplemented("test.mock.fn");
|
|
|
|
},
|
|
|
|
getter: () => {
|
|
|
|
notImplemented("test.mock.getter");
|
|
|
|
},
|
|
|
|
method: () => {
|
|
|
|
notImplemented("test.mock.method");
|
|
|
|
},
|
|
|
|
reset: () => {
|
|
|
|
notImplemented("test.mock.reset");
|
|
|
|
},
|
|
|
|
restoreAll: () => {
|
|
|
|
notImplemented("test.mock.restoreAll");
|
|
|
|
},
|
|
|
|
setter: () => {
|
|
|
|
notImplemented("test.mock.setter");
|
|
|
|
},
|
|
|
|
timers: {
|
|
|
|
enable: () => {
|
|
|
|
notImplemented("test.mock.timers.enable");
|
|
|
|
},
|
|
|
|
reset: () => {
|
|
|
|
notImplemented("test.mock.timers.reset");
|
|
|
|
},
|
|
|
|
tick: () => {
|
|
|
|
notImplemented("test.mock.timers.tick");
|
|
|
|
},
|
|
|
|
runAll: () => {
|
|
|
|
notImplemented("test.mock.timers.runAll");
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export default test;
|