Skip to content

Commit

Permalink
feat: support Deno.test.only (#367)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Feb 3, 2024
1 parent c9bb94e commit 2537df1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lib/test_runner/test_runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,31 @@ GokG
);
});

Deno.test("only test definitions", async () => {
const context = getContext();
await assertRejects(
async () => {
await runTestDefinitions([{
name: "won't run",
fn: () => {
throw new Error("FAIL");
},
}, {
only: true,
name: "my test",
fn: () => {}, // pass
}], context);
},
Error,
"Exit code 1 thrown.",
);
wildcardAssertEquals(
context.output,
`test my test[WILDCARD]
error: Test failed because the "only" option was used.\n`,
);
});

function getContext() {
let output = "";
return {
Expand Down
10 changes: 10 additions & 0 deletions lib/test_runner/test_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface RunTestDefinitionsOptions {
export interface TestDefinition {
name: string;
fn: (context: TestContext) => Promise<void> | void;
only?: boolean;
ignore?: boolean;
}

Expand All @@ -46,6 +47,10 @@ export async function runTestDefinitions(
options: RunTestDefinitionsOptions,
) {
const testFailures = [];
const hasOnly = testDefinitions.some((d) => d.only);
if (hasOnly) {
testDefinitions = testDefinitions.filter((d) => d.only);
}
for (const definition of testDefinitions) {
options.process.stdout.write("test " + definition.name + " ...");
if (definition.ignore) {
Expand Down Expand Up @@ -87,6 +92,11 @@ export async function runTestDefinitions(
);
}
options.process.exit(1);
} else if (hasOnly) {
options.process.stdout.write(
'error: Test failed because the "only" option was used.\n',
);
options.process.exit(1);
}

function getTestContext(
Expand Down

0 comments on commit 2537df1

Please sign in to comment.