From 81c9913ccf82dca9b612361e1a4bd1aabfa118aa Mon Sep 17 00:00:00 2001 From: patak Date: Fri, 3 Dec 2021 23:43:08 +0100 Subject: [PATCH] feat: basic suite modes (skip, todo, only*) (#1) --- src/run.ts | 28 +++++++++++++++++++--------- src/suite.ts | 21 +++++++++++++++++++-- src/types.ts | 5 +++++ test/modes.test.ts | 9 +++++++++ 4 files changed, 52 insertions(+), 11 deletions(-) create mode 100644 test/modes.test.ts diff --git a/src/run.ts b/src/run.ts index 20eb1749d3f3..2b410190b025 100644 --- a/src/run.ts +++ b/src/run.ts @@ -63,17 +63,27 @@ export async function runFile(filepath: string) { indent += 1 } - const result = await runTasks(tasks) - for (const r of result) { - if (r.error === undefined) { - log(`${' '.repeat(indent * 2)}${c.inverse(c.green(' PASS '))} ${c.green(r.task.name)}`) - } - else { - console.error(`${' '.repeat(indent * 2)}${c.inverse(c.red(' FAIL '))} ${c.red(r.task.name)}`) - console.error(' '.repeat((indent + 2) * 2) + c.red(String(r.error))) - process.exitCode = 1 + if (suite.mode === 'run' || suite.mode === 'only') { + // TODO: If there is a task with 'only', skip all others + const result = await runTasks(tasks) + for (const r of result) { + if (r.error === undefined) { + log(`${' '.repeat(indent * 2)}${c.inverse(c.green(' PASS '))} ${c.green(r.task.name)}`) + } + else { + console.error(`${' '.repeat(indent * 2)}${c.inverse(c.red(' FAIL '))} ${c.red(r.task.name)}`) + console.error(' '.repeat((indent + 2) * 2) + c.red(String(r.error))) + process.exitCode = 1 + } } } + else if (suite.mode === 'skip') { + log(`${' '.repeat(indent * 2)}${c.inverse(c.gray(' SKIP '))}`) + } + else if (suite.mode === 'todo') { + // TODO: In Jest, these suites are collected and printed together at the end of the report + log(`${' '.repeat(indent * 2)}${c.inverse(c.gray(' TODO '))}`) + } if (suite.name) indent -= 1 diff --git a/src/suite.ts b/src/suite.ts index 8d6bf59ebcdf..b39679a7bac7 100644 --- a/src/suite.ts +++ b/src/suite.ts @@ -1,5 +1,5 @@ import { context } from './context' -import { Task, Suite } from './types' +import { Task, Suite, SuiteMode, TestFactory } from './types' export const defaultSuite = suite('') export const test = (name: string, fn: () => Promise | void) => (context.currentSuite || defaultSuite).test(name, fn) @@ -9,12 +9,13 @@ export function clearContext() { defaultSuite.clear() } -export function suite(suiteName: string, factory?: (test: Suite['test']) => Promise | void) { +function processSuite(mode: SuiteMode, suiteName: string, factory?: TestFactory) { const queue: Task[] = [] const factoryQueue: Task[] = [] const suite: Suite = { name: suiteName, + mode, test, collect, clear, @@ -47,6 +48,22 @@ export function suite(suiteName: string, factory?: (test: Suite['test']) => Prom return suite } +export function suite(suiteName: string, factory?: TestFactory) { + return processSuite('run', suiteName, factory) +} + +suite.skip = function skip(suiteName: string, factory?: TestFactory) { + return processSuite('skip', suiteName, factory) +} + +suite.only = function skip(suiteName: string, factory?: TestFactory) { + return processSuite('only', suiteName, factory) +} + +suite.todo = function skip(suiteName: string) { + return processSuite('todo', suiteName) +} + // alias export const describe = suite export const it = test diff --git a/src/types.ts b/src/types.ts index 781039fbf932..ac0761b4d6fd 100644 --- a/src/types.ts +++ b/src/types.ts @@ -22,13 +22,18 @@ export interface TaskResult { error?: unknown } +export type SuiteMode = 'run' | 'skip' | 'only' | 'todo' + export interface Suite { name: string + mode: SuiteMode test: (name: string, fn: () => Promise | void) => void collect: () => Promise clear: () => void } +export type TestFactory = (test: Suite['test']) => Promise | void + export interface File { filepath: string suites: Suite[] diff --git a/test/modes.test.ts b/test/modes.test.ts new file mode 100644 index 000000000000..3cf7528d9fcd --- /dev/null +++ b/test/modes.test.ts @@ -0,0 +1,9 @@ +import { it, describe, assert } from '../src' + +describe.skip('skipped suite', () => { + it('no fail as it is skipped', () => { + assert.equal(Math.sqrt(4), 3) + }) +}) + +describe.todo('unimplemented suite')