Skip to content

Commit

Permalink
feat: basic suite modes (skip, todo, only*) (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev committed Dec 3, 2021
1 parent ecbc0c1 commit 81c9913
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 11 deletions.
28 changes: 19 additions & 9 deletions src/run.ts
Expand Up @@ -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
Expand Down
21 changes: 19 additions & 2 deletions 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> | void) => (context.currentSuite || defaultSuite).test(name, fn)
Expand All @@ -9,12 +9,13 @@ export function clearContext() {
defaultSuite.clear()
}

export function suite(suiteName: string, factory?: (test: Suite['test']) => Promise<void> | void) {
function processSuite(mode: SuiteMode, suiteName: string, factory?: TestFactory) {
const queue: Task[] = []
const factoryQueue: Task[] = []

const suite: Suite = {
name: suiteName,
mode,
test,
collect,
clear,
Expand Down Expand Up @@ -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
5 changes: 5 additions & 0 deletions src/types.ts
Expand Up @@ -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) => void
collect: () => Promise<Task[]>
clear: () => void
}

export type TestFactory = (test: Suite['test']) => Promise<void> | void

export interface File {
filepath: string
suites: Suite[]
Expand Down
9 changes: 9 additions & 0 deletions 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')

0 comments on commit 81c9913

Please sign in to comment.