Skip to content

Commit

Permalink
feat: skip tests in bail mode and allow skipping group tests in bulk
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jan 5, 2025
1 parent e86a337 commit f8f710b
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 16 deletions.
15 changes: 15 additions & 0 deletions src/group/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ export class Group<Context extends Record<any, any>> extends Macroable {
#testsRetries?: number
#testSetupHooks: TestHooksHandler<Context>[] = []
#testTeardownHooks: TestHooksHandler<Context>[] = []
#testsSkip?: {
skip: boolean | (() => Promise<boolean> | boolean)
skipReason?: string
}

/**
* Know if one or more tests/hooks within this group
Expand All @@ -77,6 +81,7 @@ export class Group<Context extends Record<any, any>> extends Macroable {
teardown: (handler: TestHooksHandler<Context>) => void
timeout: (timeout: number) => void
retry: (retries: number) => void
skip: (skip?: boolean | (() => Promise<boolean> | boolean), skipReason?: string) => void
disableTimeout: () => void
} = {
/**
Expand Down Expand Up @@ -113,6 +118,13 @@ export class Group<Context extends Record<any, any>> extends Macroable {
retry: (retries: number) => {
this.#testsRetries = retries
},

/**
* Skip all the tests inside the group
*/
skip: (skip, skipReason) => {
this.#testsSkip = { skip: skip ?? true, skipReason }
},
}

constructor(
Expand Down Expand Up @@ -163,6 +175,9 @@ export class Group<Context extends Record<any, any>> extends Macroable {
if (this.#testTeardownHooks.length) {
this.#testTeardownHooks.forEach((handler) => test.teardown(handler))
}
if (this.#testsSkip) {
test.skip(this.#testsSkip.skip, this.#testsSkip.skipReason)
}

/**
* Invoke tap callback passing test to each callback
Expand Down
11 changes: 7 additions & 4 deletions src/group/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,17 @@ export class GroupRunner {
* Run the test executor
*/
for (let test of this.#group.tests) {
/**
* Skip tests in bail mode when there is an error
*/
if (this.#options.bail && this.#hasError) {
test.skip(true, 'Skipped due to bail mode')
}

await test.exec()
if (!this.#hasError && test.failed) {
this.#hasError = true
}

if (this.#options.bail && this.#hasError) {
break
}
}

/**
Expand Down
16 changes: 12 additions & 4 deletions src/suite/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import debug from '../debug.js'
import { Suite } from './main.js'
import { Emitter } from '../emitter.js'
import type { SuiteEndNode, SuiteHooks, SuiteHooksData, SuiteStartNode } from '../types.js'
import { Group } from '../group/main.js'

/**
* Run all groups or tests inside the suite stack
Expand Down Expand Up @@ -178,14 +179,21 @@ export class SuiteRunner {
* Run the test executor
*/
for (let groupOrTest of this.#suite.stack) {
/**
* Skip tests in bail mode when there is an error
*/
if (this.#options.bail && this.#hasError) {
if (groupOrTest instanceof Group) {
groupOrTest.each.skip(true, 'Skipped due to bail mode')
} else {
groupOrTest.skip(true, 'Skipped due to bail mode')
}
}

await groupOrTest.exec()
if (!this.#hasError && groupOrTest.failed) {
this.#hasError = true
}

if (this.#options.bail && this.#hasError) {
break
}
}

/**
Expand Down
13 changes: 13 additions & 0 deletions tests/group/configure.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,17 @@ test.describe('configure', () => {

assert.equal(testInstance.options.timeout, 0)
})

test('skip all tests inside the group', async () => {
const emitter = new Emitter()
const refiner = new Refiner({})

const group = new Group<TestContext>('sample group', emitter, refiner)
const testInstance = new Test('2 + 2 = 4', new TestContext(), emitter, refiner)

group.each.skip()
group.add(testInstance)

assert.equal(testInstance.options.isSkipped, true)
})
})
4 changes: 3 additions & 1 deletion tests/group/execute.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,14 @@ test.describe('execute | test', () => {
const [groupEndEvent] = await Promise.all([pEvent(emitter, 'group:end'), group.exec()])

assert.isTrue(group.failed)
assert.lengthOf(events, 1)
assert.lengthOf(events, 2)
assert.equal(events[0].title.expanded, 'test')
assert.isTrue(events[0].hasError)
assert.equal(events[0].errors[0].phase, 'test')
assert.equal(events[0].errors[0].error.message, 'blow up')

assert.isTrue(events[1].isSkipped)

assert.equal(groupEndEvent!.title, 'sample group')
assert.deepEqual(stack, ['test'])
})
Expand Down
26 changes: 19 additions & 7 deletions tests/suite/execute.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,22 +166,34 @@ test.describe('execute | test', () => {
})

group1.add(testInstance).add(testInstance1)

const testInstance3 = new Test('test 3', new TestContext(), emitter, refiner)
testInstance3.run(() => {
stack.push('test 3')
throw new Error('blow up')
})
const testInstance4 = new Test('test 4', new TestContext(), emitter, refiner)
testInstance4.run(() => {
stack.push('test 1')
})

suite.bail()
group2.add(testInstance).add(testInstance1)
group2.add(testInstance3).add(testInstance4)

const [suiteEndEvent] = await Promise.all([pEvent(emitter, 'suite:end'), suite.exec()])
await Promise.all([pEvent(emitter, 'suite:end'), suite.exec()])

assert.isTrue(suite.failed)
assert.lengthOf(events, 2)
assert.lengthOf(events, 6)

assert.equal((events[0].title as { expanded: string }).expanded, 'test')
assert.isTrue(events[0].hasError)
assert.isTrue((events[1] as TestEndNode).isSkipped)

assert.equal(String(events[1].title), 'group')
assert.isTrue(events[1].hasError)
assert.equal((events[3].title as { expanded: string }).expanded, 'test 3')
assert.isTrue(events[3].hasError)
assert.isTrue((events[4] as TestEndNode).isSkipped)

assert.equal(suiteEndEvent!.name, 'sample suite')
assert.deepEqual(stack, ['test'])
assert.deepEqual(stack, ['test', 'test 3'])
})
})

Expand Down

0 comments on commit f8f710b

Please sign in to comment.