Skip to content

Commit

Permalink
feat(reporters): show full test suite when testing 1 spec file at a t…
Browse files Browse the repository at this point in the history
…ime (#3543)
  • Loading branch information
Dunqing committed Jun 19, 2023
1 parent 969dcc1 commit 7531c29
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/vitest/src/node/reporters/default.ts
Expand Up @@ -7,6 +7,17 @@ import { createListRenderer } from './renderers/listRenderer'
export class DefaultReporter extends BaseReporter {
renderer?: ReturnType<typeof createListRenderer>
rendererOptions: ListRendererOptions = {} as any
private renderSucceedDefault?: boolean

onPathsCollected(paths: string[] = []) {
if (this.isTTY) {
if (this.renderSucceedDefault === undefined)
this.renderSucceedDefault = !!this.rendererOptions.renderSucceed

if (this.renderSucceedDefault !== true)
this.rendererOptions.renderSucceed = paths.length <= 1
}
}

async onTestRemoved(trigger?: string) {
await this.stopListRender()
Expand Down
5 changes: 5 additions & 0 deletions test/reporters/fixtures/default/MockReporter.ts
@@ -0,0 +1,5 @@
import { DefaultReporter } from '../../../../packages/vitest/src/node/reporters/default'

export default class MockDefaultReporter extends DefaultReporter {
isTTY = true
}
26 changes: 26 additions & 0 deletions test/reporters/fixtures/default/a.test.ts
@@ -0,0 +1,26 @@
import { describe, expect, test } from 'vitest'

describe('a passed', () => {
test.each([1, 2, 3])('a%d test', (d) => {
expect(d).toBe(d)
})
describe('nested a', () => {
test.each([1, 2, 3])('nested a%d test', (d) => {
expect(d).toBe(d)
})
})
})

describe('a failed', () => {
test.each([1, 2, 3])('a failed %d test', (d) => {
expect(d).toBe(d)
})
test('a failed test', () => {
expect(1).toBe(2)
})
describe('nested a failed', () => {
test.each([1, 2, 3])('nested a failed %d test', (d) => {
expect(d).toBe(d)
})
})
})
26 changes: 26 additions & 0 deletions test/reporters/fixtures/default/b1.test.ts
@@ -0,0 +1,26 @@
import { describe, expect, test } from 'vitest'

describe('b1 passed', () => {
test.each([1, 2, 3])('b%d test', (d) => {
expect(d).toBe(d)
})
describe('nested b', () => {
test.each([1, 2, 3])('nested b%d test', (d) => {
expect(d).toBe(d)
})
})
})

describe('b1 failed', () => {
test.each([1, 2, 3])('b%d test', (d) => {
expect(d).toBe(d)
})
test('b failed test', () => {
expect(1).toBe(2)
})
describe('nested b', () => {
test.each([1, 2, 3])('nested b%d test', (d) => {
expect(d).toBe(d)
})
})
})
26 changes: 26 additions & 0 deletions test/reporters/fixtures/default/b2.test.ts
@@ -0,0 +1,26 @@
import { describe, expect, test } from 'vitest'

describe('b2 passed', () => {
test.each([1, 2, 3])('b%d test', (d) => {
expect(d).toBe(d)
})
describe('nested b', () => {
test.each([1, 2, 3])('nested b%d test', (d) => {
expect(d).toBe(d)
})
})
})

describe('b2 failed', () => {
test.each([1, 2, 3])('b%d test', (d) => {
expect(d).toBe(d)
})
test('b failed test', () => {
expect(1).toBe(2)
})
describe('nested b', () => {
test.each([1, 2, 3])('nested b%d test', (d) => {
expect(d).toBe(d)
})
})
})
10 changes: 10 additions & 0 deletions test/reporters/fixtures/default/vitest.config.ts
@@ -0,0 +1,10 @@
import { defineConfig } from 'vitest/config'

process.stdin.isTTY = true
process.stdin.setRawMode = () => process.stdin

export default defineConfig({
test: {
reporters: './MockReporter',
},
})
57 changes: 57 additions & 0 deletions test/reporters/tests/default.test.ts
@@ -0,0 +1,57 @@
import path from 'pathe'
import { describe, expect, test } from 'vitest'
import { runVitestCli } from '../../test-utils'

const resolve = (id = '') => path.resolve(__dirname, '../fixtures/default', id)
async function run(fileFilter: string[], watch = false, ...args: string[]) {
return runVitestCli(
...fileFilter,
'--root',
resolve(),
watch ? '--watch' : '--run',
...args,
)
}

describe('default reporter', async () => {
test('normal', async () => {
const { stdout } = await run(['b1.test.ts', 'b2.test.ts'])
expect(stdout).contain('✓ b2 test')
expect(stdout).not.contain('✓ nested b1 test')
expect(stdout).contain('× b failed test')
})

test('show full test suite when only one file', async () => {
const { stdout } = await run(['a.test.ts'])
expect(stdout).contain('✓ a1 test')
expect(stdout).contain('✓ nested a3 test')
expect(stdout).contain('× a failed test')
expect(stdout).contain('nested a failed 1 test')
})

test('rerun should undo', async () => {
const vitest = await run([], true, '-t', 'passed')

// one file
vitest.write('p')
await vitest.waitForStdout('Input filename pattern')
vitest.write('a\n')
await vitest.waitForStdout('Filename pattern: a')
await vitest.waitForStdout('Waiting for file changes')
expect(vitest.stdout).contain('✓ a1 test')
expect(vitest.stdout).contain('✓ nested a3 test')

// rerun and two files
vitest.write('p')
await vitest.waitForStdout('Input filename pattern')
vitest.write('b\n')
await vitest.waitForStdout('Filename pattern: b')
await vitest.waitForStdout('PASS Waiting for file changes...')
expect(vitest.stdout).toContain('RERUN')
expect(vitest.stdout).toContain('b1.test.ts')
expect(vitest.stdout).toContain('b2.test.ts')
expect(vitest.stdout).not.toContain('nested b1 test')
expect(vitest.stdout).not.toContain('b1 test')
expect(vitest.stdout).not.toContain('b2 test')
})
}, 120000)

0 comments on commit 7531c29

Please sign in to comment.