Skip to content

Commit 7531c29

Browse files
authoredJun 19, 2023
feat(reporters): show full test suite when testing 1 spec file at a time (#3543)
1 parent 969dcc1 commit 7531c29

File tree

7 files changed

+161
-0
lines changed

7 files changed

+161
-0
lines changed
 

‎packages/vitest/src/node/reporters/default.ts

+11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ import { createListRenderer } from './renderers/listRenderer'
77
export class DefaultReporter extends BaseReporter {
88
renderer?: ReturnType<typeof createListRenderer>
99
rendererOptions: ListRendererOptions = {} as any
10+
private renderSucceedDefault?: boolean
11+
12+
onPathsCollected(paths: string[] = []) {
13+
if (this.isTTY) {
14+
if (this.renderSucceedDefault === undefined)
15+
this.renderSucceedDefault = !!this.rendererOptions.renderSucceed
16+
17+
if (this.renderSucceedDefault !== true)
18+
this.rendererOptions.renderSucceed = paths.length <= 1
19+
}
20+
}
1021

1122
async onTestRemoved(trigger?: string) {
1223
await this.stopListRender()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { DefaultReporter } from '../../../../packages/vitest/src/node/reporters/default'
2+
3+
export default class MockDefaultReporter extends DefaultReporter {
4+
isTTY = true
5+
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { describe, expect, test } from 'vitest'
2+
3+
describe('a passed', () => {
4+
test.each([1, 2, 3])('a%d test', (d) => {
5+
expect(d).toBe(d)
6+
})
7+
describe('nested a', () => {
8+
test.each([1, 2, 3])('nested a%d test', (d) => {
9+
expect(d).toBe(d)
10+
})
11+
})
12+
})
13+
14+
describe('a failed', () => {
15+
test.each([1, 2, 3])('a failed %d test', (d) => {
16+
expect(d).toBe(d)
17+
})
18+
test('a failed test', () => {
19+
expect(1).toBe(2)
20+
})
21+
describe('nested a failed', () => {
22+
test.each([1, 2, 3])('nested a failed %d test', (d) => {
23+
expect(d).toBe(d)
24+
})
25+
})
26+
})
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { describe, expect, test } from 'vitest'
2+
3+
describe('b1 passed', () => {
4+
test.each([1, 2, 3])('b%d test', (d) => {
5+
expect(d).toBe(d)
6+
})
7+
describe('nested b', () => {
8+
test.each([1, 2, 3])('nested b%d test', (d) => {
9+
expect(d).toBe(d)
10+
})
11+
})
12+
})
13+
14+
describe('b1 failed', () => {
15+
test.each([1, 2, 3])('b%d test', (d) => {
16+
expect(d).toBe(d)
17+
})
18+
test('b failed test', () => {
19+
expect(1).toBe(2)
20+
})
21+
describe('nested b', () => {
22+
test.each([1, 2, 3])('nested b%d test', (d) => {
23+
expect(d).toBe(d)
24+
})
25+
})
26+
})
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { describe, expect, test } from 'vitest'
2+
3+
describe('b2 passed', () => {
4+
test.each([1, 2, 3])('b%d test', (d) => {
5+
expect(d).toBe(d)
6+
})
7+
describe('nested b', () => {
8+
test.each([1, 2, 3])('nested b%d test', (d) => {
9+
expect(d).toBe(d)
10+
})
11+
})
12+
})
13+
14+
describe('b2 failed', () => {
15+
test.each([1, 2, 3])('b%d test', (d) => {
16+
expect(d).toBe(d)
17+
})
18+
test('b failed test', () => {
19+
expect(1).toBe(2)
20+
})
21+
describe('nested b', () => {
22+
test.each([1, 2, 3])('nested b%d test', (d) => {
23+
expect(d).toBe(d)
24+
})
25+
})
26+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { defineConfig } from 'vitest/config'
2+
3+
process.stdin.isTTY = true
4+
process.stdin.setRawMode = () => process.stdin
5+
6+
export default defineConfig({
7+
test: {
8+
reporters: './MockReporter',
9+
},
10+
})

‎test/reporters/tests/default.test.ts

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import path from 'pathe'
2+
import { describe, expect, test } from 'vitest'
3+
import { runVitestCli } from '../../test-utils'
4+
5+
const resolve = (id = '') => path.resolve(__dirname, '../fixtures/default', id)
6+
async function run(fileFilter: string[], watch = false, ...args: string[]) {
7+
return runVitestCli(
8+
...fileFilter,
9+
'--root',
10+
resolve(),
11+
watch ? '--watch' : '--run',
12+
...args,
13+
)
14+
}
15+
16+
describe('default reporter', async () => {
17+
test('normal', async () => {
18+
const { stdout } = await run(['b1.test.ts', 'b2.test.ts'])
19+
expect(stdout).contain('✓ b2 test')
20+
expect(stdout).not.contain('✓ nested b1 test')
21+
expect(stdout).contain('× b failed test')
22+
})
23+
24+
test('show full test suite when only one file', async () => {
25+
const { stdout } = await run(['a.test.ts'])
26+
expect(stdout).contain('✓ a1 test')
27+
expect(stdout).contain('✓ nested a3 test')
28+
expect(stdout).contain('× a failed test')
29+
expect(stdout).contain('nested a failed 1 test')
30+
})
31+
32+
test('rerun should undo', async () => {
33+
const vitest = await run([], true, '-t', 'passed')
34+
35+
// one file
36+
vitest.write('p')
37+
await vitest.waitForStdout('Input filename pattern')
38+
vitest.write('a\n')
39+
await vitest.waitForStdout('Filename pattern: a')
40+
await vitest.waitForStdout('Waiting for file changes')
41+
expect(vitest.stdout).contain('✓ a1 test')
42+
expect(vitest.stdout).contain('✓ nested a3 test')
43+
44+
// rerun and two files
45+
vitest.write('p')
46+
await vitest.waitForStdout('Input filename pattern')
47+
vitest.write('b\n')
48+
await vitest.waitForStdout('Filename pattern: b')
49+
await vitest.waitForStdout('PASS Waiting for file changes...')
50+
expect(vitest.stdout).toContain('RERUN')
51+
expect(vitest.stdout).toContain('b1.test.ts')
52+
expect(vitest.stdout).toContain('b2.test.ts')
53+
expect(vitest.stdout).not.toContain('nested b1 test')
54+
expect(vitest.stdout).not.toContain('b1 test')
55+
expect(vitest.stdout).not.toContain('b2 test')
56+
})
57+
}, 120000)

0 commit comments

Comments
 (0)
Please sign in to comment.