1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00
This commit is contained in:
Marvin Hagemeister 2024-10-10 17:14:28 +02:00
parent b55d7f91cb
commit 2f2df854b0

View file

@ -544,7 +544,8 @@ globalThis.Deno.test = test;
* name: string, * name: string,
* fn: () => any, * fn: () => any,
* only: boolean, * only: boolean,
* ignore: boolean * ignore: boolean,
* location: TestLocationInfo,
* }} BddTest * }} BddTest
* *
* @typedef {() => unknown | Promise<unknown>} TestLifecycleFn * @typedef {() => unknown | Promise<unknown>} TestLifecycleFn
@ -560,7 +561,22 @@ globalThis.Deno.test = test;
* afterAll: TestLifecycleFn | null, * afterAll: TestLifecycleFn | null,
* beforeEach: TestLifecycleFn | null, * beforeEach: TestLifecycleFn | null,
* afterEach: TestLifecycleFn | null * afterEach: TestLifecycleFn | null
* sanitizeOps: boolean,
* sanitizeResources: boolean,
* sanitizeExit: boolean,
* permissions?: Deno.PermissionOptions,
* }} TestGroup * }} TestGroup
*
* @typedef {{
* only: boolean,
* ignore: boolean,
* name: string,
* fn: () => any,
* sanitizeOps: boolean,
* sanitizeResources: boolean,
* sanitizeExit: boolean,
* permissions?: Deno.PermissionOptions,
* }} BddArgs
*/ */
/** @type {TestGroup} */ /** @type {TestGroup} */
@ -575,6 +591,10 @@ const ROOT_TEST_GROUP = {
beforeEach: null, beforeEach: null,
afterAll: null, afterAll: null,
afterEach: null, afterEach: null,
sanitizeExit: false,
sanitizeOps: false,
sanitizeResources: false,
permissions: undefined,
}; };
// No-op if we're not running in `deno test` subcommand. // No-op if we're not running in `deno test` subcommand.
if (typeof op_register_test === "function") { if (typeof op_register_test === "function") {
@ -594,12 +614,90 @@ const BDD_CONTEXT = {
}; };
/** /**
* @param {string} name * @overload
* @param {() => any} fn * @param {() => any} nameOrFnOrOptions
* @param {boolean} ignore * @returns {BddArgs}
* @param {boolean} only
*/ */
function itInner(name, fn, ignore, only) { /**
* @overload
* @param {BddArgs} nameOrFnOrOptions
* @returns {BddArgs}
*/
/**
* @overload
* @param {string} nameOrFnOrOptions
* @param {() => any} fnOrOptions
* @returns {BddArgs}
*/
/**
* @overload
* @param {string} nameOrFnOrOptions
* @param {BddArgs} fnOrOptions
* @param {() => any} maybeFn
* @returns {BddArgs}
*/
/**
* @param {string | (() => any) | BddArgs} nameOrFnOrOptions
* @param {(() => any) | BddArgs} [fnOrOptions]
* @param {(() => any)} [maybeFn]
* @returns {BddArgs}
*/
function normalizeBddArgs(nameOrFnOrOptions, fnOrOptions, maybeFn) {
let name = "";
let fn;
let only = false;
let ignore = false;
let sanitizeExit = false;
let sanitizeOps = false;
let sanitizeResources = false;
let permissions;
if (typeof nameOrFnOrOptions === "function") {
name = nameOrFnOrOptions.name;
fn = nameOrFnOrOptions;
} else if (typeof nameOrFnOrOptions === "object") {
return nameOrFnOrOptions;
} else if (typeof fnOrOptions === "function") {
name = nameOrFnOrOptions;
fn = fnOrOptions;
} else if (fnOrOptions !== undefined && maybeFn !== undefined) {
name = nameOrFnOrOptions;
only = fnOrOptions.only;
ignore = fnOrOptions.ignore;
sanitizeExit = fnOrOptions.sanitizeExit,
sanitizeOps = fnOrOptions.sanitizeOps,
sanitizeResources = fnOrOptions.sanitizeResources;
permissions = fnOrOptions.permissions;
fn = maybeFn;
} else {
throw new TypeError(`Invalid arguments passed to "Deno.test/it/describe"`);
}
return {
name: escapeName(name),
fn,
only,
ignore,
sanitizeExit,
sanitizeOps,
sanitizeResources,
permissions,
};
}
/**
* @param {BddArgs} args
*/
function itInner({
name,
fn,
ignore,
only,
sanitizeExit,
sanitizeOps,
sanitizeResources,
permissions,
}) {
if ( if (
!ignore && BDD_CONTEXT.stack.length > 1 && !ignore && BDD_CONTEXT.stack.length > 1 &&
BDD_CONTEXT.stack.some((x) => x.ignore) BDD_CONTEXT.stack.some((x) => x.ignore)
@ -612,18 +710,6 @@ function itInner(name, fn, ignore, only) {
} }
const location = core.currentUserCallSite(); const location = core.currentUserCallSite();
const sanitizeOps = false;
const sanitizeResources = false;
const testFn = async () => {
if (ignore) return "ignored";
try {
await fn();
return "ok";
} catch (error) {
return { failed: { jsError: core.destructureError(error) } };
}
};
const parent = getGroupParent(); const parent = getGroupParent();
@ -632,16 +718,17 @@ function itInner(name, fn, ignore, only) {
id: 0, id: 0,
parentId: parent.id, parentId: parent.id,
name, name,
fn: testFn, fn,
ignore, ignore,
only, only,
location,
}; };
parent.children.push(testDef); parent.children.push(testDef);
BDD_CONTEXT.total++; BDD_CONTEXT.total++;
op_register_test( op_register_test(
parent.id, parent.id,
testFn, fn,
escapeName(name), escapeName(name),
ignore, ignore,
only, only,
@ -657,26 +744,37 @@ function itInner(name, fn, ignore, only) {
} }
/** /**
* @param {string} name * @param {string | (() => any) | BddArgs} nameOrFnOrOptions
* @param {() => any} fn * @param {(() => any) | BddArgs} [fnOrOptions]
* @param {(() => any)} [maybeFn]
*/ */
function it(name, fn) { function it(nameOrFnOrOptions, fnOrOptions, maybeFn) {
itInner(name, fn, false, false); const args = normalizeBddArgs(nameOrFnOrOptions, fnOrOptions, maybeFn);
if (args.only) BDD_CONTEXT.hasOnly = true;
itInner(args);
} }
/** /**
* @param {string} name * @param {string | (() => any) | BddArgs} nameOrFnOrOptions
* @param {() => any} fn * @param {(() => any) | BddArgs} [fnOrOptions]
* @param {(() => any)} [maybeFn]
*/ */
it.only = (name, fn) => { it.only = (nameOrFnOrOptions, fnOrOptions, maybeFn) => {
const args = normalizeBddArgs(nameOrFnOrOptions, fnOrOptions, maybeFn);
BDD_CONTEXT.hasOnly = true; BDD_CONTEXT.hasOnly = true;
itInner(name, fn, false, true); args.only = true;
args.ignore = false;
itInner(args);
}; };
/** /**
* @param {string} name * @param {string | (() => any) | BddArgs} nameOrFnOrOptions
* @param {() => any} fn * @param {(() => any) | BddArgs} [fnOrOptions]
* @param {(() => any)} [maybeFn]
*/ */
it.ignore = (name, fn) => { it.ignore = (nameOrFnOrOptions, fnOrOptions, maybeFn) => {
itInner(name, fn, true, false); const args = normalizeBddArgs(nameOrFnOrOptions, fnOrOptions, maybeFn);
args.ignore = true;
args.only = false;
itInner(args);
}; };
it.skip = it.ignore; it.skip = it.ignore;
@ -693,12 +791,20 @@ function getGroupParent() {
} }
/** /**
* @param {string} name * @param {BddArgs} args
* @param {() => void} fn
* @param {boolean} ignore
* @param {boolean} only
*/ */
function describeInner(name, fn, ignore, only) { function describeInner(
{
name,
fn,
ignore,
only,
sanitizeExit,
sanitizeOps,
sanitizeResources,
permissions,
},
) {
// No-op if we're not running in `deno test` subcommand. // No-op if we're not running in `deno test` subcommand.
if (typeof op_register_test !== "function") { if (typeof op_register_test !== "function") {
return; return;
@ -720,6 +826,10 @@ function describeInner(name, fn, ignore, only) {
beforeEach: null, beforeEach: null,
afterAll: null, afterAll: null,
afterEach: null, afterEach: null,
sanitizeExit,
sanitizeOps,
sanitizeResources,
permissions,
}; };
parent.children.push(group); parent.children.push(group);
BDD_CONTEXT.stack.push(group); BDD_CONTEXT.stack.push(group);
@ -769,26 +879,37 @@ function describeInner(name, fn, ignore, only) {
} }
/** /**
* @param {string} name * @param {string | (() => any) | BddArgs} nameOrFnOrOptions
* @param {() => void} fn * @param {(() => any) | BddArgs} [fnOrOptions]
* @param {(() => any)} [maybeFn]
*/ */
function describe(name, fn) { function describe(nameOrFnOrOptions, fnOrOptions, maybeFn) {
describeInner(name, fn, false, false); const args = normalizeBddArgs(nameOrFnOrOptions, fnOrOptions, maybeFn);
if (args.only) BDD_CONTEXT.hasOnly = true;
describeInner(args);
} }
/** /**
* @param {string} name * @param {string | (() => any) | BddArgs} nameOrFnOrOptions
* @param {() => void} fn * @param {(() => any) | BddArgs} [fnOrOptions]
* @param {(() => any)} [maybeFn]
*/ */
describe.only = (name, fn) => { describe.only = (nameOrFnOrOptions, fnOrOptions, maybeFn) => {
const args = normalizeBddArgs(nameOrFnOrOptions, fnOrOptions, maybeFn);
BDD_CONTEXT.hasOnly = true; BDD_CONTEXT.hasOnly = true;
describeInner(name, fn, false, true); args.only = true;
args.ignore = false;
describeInner(args);
}; };
/** /**
* @param {string} name * @param {string | (() => any) | BddArgs} nameOrFnOrOptions
* @param {() => void} fn * @param {(() => any) | BddArgs} [fnOrOptions]
* @param {(() => any)} [maybeFn]
*/ */
describe.ignore = (name, fn) => { describe.ignore = (nameOrFnOrOptions, fnOrOptions, maybeFn) => {
describeInner(name, fn, true, false); const args = normalizeBddArgs(nameOrFnOrOptions, fnOrOptions, maybeFn);
args.only = false;
args.ignore = true;
describeInner(args);
}; };
describe.skip = describe.ignore; describe.skip = describe.ignore;