Skip to content

Commit 0ef1361

Browse files
authored
test: add toTestSpecifications tests (#9471)
1 parent 96eb928 commit 0ef1361

File tree

1 file changed

+93
-45
lines changed

1 file changed

+93
-45
lines changed

test/cli/test/reported-tasks.test.ts

Lines changed: 93 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,67 @@
11
import type { RunnerTestFile } from 'vitest'
2-
import type { TestProject } from 'vitest/node'
3-
import type { StateManager } from 'vitest/src/node/state.js'
4-
import type { TestCase, TestCollection, TestModule } from '../../../packages/vitest/src/node/reporters/reported-tasks'
2+
import type { TestCase, TestCollection, TestModule, TestProject, Vitest } from 'vitest/node'
53
import { resolve } from 'pathe'
6-
import { beforeAll, expect, it } from 'vitest'
4+
import { it as baseTest, expect } from 'vitest'
75
import { runVitest } from '../../test-utils'
86

9-
const now = new Date()
10-
const collectedTestModules: TestModule[] = []
11-
let state: StateManager
12-
let project: TestProject
13-
let files: RunnerTestFile[]
14-
let testModule: TestModule
15-
167
const root = resolve(__dirname, '..', 'fixtures', 'reported-tasks')
178

18-
beforeAll(async () => {
19-
const { ctx } = await runVitest({
20-
root,
21-
include: ['**/*.test.ts'],
22-
reporters: [
23-
'verbose',
24-
{
25-
onTestModuleCollected(testModule) {
26-
collectedTestModules.push(testModule)
27-
},
28-
},
29-
],
30-
includeTaskLocation: true,
31-
logHeapUsage: true,
32-
})
33-
state = ctx!.state
34-
project = ctx!.getRootProject()
35-
files = state.getFiles()
36-
expect(files).toHaveLength(1)
37-
testModule = state.getReportedEntity(files[0])! as TestModule
38-
expect(testModule).toBeDefined()
39-
expect(testModule).toBe(collectedTestModules[0])
9+
const it = baseTest.extend<{
10+
ctx: Vitest
11+
files: RunnerTestFile[]
12+
testModule: TestModule
13+
project: TestProject
14+
}>({
15+
ctx: [
16+
async ({}, use) => {
17+
const collectedTestModules: TestModule[] = []
18+
const { ctx } = await runVitest({
19+
root,
20+
include: ['**/*.test.ts'],
21+
reporters: [
22+
'verbose',
23+
{
24+
onTestModuleCollected(testModule) {
25+
collectedTestModules.push(testModule)
26+
},
27+
},
28+
],
29+
includeTaskLocation: true,
30+
logHeapUsage: true,
31+
})
32+
expect(collectedTestModules).toHaveLength(1)
33+
await use(ctx!)
34+
},
35+
{ scope: 'file' },
36+
],
37+
files: [
38+
async ({ ctx }, use) => {
39+
const state = ctx!.state
40+
const files = state.getFiles()
41+
await use(files)
42+
},
43+
{ scope: 'file' },
44+
],
45+
testModule: [
46+
async ({ ctx, files }, use) => {
47+
const state = ctx!.state
48+
const testModule = state.getReportedEntity(files[0])! as TestModule
49+
await use(testModule)
50+
},
51+
{ scope: 'file' },
52+
],
53+
project: [
54+
async ({ ctx }, use) => {
55+
const project = ctx!.getRootProject()
56+
await use(project)
57+
},
58+
{ scope: 'file' },
59+
],
4060
})
4161

42-
it('correctly reports a file', () => {
62+
const now = new Date()
63+
64+
it('correctly reports a file', ({ testModule, files, project }) => {
4365
// suite properties not available on file
4466
expect(testModule).not.toHaveProperty('parent')
4567
expect(testModule).not.toHaveProperty('options')
@@ -80,7 +102,7 @@ it('correctly reports a file', () => {
80102
expect(diagnostic.setupDuration).toBe(0)
81103
})
82104

83-
it('correctly reports a passed test', () => {
105+
it('correctly reports a passed test', ({ testModule, files }) => {
84106
const passedTest = findTest(testModule.children, 'runs a test')
85107
expect(passedTest.type).toBe('test')
86108
expect(passedTest.task).toBe(files[0].tasks[0])
@@ -113,7 +135,7 @@ it('correctly reports a passed test', () => {
113135
expect(diagnostic.repeatCount).toBe(0)
114136
})
115137

116-
it('correctly reports failed test', () => {
138+
it('correctly reports failed test', ({ testModule, files }) => {
117139
const passedTest = findTest(testModule.children, 'fails a test')
118140
expect(passedTest.type).toBe('test')
119141
expect(passedTest.task).toBe(files[0].tasks[1])
@@ -160,7 +182,7 @@ it('correctly reports failed test', () => {
160182
expect(diagnostic.repeatCount).toBe(0)
161183
})
162184

163-
it('correctly reports a skipped test', () => {
185+
it('correctly reports a skipped test', ({ testModule }) => {
164186
const optionTestCase = findTest(testModule.children, 'skips an option test')
165187
expect(optionTestCase.result()).toEqual({
166188
state: 'skipped',
@@ -197,7 +219,7 @@ it('correctly reports a skipped test', () => {
197219
})
198220
})
199221

200-
it('correctly reports multiple failures', () => {
222+
it('correctly reports multiple failures', ({ testModule }) => {
201223
const testCase = findTest(testModule.children, 'fails multiple times')
202224
const result = testCase.result()!
203225
expect(result).toBeDefined()
@@ -211,7 +233,7 @@ it('correctly reports multiple failures', () => {
211233
})
212234
})
213235

214-
it('correctly reports test assigned options', () => {
236+
it('correctly reports test assigned options', ({ testModule }) => {
215237
const testOptionSkip = findTest(testModule.children, 'skips an option test')
216238
expect(testOptionSkip.options.mode).toBe('skip')
217239
const testModifierSkip = findTest(testModule.children, 'skips a .modifier test')
@@ -229,14 +251,14 @@ it('correctly reports test assigned options', () => {
229251
expect(testInsideSkippedDescribe.options.mode).toBe('skip')
230252
})
231253

232-
it('correctly reports retried tests', () => {
254+
it('correctly reports retried tests', ({ testModule }) => {
233255
const testRetry = findTest(testModule.children, 'retries a test')
234256
expect(testRetry.options.retry).toBe(5)
235257
expect(testRetry.options.repeats).toBeUndefined()
236258
expect(testRetry.result()!.state).toBe('failed')
237259
})
238260

239-
it('correctly reports flaky tests', () => {
261+
it('correctly reports flaky tests', ({ testModule }) => {
240262
const testFlaky = findTest(testModule.children, 'retries a test with success')
241263
const diagnostic = testFlaky.diagnostic()!
242264
expect(diagnostic.flaky).toBe(true)
@@ -247,7 +269,7 @@ it('correctly reports flaky tests', () => {
247269
expect(result.errors).toHaveLength(2)
248270
})
249271

250-
it('correctly reports repeated tests', () => {
272+
it('correctly reports repeated tests', ({ testModule }) => {
251273
const testRepeated = findTest(testModule.children, 'repeats a test')
252274
const diagnostic = testRepeated.diagnostic()!
253275
expect(diagnostic.flaky).toBe(false)
@@ -258,13 +280,13 @@ it('correctly reports repeated tests', () => {
258280
expect(result.errors).toHaveLength(6)
259281
})
260282

261-
it('correctly passed down metadata', () => {
283+
it('correctly passed down metadata', ({ testModule }) => {
262284
const testMetadata = findTest(testModule.children, 'registers a metadata')
263285
const meta = testMetadata.meta()
264286
expect(meta).toHaveProperty('key', 'value')
265287
})
266288

267-
it('correctly builds the full name', () => {
289+
it('correctly builds the full name', ({ testModule }) => {
268290
const suiteTopLevel = testModule.children.suites().next().value!
269291
const suiteSecondLevel = suiteTopLevel.children.suites().next().value!
270292
const test = suiteSecondLevel.children.at(0) as TestCase
@@ -273,7 +295,7 @@ it('correctly builds the full name', () => {
273295
expect(suiteSecondLevel.fullName).toBe('a group > a nested group')
274296
})
275297

276-
it('correctly reports import durations', () => {
298+
it('correctly reports import durations', ({ testModule }) => {
277299
const diagnostic = testModule.diagnostic()
278300

279301
const filePath = resolve(root, './1_first.test.ts')
@@ -282,6 +304,32 @@ it('correctly reports import durations', () => {
282304
expect(importDuration.totalTime).toBeGreaterThan(0)
283305
})
284306

307+
it('can create new test specifications', ({ testModule }) => {
308+
const moduleSpec = testModule.toTestSpecification()
309+
expect(moduleSpec.moduleId).toBe(testModule.moduleId)
310+
expect(moduleSpec.testIds).toBeUndefined()
311+
expect(moduleSpec.project).toBe(testModule.project)
312+
313+
const testSuite = [...testModule.children.suites()][0]
314+
const suiteSpec = testSuite.toTestSpecification()
315+
expect(suiteSpec.moduleId).toBe(testModule.moduleId)
316+
expect(suiteSpec.testIds).toEqual([
317+
'-1008553841_11_0',
318+
'-1008553841_11_1',
319+
'-1008553841_11_2_0',
320+
'-1008553841_11_2_1',
321+
'-1008553841_11_2_2',
322+
'-1008553841_11_2_3',
323+
])
324+
expect(suiteSpec.project).toBe(testModule.project)
325+
326+
const testCase = testSuite.children.at(0) as TestCase
327+
const caseSpec = testCase.toTestSpecification()
328+
expect(caseSpec.moduleId).toBe(testModule.moduleId)
329+
expect(caseSpec.testIds).toEqual(['-1008553841_11_0'])
330+
expect(caseSpec.project).toBe(testModule.project)
331+
})
332+
285333
function date(time: Date) {
286334
return `${time.getDate()}/${time.getMonth() + 1}/${time.getFullYear()}`
287335
}

0 commit comments

Comments
 (0)