1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 07:14:47 -05:00

feat: stabilize test steps API (#13400)

This commit is contained in:
David Sherret 2022-01-18 15:02:56 -05:00 committed by GitHub
parent ce52bfc59c
commit 0f3a53e5d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 38 additions and 81 deletions

View file

@ -113,8 +113,40 @@ declare namespace Deno {
* See: https://no-color.org/ */ * See: https://no-color.org/ */
export const noColor: boolean; export const noColor: boolean;
/** **UNSTABLE**: New option, yet to be vetted. */
export interface TestContext { export interface 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.
* If the test was ignored the promise returns `false`.
*/
step(t: TestStepDefinition): Promise<boolean>;
/** 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.
* If the test was ignored the promise returns `false`.
*/
step(
name: string,
fn: (t: TestContext) => void | Promise<void>,
): Promise<boolean>;
}
export interface TestStepDefinition {
fn: (t: TestContext) => void | Promise<void>;
name: string;
ignore?: boolean;
/** Check that the number of async completed ops after the test step is the same
* as number of dispatched ops. Defaults to the parent test or step's value. */
sanitizeOps?: boolean;
/** Ensure the test step does not "leak" resources - ie. the resource table
* after the test has exactly the same contents as before the test. Defaults
* to the parent test or step's value. */
sanitizeResources?: boolean;
/** Ensure the test step does not prematurely cause the process to exit,
* for example via a call to `Deno.exit`. Defaults to the parent test or
* step's value. */
sanitizeExit?: boolean;
} }
export interface TestDefinition { export interface TestDefinition {

View file

@ -934,43 +934,6 @@ declare namespace Deno {
*/ */
export function sleepSync(millis: number): void; export function sleepSync(millis: number): void;
/** **UNSTABLE**: New option, yet to be vetted. */
export interface TestContext {
/** Run a sub step of the parent test with a given name. Returns a promise
* that resolves to a boolean signifying if the step completed successfully.
* The returned promise never rejects unless the arguments are invalid.
* If the test was ignored, the promise returns `false`.
*/
step(t: TestStepDefinition): Promise<boolean>;
/** Run a sub step of the parent test with a given name. Returns a promise
* that resolves to a boolean signifying if the step completed successfully.
* The returned promise never rejects unless the arguments are invalid.
* If the test was ignored, the promise returns `false`.
*/
step(
name: string,
fn: (t: TestContext) => void | Promise<void>,
): Promise<boolean>;
}
/** **UNSTABLE**: New option, yet to be vetted. */
export interface TestStepDefinition {
fn: (t: TestContext) => void | Promise<void>;
name: string;
ignore?: boolean;
/** Check that the number of async completed ops after the test is the same
* as number of dispatched ops. Defaults to true. */
sanitizeOps?: boolean;
/** Ensure the test case does not "leak" resources - ie. the resource table
* after the test has exactly the same contents as before the test. Defaults
* to true. */
sanitizeResources?: boolean;
/** Ensure the test case does not prematurely cause the process to exit,
* for example via a call to `Deno.exit`. Defaults to true. */
sanitizeExit?: boolean;
}
/** **UNSTABLE**: new API, yet to be vetted. /** **UNSTABLE**: new API, yet to be vetted.
* *
* A generic transport listener for message-oriented protocols. */ * A generic transport listener for message-oriented protocols. */

View file

@ -222,37 +222,31 @@ itest!(aggregate_error {
}); });
itest!(steps_passing_steps { itest!(steps_passing_steps {
args: "test --unstable test/steps/passing_steps.ts", args: "test test/steps/passing_steps.ts",
exit_code: 0, exit_code: 0,
output: "test/steps/passing_steps.out", output: "test/steps/passing_steps.out",
}); });
itest!(steps_passing_steps_concurrent { itest!(steps_passing_steps_concurrent {
args: "test --unstable --jobs=2 test/steps/passing_steps.ts", args: "test --jobs=2 test/steps/passing_steps.ts",
exit_code: 0, exit_code: 0,
output: "test/steps/passing_steps.out", output: "test/steps/passing_steps.out",
}); });
itest!(steps_failing_steps { itest!(steps_failing_steps {
args: "test --unstable test/steps/failing_steps.ts", args: "test test/steps/failing_steps.ts",
exit_code: 1, exit_code: 1,
output: "test/steps/failing_steps.out", output: "test/steps/failing_steps.out",
}); });
itest!(steps_ignored_steps { itest!(steps_ignored_steps {
args: "test --unstable test/steps/ignored_steps.ts", args: "test test/steps/ignored_steps.ts",
exit_code: 0, exit_code: 0,
output: "test/steps/ignored_steps.out", output: "test/steps/ignored_steps.out",
}); });
itest!(steps_invalid_usage { itest!(steps_invalid_usage {
args: "test --unstable test/steps/invalid_usage.ts", args: "test test/steps/invalid_usage.ts",
exit_code: 1, exit_code: 1,
output: "test/steps/invalid_usage.out", output: "test/steps/invalid_usage.out",
}); });
itest!(steps_no_unstable_flag {
args: "test test/steps/no_unstable_flag.ts",
exit_code: 1,
output: "test/steps/no_unstable_flag.out",
});

View file

@ -1,13 +0,0 @@
[WILDCARD]
running 1 test from [WILDCARD]/no_unstable_flag.ts
test description ... FAILED ([WILDCARD])
failures:
description
Error: Test steps are unstable. The --unstable flag must be provided.
at [WILDCARD]
failures:
[WILDCARD]

View file

@ -1,4 +0,0 @@
Deno.test("description", async (t) => {
// deno-lint-ignore no-explicit-any
await (t as any).step("step", () => {});
});

View file

@ -29,7 +29,6 @@
RegExpPrototypeTest, RegExpPrototypeTest,
SymbolToStringTag, SymbolToStringTag,
} = window.__bootstrap.primordials; } = window.__bootstrap.primordials;
let testStepsEnabled = false;
const opSanitizerDelayResolveQueue = []; const opSanitizerDelayResolveQueue = [];
@ -746,12 +745,6 @@ finishing test case.`;
* @param fn {(t: TestContext) => void | Promise<void>} * @param fn {(t: TestContext) => void | Promise<void>}
*/ */
async step(nameOrTestDefinition, fn) { async step(nameOrTestDefinition, fn) {
if (!testStepsEnabled) {
throw new Error(
"Test steps are unstable. The --unstable flag must be provided.",
);
}
if (parentStep.finalized) { if (parentStep.finalized) {
throw new Error( throw new Error(
"Cannot run test step after parent scope has finished execution. " + "Cannot run test step after parent scope has finished execution. " +
@ -890,14 +883,9 @@ finishing test case.`;
return value == null ? defaultValue : value; return value == null ? defaultValue : value;
} }
function enableTestSteps() {
testStepsEnabled = true;
}
window.__bootstrap.internals = { window.__bootstrap.internals = {
...window.__bootstrap.internals ?? {}, ...window.__bootstrap.internals ?? {},
runTests, runTests,
enableTestSteps,
}; };
window.__bootstrap.testing = { window.__bootstrap.testing = {

View file

@ -214,9 +214,6 @@ delete Object.prototype.__proto__;
runtimeOptions.v8Version, runtimeOptions.v8Version,
runtimeOptions.tsVersion, runtimeOptions.tsVersion,
); );
if (runtimeOptions.unstableFlag) {
internals.enableTestSteps();
}
build.setBuildInfo(runtimeOptions.target); build.setBuildInfo(runtimeOptions.target);
util.setLogDebug(runtimeOptions.debugFlag, source); util.setLogDebug(runtimeOptions.debugFlag, source);
const prepareStackTrace = core.createPrepareStackTrace( const prepareStackTrace = core.createPrepareStackTrace(