Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: basic suite modes (skip, todo, only*) #1

Merged
merged 2 commits into from Dec 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -19,13 +19,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
patak-dev marked this conversation as resolved.
Show resolved Hide resolved

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')