Skip to content

Commit f5d7999

Browse files
authored
refactor(api): rename TestFile to TestModule (#6356)
1 parent d09f00c commit f5d7999

File tree

6 files changed

+144
-111
lines changed

6 files changed

+144
-111
lines changed

docs/advanced/reporters.md

+34-33
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ You can get access to this API by calling `vitest.state.getReportedEntity(runner
6666
```ts twoslash
6767
import type { Vitest } from 'vitest/node'
6868
import type { RunnerTestFile } from 'vitest'
69-
import type { Reporter, TestFile } from 'vitest/reporters'
69+
import type { Reporter, TestModule } from 'vitest/reporters'
7070

7171
class MyReporter implements Reporter {
7272
ctx!: Vitest
@@ -77,9 +77,10 @@ class MyReporter implements Reporter {
7777

7878
onFinished(files: RunnerTestFile[]) {
7979
for (const fileTask of files) {
80-
const testFile = this.ctx.state.getReportedEntity(fileTask) as TestFile
81-
for (const task of testFile.children) {
82-
// ^?
80+
// note that the old task implementation uses "file" instead of "module"
81+
const testModule = this.ctx.state.getReportedEntity(fileTask) as TestModule
82+
for (const task of testModule.children) {
83+
// ^?
8384
console.log('finished', task.type, task.fullName)
8485
}
8586
}
@@ -107,9 +108,9 @@ declare class TestCase {
107108
*/
108109
readonly project: TestProject
109110
/**
110-
* Direct reference to the test file where the test is defined.
111+
* Direct reference to the test module where the test is defined.
111112
*/
112-
readonly file: TestFile
113+
readonly module: TestModule
113114
/**
114115
* Name of the test.
115116
*/
@@ -121,18 +122,18 @@ declare class TestCase {
121122
/**
122123
* Unique identifier.
123124
* This ID is deterministic and will be the same for the same test across multiple runs.
124-
* The ID is based on the project name, file path and test position.
125+
* The ID is based on the project name, module id and test position.
125126
*/
126127
readonly id: string
127128
/**
128-
* Location in the file where the test was defined.
129+
* Location in the module where the test was defined.
129130
* Locations are collected only if `includeTaskLocation` is enabled in the config.
130131
*/
131132
readonly location: { line: number; column: number } | undefined
132133
/**
133-
* Parent suite. If the test was called directly inside the file, the parent will be the file.
134+
* Parent suite. If the test was called directly inside the module, the parent will be the module itself.
134135
*/
135-
readonly parent: TestSuite | TestFile
136+
readonly parent: TestSuite | TestModule
136137
/**
137138
* Options that test was initiated with.
138139
*/
@@ -241,9 +242,9 @@ declare class TestSuite {
241242
*/
242243
readonly project: TestProject
243244
/**
244-
* Direct reference to the test file where the suite is defined.
245+
* Direct reference to the test module where the suite is defined.
245246
*/
246-
readonly file: TestFile
247+
readonly module: TestModule
247248
/**
248249
* Name of the suite.
249250
*/
@@ -255,11 +256,11 @@ declare class TestSuite {
255256
/**
256257
* Unique identifier.
257258
* This ID is deterministic and will be the same for the same test across multiple runs.
258-
* The ID is based on the project name, file path and test position.
259+
* The ID is based on the project name, module id and test position.
259260
*/
260261
readonly id: string
261262
/**
262-
* Location in the file where the suite was defined.
263+
* Location in the module where the suite was defined.
263264
* Locations are collected only if `includeTaskLocation` is enabled in the config.
264265
*/
265266
readonly location: { line: number; column: number } | undefined
@@ -274,20 +275,20 @@ declare class TestSuite {
274275
}
275276
```
276277

277-
### TestFile
278+
### TestModule
278279

279-
`TestFile` represents a single file that contains suites and tests.
280+
`TestModule` represents a single file that contains suites and tests.
280281

281282
```ts
282-
declare class TestFile extends SuiteImplementation {
283-
readonly type = 'file'
283+
declare class TestModule extends SuiteImplementation {
284+
readonly type = 'module'
284285
/**
285286
* Task instance.
286287
* @experimental Public task API is experimental and does not follow semver.
287288
*/
288289
readonly task: RunnerTestFile
289290
/**
290-
* Collection of suites and tests that are part of this file.
291+
* Collection of suites and tests that are part of this module.
291292
*/
292293
readonly children: TestCollection
293294
/**
@@ -297,13 +298,13 @@ declare class TestFile extends SuiteImplementation {
297298
*/
298299
readonly moduleId: string
299300
/**
300-
* Useful information about the file like duration, memory usage, etc.
301-
* If the file was not executed yet, all diagnostic values will return `0`.
301+
* Useful information about the module like duration, memory usage, etc.
302+
* If the module was not executed yet, all diagnostic values will return `0`.
302303
*/
303-
diagnostic(): FileDiagnostic
304+
diagnostic(): ModuleDiagnostic
304305
}
305306

306-
export interface FileDiagnostic {
307+
export interface ModuleDiagnostic {
307308
/**
308309
* The time it takes to import and initiate an environment.
309310
*/
@@ -313,16 +314,16 @@ export interface FileDiagnostic {
313314
*/
314315
prepareDuration: number
315316
/**
316-
* The time it takes to import the test file.
317-
* This includes importing everything in the file and executing suite callbacks.
317+
* The time it takes to import the test module.
318+
* This includes importing everything in the module and executing suite callbacks.
318319
*/
319320
collectDuration: number
320321
/**
321-
* The time it takes to import the setup file.
322+
* The time it takes to import the setup module.
322323
*/
323324
setupDuration: number
324325
/**
325-
* Accumulated duration of all tests and hooks in the file.
326+
* Accumulated duration of all tests and hooks in the module.
326327
*/
327328
duration: number
328329
}
@@ -366,22 +367,22 @@ declare class TestCollection {
366367
}
367368
```
368369

369-
For example, you can iterate over all tests inside a file by calling `testFile.children.allTests()`:
370+
For example, you can iterate over all tests inside a module by calling `testModule.children.allTests()`:
370371

371372
```ts
372-
function onFileCollected(testFile: TestFile): void {
373-
console.log('collecting tests in', testFile.moduleId)
373+
function onFileCollected(testModule: TestModule): void {
374+
console.log('collecting tests in', testModule.moduleId)
374375

375-
// iterate over all tests and suites in the file
376-
for (const task of testFile.children.allTests()) {
376+
// iterate over all tests and suites in the module
377+
for (const task of testModule.children.allTests()) {
377378
console.log('collected', task.type, task.fullName)
378379
}
379380
}
380381
```
381382

382383
### TestProject
383384

384-
`TestProject` is a project assosiated with the file. Every test and suite inside that file will reference the same project.
385+
`TestProject` is a project assosiated with the module. Every test and suite inside that module will reference the same project.
385386

386387
Project is useful to get the configuration or provided context.
387388

packages/vitest/src/node/reporters/index.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import type { BaseOptions, BaseReporter } from './base'
1313
import type { HTMLOptions } from './html'
1414
import type { BlobOptions } from './blob'
1515
import { BlobReporter } from './blob'
16+
import { TestModule as _TestFile } from './reported-tasks'
17+
import type { ModuleDiagnostic as _FileDiagnostic } from './reported-tasks'
1618

1719
export {
1820
DefaultReporter,
@@ -28,20 +30,27 @@ export {
2830
}
2931
export type { BaseReporter, Reporter }
3032

31-
export { TestCase, TestFile, TestSuite } from './reported-tasks'
33+
export { TestCase, TestModule, TestSuite } from './reported-tasks'
34+
/**
35+
* @deprecated Use `TestModule` instead
36+
*/
37+
export const TestFile = _TestFile
3238
export type { TestProject } from '../reported-workspace-project'
3339
export type {
3440
TestCollection,
3541

3642
TaskOptions,
3743
TestDiagnostic,
38-
FileDiagnostic,
3944

4045
TestResult,
4146
TestResultFailed,
4247
TestResultPassed,
4348
TestResultSkipped,
4449
} from './reported-tasks'
50+
/**
51+
* @deprecated Use `ModuleDiagnostic` instead
52+
*/
53+
export type FileDiagnostic = _FileDiagnostic
4554

4655
export type {
4756
JsonAssertionResult,

0 commit comments

Comments
 (0)