Skip to content
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/advanced/api/test-case.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ Checks if the test did not fail the suite. If the test is not finished yet or wa
function meta(): TaskMeta
```

Custom metadata that was attached to the test during its execution. The meta can be attached by assigning a property to the `ctx.task.meta` object during a test run:
Custom [metadata](/advanced/metadata) that was attached to the test during its execution. The meta can be attached by assigning a property to the `ctx.task.meta` object during a test run:

```ts {3,6}
import { test } from 'vitest'
Expand Down
32 changes: 32 additions & 0 deletions docs/advanced/api/test-module.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,33 @@ function state(): TestModuleState

Works the same way as [`testSuite.state()`](/advanced/api/test-suite#state), but can also return `queued` if module wasn't executed yet.

## meta <Version>3.1.0</Version> {#meta}

```ts
function meta(): TaskMeta
```

Custom [metadata](/advanced/metadata) that was attached to the module during its execution or collection. The meta can be attached by assigning a property to the `task.meta` object during a test run:

```ts {5,10}
import { test } from 'vitest'
describe('the validation works correctly', (task) => {
// assign "decorated" during collection
task.file.meta.decorated = false
test('some test', ({ task }) => {
// assign "decorated" during test run, it will be available
// only in onTestCaseReady hook
task.file.meta.decorated = false
})
})
```

:::tip
If metadata was attached during collection (outside of the `test` function), then it will be available in [`onTestModuleCollected`](./reporters#ontestmodulecollected) hook in the custom reporter.
:::

## diagnostic

```ts
Expand Down Expand Up @@ -63,5 +90,10 @@ interface ModuleDiagnostic {
* Accumulated duration of all tests and hooks in the module.
*/
readonly duration: number
/**
* The amount of memory used by the module in bytes.
* This value is only available if the test was executed with `logHeapUsage` flag.
*/
readonly heap: number | undefined
}
```
27 changes: 27 additions & 0 deletions docs/advanced/api/test-suite.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,30 @@ describe('collection failed', () => {
::: warning
Note that errors are serialized into simple objects: `instanceof Error` will always return `false`.
:::

## meta <Version>3.1.0</Version> {#meta}

```ts
function meta(): TaskMeta
```

Custom [metadata](/advanced/metadata) that was attached to the suite during its execution or collection. The meta can be attached by assigning a property to the `task.meta` object during a test run:

```ts {5,10}
import { test } from 'vitest'
describe('the validation works correctly', (task) => {
// assign "decorated" during collection
task.meta.decorated = false
test('some test', ({ task }) => {
// assign "decorated" during test run, it will be available
// only in onTestCaseReady hook
task.suite.meta.decorated = false
})
})
```

:::tip
If metadata was attached during collection (outside of the `test` function), then it will be available in [`onTestModuleCollected`](./reporters#ontestmodulecollected) hook in the custom reporter.
:::
43 changes: 24 additions & 19 deletions packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,25 +502,7 @@ export class Vitest {
await this._testRun.start(specifications).catch(noop)

for (const file of files) {
const project = this.getProjectByName(file.projectName || '')
await this._testRun.enqueued(project, file).catch(noop)
await this._testRun.collected(project, [file]).catch(noop)

const logs: UserConsoleLog[] = []

const { packs, events } = convertTasksToEvents(file, (task) => {
if (task.logs) {
logs.push(...task.logs)
}
})

logs.sort((log1, log2) => log1.time - log2.time)

for (const log of logs) {
await this._testRun.log(log).catch(noop)
}

await this._testRun.updated(packs, events).catch(noop)
await this._reportFileTask(file)
}

if (hasFailed(files)) {
Expand All @@ -538,6 +520,29 @@ export class Vitest {
}
}

/** @internal */
public async _reportFileTask(file: File): Promise<void> {
const project = this.getProjectByName(file.projectName || '')
await this._testRun.enqueued(project, file).catch(noop)
await this._testRun.collected(project, [file]).catch(noop)

const logs: UserConsoleLog[] = []

const { packs, events } = convertTasksToEvents(file, (task) => {
if (task.logs) {
logs.push(...task.logs)
}
})

logs.sort((log1, log2) => log1.time - log2.time)

for (const log of logs) {
await this._testRun.log(log).catch(noop)
}

await this._testRun.updated(packs, events).catch(noop)
}

async collect(filters?: string[]): Promise<TestRunResult> {
const files = await this.specifications.getRelevantTestSpecifications(filters)

Expand Down
Loading
Loading