0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-02-01 12:16:11 -05:00
This commit is contained in:
Marvin Hagemeister 2025-01-20 16:07:40 +01:00 committed by GitHub
commit c8c0f9f6c6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,5 +1,7 @@
// Copyright 2018-2025 the Deno authors. MIT license. // Copyright 2018-2025 the Deno authors. MIT license.
// @ts-check
import { core, primordials } from "ext:core/mod.js"; import { core, primordials } from "ext:core/mod.js";
import { escapeName, withPermissions } from "ext:cli/40_test_common.js"; import { escapeName, withPermissions } from "ext:cli/40_test_common.js";
@ -33,6 +35,14 @@ import { setExitHandler } from "ext:deno_os/30_os.js";
const DenoNs = globalThis.Deno; const DenoNs = globalThis.Deno;
/** /**
* @typedef {() => Promise<"ignored" | "ok" | { failed: any}>} TestFunction
*
* @typedef {{
* fileName: string,
* lineNumber: number,
* columnNumber: number
* }} TestLocation
*
* @typedef {{ * @typedef {{
* id: number, * id: number,
* name: string, * name: string,
@ -40,17 +50,17 @@ const DenoNs = globalThis.Deno;
* origin: string, * origin: string,
* location: TestLocation, * location: TestLocation,
* ignore: boolean, * ignore: boolean,
* only: boolean. * only: boolean,
* sanitizeOps: boolean, * sanitizeOps: boolean,
* sanitizeResources: boolean, * sanitizeResources: boolean,
* sanitizeExit: boolean, * sanitizeExit: boolean,
* permissions: PermissionOptions, * permissions: Deno.PermissionOptions,
* }} TestDescription * }} TestDescription
* *
* @typedef {{ * @typedef {{
* id: number, * id: number,
* name: string, * name: string,
* fn: TestFunction * fn: TestFunction,
* origin: string, * origin: string,
* location: TestLocation, * location: TestLocation,
* ignore: boolean, * ignore: boolean,
@ -79,12 +89,12 @@ const DenoNs = globalThis.Deno;
* @typedef {{ * @typedef {{
* id: number, * id: number,
* name: string, * name: string,
* fn: BenchFunction * fn: BenchFunction,
* origin: string, * origin: string,
* ignore: boolean, * ignore: boolean,
* only: boolean. * only: boolean,
* sanitizeExit: boolean, * sanitizeExit: boolean,
* permissions: PermissionOptions, * permissions: Deno.PermissionOptions,
* }} BenchDescription * }} BenchDescription
*/ */
@ -125,6 +135,11 @@ function assertExit(fn, isTest) {
}; };
} }
/**
* @param {*} fn
* @param {TestDescription | TestStepDescription} desc
* @returns {() => Promise<"ignored" | "ok" | { failed: any}>}
*/
function wrapOuter(fn, desc) { function wrapOuter(fn, desc) {
return async function outerWrapped() { return async function outerWrapped() {
try { try {
@ -145,9 +160,10 @@ function wrapOuter(fn, desc) {
} }
function wrapInner(fn) { function wrapInner(fn) {
/** @param desc {TestDescription | TestStepDescription} */ /** @param {TestDescription | TestStepDescription} desc */
return async function innerWrapped(desc) { return async function innerWrapped(desc) {
function getRunningStepDescs() { function getRunningStepDescs() {
/** @type {TestStepDescription[]} */
const results = []; const results = [];
let childDesc = desc; let childDesc = desc;
while (childDesc.parent != null) { while (childDesc.parent != null) {
@ -206,7 +222,11 @@ function wrapInner(fn) {
const registerTestIdRetBuf = new Uint32Array(1); const registerTestIdRetBuf = new Uint32Array(1);
const registerTestIdRetBufU8 = new Uint8Array(registerTestIdRetBuf.buffer); const registerTestIdRetBufU8 = new Uint8Array(registerTestIdRetBuf.buffer);
// As long as we're using one isolate per test, we can cache the origin since it won't change /**
* As long as we're using one isolate per test, we can cache the origin
* since it won't change.
* @type {string | undefined}
*/
let cachedOrigin = undefined; let cachedOrigin = undefined;
function testInner( function testInner(
@ -355,6 +375,10 @@ test.only = function (
return testInner(nameOrFnOrOptions, optionsOrFn, maybeFn, { only: true }); return testInner(nameOrFnOrOptions, optionsOrFn, maybeFn, { only: true });
}; };
/**
* @param {TestDescription | TestStepDescription} desc
* @returns {string}
*/
function getFullName(desc) { function getFullName(desc) {
if ("parent" in desc) { if ("parent" in desc) {
return `${getFullName(desc.parent)} ... ${desc.name}`; return `${getFullName(desc.parent)} ... ${desc.name}`;
@ -362,10 +386,19 @@ function getFullName(desc) {
return desc.name; return desc.name;
} }
/**
* @param {TestDescription | TestStepDescription} desc
* @returns {boolean}
*/
function usesSanitizer(desc) { function usesSanitizer(desc) {
return desc.sanitizeResources || desc.sanitizeOps || desc.sanitizeExit; return desc.sanitizeResources || desc.sanitizeOps || desc.sanitizeExit;
} }
/**
* @param {TestStepDescription} desc
* @param {*} result
* @param {number} elapsed
*/
function stepReportResult(desc, result, elapsed) { function stepReportResult(desc, result, elapsed) {
const state = MapPrototypeGet(testStates, desc.id); const state = MapPrototypeGet(testStates, desc.id);
for (const childDesc of state.children) { for (const childDesc of state.children) {
@ -380,7 +413,9 @@ function stepReportResult(desc, result, elapsed) {
} }
} }
/** @param desc {TestDescription | TestStepDescription} */ /**
* @param {TestDescription | TestStepDescription} desc
*/
function createTestContext(desc) { function createTestContext(desc) {
let parent; let parent;
let level; let level;
@ -412,8 +447,8 @@ function createTestContext(desc) {
*/ */
origin: desc.origin, origin: desc.origin,
/** /**
* @param nameOrFnOrOptions {string | TestStepDefinition | ((t: TestContext) => void | Promise<void>)} * @param {string | TestStepDescription | ((t: TestContext) => void | Promise<void>)} nameOrFnOrOptions
* @param maybeFn {((t: TestContext) => void | Promise<void>) | undefined} * @param {((t: TestContext) => void | Promise<void>) | undefined} maybeFn
*/ */
async step(nameOrFnOrOptions, maybeFn) { async step(nameOrFnOrOptions, maybeFn) {
if (MapPrototypeGet(testStates, desc.id).completed) { if (MapPrototypeGet(testStates, desc.id).completed) {
@ -423,6 +458,7 @@ function createTestContext(desc) {
); );
} }
/** @type {TestStepDescription & { fn: any }} */
let stepDesc; let stepDesc;
if (typeof nameOrFnOrOptions === "string") { if (typeof nameOrFnOrOptions === "string") {
if (typeof maybeFn !== "function") { if (typeof maybeFn !== "function") {
@ -500,10 +536,8 @@ function createTestContext(desc) {
/** /**
* Wrap a user test function in one which returns a structured result. * Wrap a user test function in one which returns a structured result.
* @template T {Function} * @param {TestDescription | TestStepDescription} desc
* @param testFn {T} * @returns {TestFunction}
* @param desc {TestDescription | TestStepDescription}
* @returns {T}
*/ */
function wrapTest(desc) { function wrapTest(desc) {
let testFn = wrapInner(desc.fn); let testFn = wrapInner(desc.fn);