diff --git a/lib/test_runner/test_runner.test.ts b/lib/test_runner/test_runner.test.ts index 0af61a5..37c2611 100644 --- a/lib/test_runner/test_runner.test.ts +++ b/lib/test_runner/test_runner.test.ts @@ -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 { diff --git a/lib/test_runner/test_runner.ts b/lib/test_runner/test_runner.ts index 847745d..9d2d159 100644 --- a/lib/test_runner/test_runner.ts +++ b/lib/test_runner/test_runner.ts @@ -23,6 +23,7 @@ export interface RunTestDefinitionsOptions { export interface TestDefinition { name: string; fn: (context: TestContext) => Promise | void; + only?: boolean; ignore?: boolean; } @@ -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) { @@ -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(