Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(test): Add "name", "origin" and "parent" to "Deno.TestContext" #14007

Merged
merged 19 commits into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions cli/dts/lib.deno.ns.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,19 @@ declare namespace Deno {
}

export interface TestContext {
/**
* The current test name.
*/
name: string;
dsherret marked this conversation as resolved.
Show resolved Hide resolved
dsherret marked this conversation as resolved.
Show resolved Hide resolved
/**
* File Uri of the current test code.
*/
origin: string;
dsherret marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering that import.meta.url is a string, I think it makes sense for this to be a string as well.

/**
* Parent test context.
*/
parent?: TestContext;

/** Run a sub step of the parent test or step. Returns a promise
* that resolves to a boolean signifying if the step completed successfully.
* The returned promise never rejects unless the arguments are invalid.
Expand All @@ -270,6 +283,9 @@ declare namespace Deno {

export interface TestStepDefinition {
fn: (t: TestContext) => void | Promise<void>;
/**
* The current test name.
*/
name: string;
ignore?: boolean;
/** Check that the number of async completed ops after the test step is the same
Expand All @@ -287,6 +303,9 @@ declare namespace Deno {

export interface TestDefinition {
fn: (t: TestContext) => void | Promise<void>;
/**
* The current test name.
*/
name: string;
ignore?: boolean;
/** If at least one test has `only` set to true, only run tests that have
Expand Down
32 changes: 31 additions & 1 deletion cli/tests/unit/testing_test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assertRejects, assertThrows } from "./test_util.ts";
import { assertEquals, assertRejects, assertThrows } from "./test_util.ts";

Deno.test(function testWrongOverloads() {
assertThrows(
Expand Down Expand Up @@ -117,3 +117,33 @@ Deno.test(async function invalidStepArguments(t) {
"Expected a test definition or name and function.",
);
});

Deno.test(async function nameOnTextContext(t1) {
await assertEquals(t1.name, "nameOnTextContext");
await t1.step("step", async (t2) => {
await assertEquals(t2.name, "step");
await t2.step("nested step", async (t3) => {
await assertEquals(t3.name, "nested step");
});
});
});

Deno.test(async function originOnTextContext(t1) {
await assertEquals(t1.origin, Deno.mainModule);
await t1.step("step", async (t2) => {
await assertEquals(t2.origin, Deno.mainModule);
await t2.step("nested step", async (t3) => {
await assertEquals(t3.origin, Deno.mainModule);
});
});
});

Deno.test(async function parentOnTextContext(t1) {
await assertEquals(t1.parent, undefined);
await t1.step("step", async (t2) => {
await assertEquals(t1, t2.parent);
await t2.step("nested step", async (t3) => {
await assertEquals(t2, t3.parent);
});
});
});
21 changes: 20 additions & 1 deletion runtime/js/40_testing.js
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,7 @@
const step = new TestStep({
name: test.name,
parent: undefined,
parentContext: undefined,
rootTestDescription: description,
sanitizeOps: test.sanitizeOps,
sanitizeResources: test.sanitizeResources,
Expand Down Expand Up @@ -1064,8 +1065,9 @@
* }} TestStepDefinition
*
* @typedef {{
* name: string;
* name: string,
* parent: TestStep | undefined,
* parentContext: TestContext | undefined,
* rootTestDescription: { origin: string; name: string };
* sanitizeOps: boolean,
* sanitizeResources: boolean,
Expand Down Expand Up @@ -1099,6 +1101,10 @@
return this.#params.parent;
}

get parentContext() {
return this.#params.parentContext;
}

get rootTestDescription() {
return this.#params.rootTestDescription;
}
Expand Down Expand Up @@ -1268,6 +1274,18 @@
function createTestContext(parentStep) {
return {
[SymbolToStringTag]: "TestContext",
/**
* The current test name.
*/
name: parentStep.name,
/**
* Parent test context.
*/
parent: parentStep.parentContext ?? undefined,
/**
* File Uri of the test code.
*/
origin: parentStep.rootTestDescription.origin,
/**
* @param nameOrTestDefinition {string | TestStepDefinition}
* @param fn {(t: TestContext) => void | Promise<void>}
Expand All @@ -1284,6 +1302,7 @@
const subStep = new TestStep({
name: definition.name,
parent: parentStep,
parentContext: this,
rootTestDescription: parentStep.rootTestDescription,
sanitizeOps: getOrDefault(
definition.sanitizeOps,
Expand Down