Skip to content

Commit

Permalink
Removing repeats mode
Browse files Browse the repository at this point in the history
  • Loading branch information
samkevin1 committed Mar 9, 2023
1 parent bdec0b1 commit 1fa8795
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 81 deletions.
129 changes: 58 additions & 71 deletions packages/runner/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ const callCleanupHooks = async (cleanups: HookCleanupCallback[]) => {
export async function runTest(test: Test, runner: VitestRunner) {
await runner.onBeforeRunTest?.(test)

if (test.mode !== 'run' && test.mode !== 'repeats')
if (test.mode !== 'run' && !test.repeats)
return

if (test.result?.state === 'fail') {
Expand All @@ -125,7 +125,7 @@ export async function runTest(test: Test, runner: VitestRunner) {

setCurrentTest(test)

const repeats = test.mode === 'repeats' ? test.repeats || 1 : 1
const repeats = test.repeats ? test.repeats || 1 : 1

for (let repeatCount = 0; repeatCount < repeats; repeatCount++) {
const retry = test.retry || 1
Expand All @@ -138,6 +138,7 @@ export async function runTest(test: Test, runner: VitestRunner) {
beforeEachCleanups = await callSuiteHook(test.suite, test, 'beforeEach', runner, [test.context, test.suite])

test.result.retryCount = retryCount
test.result.repeatCount = repeatCount

if (runner.runTest) {
await runner.runTest(test)
Expand All @@ -151,9 +152,9 @@ export async function runTest(test: Test, runner: VitestRunner) {

await runner.onAfterTryTest?.(test, retryCount)

if (test.mode === 'run')
if (!test.repeats)
test.result.state = 'pass'
else if (test.mode === 'repeats' && retry === retryCount)
else if (test.repeats && retry === retryCount)
test.result.state = 'pass'
}
catch (e) {
Expand All @@ -170,10 +171,6 @@ export async function runTest(test: Test, runner: VitestRunner) {

if (test.result.state === 'pass')
break

if (test.mode === 'repeats' && retry === retryCount && test.result.state === 'fail')
break

// update retry info
updateTask(test, runner)
}
Expand Down Expand Up @@ -251,83 +248,73 @@ export async function runSuite(suite: Suite, runner: VitestRunner) {
suite.result.state = 'todo'
}
else {
const repeats = suite.mode === 'repeats' ? suite.repeats || 1 : 1

for (let repeatCount = 0; repeatCount < repeats; repeatCount++) {
const retry = suite.retry || 1
const retry = suite.retry || 1

for (let retryCount = 0; retryCount < retry; retryCount++) {
try {
beforeAllCleanups = await callSuiteHook(suite, suite, 'beforeAll', runner, [suite])
for (let retryCount = 0; retryCount < retry; retryCount++) {
try {
beforeAllCleanups = await callSuiteHook(suite, suite, 'beforeAll', runner, [suite])

if (runner.runSuite) {
await runner.runSuite(suite)
}
else {
for (let tasksGroup of partitionSuiteChildren(suite)) {
if (tasksGroup[0].concurrent === true) {
const mutex = limit(runner.config.maxConcurrency)
await Promise.all(tasksGroup.map(c => mutex(() => runSuiteChild(c, runner))))
}
else {
const { sequence } = runner.config
if (sequence.shuffle || suite.shuffle) {
// run describe block independently from tests
const suites = tasksGroup.filter(group => group.type === 'suite')
const tests = tasksGroup.filter(group => group.type === 'test')
const groups = shuffle([suites, tests], sequence.seed)
tasksGroup = groups.flatMap(group => shuffle(group, sequence.seed))
}
for (const c of tasksGroup)
await runSuiteChild(c, runner)
if (runner.runSuite) {
await runner.runSuite(suite)
}
else {
for (let tasksGroup of partitionSuiteChildren(suite)) {
if (tasksGroup[0].concurrent === true) {
const mutex = limit(runner.config.maxConcurrency)
await Promise.all(tasksGroup.map(c => mutex(() => runSuiteChild(c, runner))))
}
else {
const { sequence } = runner.config
if (sequence.shuffle || suite.shuffle) {
// run describe block independently from tests
const suites = tasksGroup.filter(group => group.type === 'suite')
const tests = tasksGroup.filter(group => group.type === 'test')
const groups = shuffle([suites, tests], sequence.seed)
tasksGroup = groups.flatMap(group => shuffle(group, sequence.seed))
}
for (const c of tasksGroup)
await runSuiteChild(c, runner)
}
}
}
catch (e) {
failTask(suite.result, e)
}
}
catch (e) {
failTask(suite.result, e)
}

try {
if (suite.mode !== 'repeats')
await callSuiteHook(suite, suite, 'afterAll', runner, [suite])
else if (suite.mode === 'repeats' && repeatCount - 1 === repeats && retry === retryCount)
await callSuiteHook(suite, suite, 'afterAll', runner, [suite])
await callCleanupHooks(beforeAllCleanups)
}
catch (e) {
failTask(suite.result, e)
}
try {
await callSuiteHook(suite, suite, 'afterAll', runner, [suite])
await callCleanupHooks(beforeAllCleanups)
}
catch (e) {
failTask(suite.result, e)
}

if (suite.mode === 'run') {
if (!hasTests(suite)) {
suite.result.state = 'fail'
if (!suite.result.error) {
const error = processError(new Error(`No test found in suite ${suite.name}`))
suite.result.error = error
suite.result.errors = [error]
}
}
else if (hasFailed(suite)) {
suite.result.state = 'fail'
}
else {
suite.result.state = 'pass'
if (suite.mode === 'run') {
if (!hasTests(suite)) {
suite.result.state = 'fail'
if (!suite.result.error) {
const error = processError(new Error(`No test found in suite ${suite.name}`))
suite.result.error = error
suite.result.errors = [error]
}
}
else if (hasFailed(suite)) {
suite.result.state = 'fail'
}
else {
suite.result.state = 'pass'
}
}

updateTask(suite, runner)

suite.result.duration = now() - start
updateTask(suite, runner)

await runner.onAfterRunSuite?.(suite)
suite.result.duration = now() - start

if (suite.result.state === 'pass')
break
await runner.onAfterRunSuite?.(suite)

if (suite.mode === 'repeats' && suite.result.state === 'fail')
break
}
if (suite.result.state === 'pass')
break
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions packages/runner/src/suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function createSuiteCollector(name: string, factory: SuiteFactory = () => { }, m
id: '',
name,
type: 'custom',
mode: self.only ? 'only' : self.skip ? 'skip' : self.todo ? 'todo' : self.repeats ? 'repeats' : 'run',
mode: self.only ? 'only' : self.skip ? 'skip' : self.todo ? 'todo' : 'run',
}
tasks.push(task)
return task
Expand Down Expand Up @@ -135,7 +135,6 @@ function createSuiteCollector(name: string, factory: SuiteFactory = () => { }, m
mode,
shuffle,
tasks: [],
repeats: suiteOptions?.repeats,
}

setHooks(suite, createSuiteHooks())
Expand Down Expand Up @@ -175,7 +174,7 @@ function createSuiteCollector(name: string, factory: SuiteFactory = () => { }, m

function createSuite() {
function suiteFn(this: Record<string, boolean | undefined>, name: string, factory?: SuiteFactory, options?: number | TestOptions) {
const mode: RunMode = this.only ? 'only' : this.skip ? 'skip' : this.todo ? 'todo' : this.repeats ? 'repeats' : 'run'
const mode: RunMode = this.only ? 'only' : this.skip ? 'skip' : this.todo ? 'todo' : 'run'
return createSuiteCollector(name, factory, mode, this.concurrent, this.shuffle, options)
}

Expand Down
2 changes: 1 addition & 1 deletion packages/runner/src/types/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Awaitable } from '@vitest/utils'
import type { ChainableFunction } from '../utils/chain'
import type { ErrorWithDiff } from '../utils/error'

export type RunMode = 'run' | 'skip' | 'only' | 'todo' | 'repeats'
export type RunMode = 'run' | 'skip' | 'only' | 'todo'
export type TaskState = RunMode | 'pass' | 'fail'

export interface TaskBase {
Expand Down
2 changes: 1 addition & 1 deletion packages/runner/src/utils/collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function interpretTaskModes(suite: Suite, namePattern?: string | RegExp,

// if all subtasks are skipped, mark as skip
if (suite.mode === 'run') {
if (suite.tasks.length && suite.tasks.every(i => i.mode !== 'run' && i.mode !== 'repeats'))
if (suite.tasks.length && suite.tasks.every(i => i.mode !== 'run' && !i.repeats))
suite.mode = 'skip'
}
}
Expand Down
1 change: 0 additions & 1 deletion packages/vitest/src/node/reporters/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const StatusMap: Record<TaskState, Status> = {
run: 'pending',
skip: 'skipped',
todo: 'todo',
repeats: 'pending',
}

interface FormattedAssertionResult {
Expand Down
4 changes: 2 additions & 2 deletions packages/vitest/src/node/reporters/renderers/listRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ export function renderTree(tasks: Task[], options: ListRendererOptions, level =
if (task.mode === 'skip' || task.mode === 'todo')
suffix += ` ${c.dim(c.gray('[skipped]'))}`

if (task.type === 'suite' && task.tasks.find(t => t.mode === 'repeats'))
suffix += ` ${c.dim(c.gray('[repeated]'))}`
if (task.type === 'test' && task.result?.repeatCount && task.result.repeatCount > 1)
suffix += c.yellow(` (repeat x${task.result.repeatCount})`)

if (task.result?.duration != null) {
if (task.result.duration > DURATION_LONG)
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/typecheck/collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ interface LocalCallDefinition {
end: number
name: string
type: 'suite' | 'test'
mode: 'run' | 'skip' | 'only' | 'todo' | 'repeats'
mode: 'run' | 'skip' | 'only' | 'todo'
task: ParsedSuite | ParsedFile | ParsedTest
}

Expand Down
2 changes: 1 addition & 1 deletion test/core/test/repeats.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('testing it/test', () => {
test.repeats.fails('test 3', () => {
testNumbers.push(3)
expect(testNumbers).toStrictEqual(result)
})
}, { repeats: 1 })

afterAll(() => {
result.push(3)
Expand Down

0 comments on commit 1fa8795

Please sign in to comment.