-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move
onTaskUpdate
to its own TaskParser
- Loading branch information
1 parent
e39d1f9
commit b7a381c
Showing
7 changed files
with
370 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import type { Custom, File, Task, TaskResultPack, Test } from '@vitest/runner' | ||
import type { Vitest } from '../core' | ||
import { getTests } from '@vitest/runner/utils' | ||
|
||
export interface HookOptions { | ||
name: string | ||
file: File | ||
id: File['id'] | Test['id'] | ||
type: Task['type'] | ||
} | ||
|
||
export class TaskParser { | ||
ctx!: Vitest | ||
|
||
onInit(ctx: Vitest) { | ||
this.ctx = ctx | ||
} | ||
|
||
onHookStart(_options: HookOptions) {} | ||
onHookEnd(_options: HookOptions) {} | ||
|
||
onTestStart(_test: Test | Custom) {} | ||
onTestFinished(_test: Test | Custom) {} | ||
|
||
onTestFilePrepare(_file: File) {} | ||
onTestFileFinished(_file: File) {} | ||
|
||
onTaskUpdate(packs: TaskResultPack[]) { | ||
const startingTestFiles: File[] = [] | ||
const finishedTestFiles: File[] = [] | ||
|
||
const startingTests: (Test | Custom)[] = [] | ||
const finishedTests: (Test | Custom)[] = [] | ||
|
||
const startingHooks: HookOptions[] = [] | ||
const endingHooks: HookOptions[] = [] | ||
|
||
for (const pack of packs) { | ||
const task = this.ctx.state.idMap.get(pack[0]) | ||
|
||
if (task?.type === 'suite' && 'filepath' in task && task.result?.state) { | ||
if (task?.result?.state === 'run') { | ||
startingTestFiles.push(task) | ||
} | ||
else { | ||
// Skipped tests are not reported, do it manually | ||
for (const test of getTests(task)) { | ||
if (!test.result || test.result?.state === 'skip') { | ||
finishedTests.push(test) | ||
} | ||
} | ||
|
||
finishedTestFiles.push(task.file) | ||
} | ||
} | ||
|
||
if (task?.type === 'test' || task?.type === 'custom') { | ||
if (task.result?.state === 'run') { | ||
startingTests.push(task) | ||
} | ||
else if (task.result?.hooks?.afterEach !== 'run') { | ||
finishedTests.push(task) | ||
} | ||
} | ||
|
||
if (task?.result?.hooks) { | ||
for (const [hook, state] of Object.entries(task.result.hooks)) { | ||
if (state === 'run') { | ||
startingHooks.push({ name: hook, file: task.file, id: task.id, type: task.type }) | ||
} | ||
else { | ||
endingHooks.push({ name: hook, file: task.file, id: task.id, type: task.type }) | ||
} | ||
} | ||
} | ||
} | ||
|
||
endingHooks.forEach(hook => this.onHookEnd(hook)) | ||
finishedTests.forEach(test => this.onTestFinished(test)) | ||
finishedTestFiles.forEach(file => this.onTestFileFinished(file)) | ||
|
||
startingTestFiles.forEach(file => this.onTestFilePrepare(file)) | ||
startingTests.forEach(test => this.onTestStart(test)) | ||
startingHooks.forEach(hook => this.onHookStart(hook), | ||
) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
40 changes: 40 additions & 0 deletions
40
test/reporters/fixtures/task-parser-tests/example-1.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { beforeAll, beforeEach, afterEach, afterAll, test, describe } from "vitest"; | ||
import { setTimeout } from "node:timers/promises"; | ||
|
||
beforeAll(async () => { | ||
await setTimeout(100); | ||
}); | ||
|
||
afterAll(async () => { | ||
await setTimeout(100); | ||
}); | ||
|
||
describe("some suite", async () => { | ||
beforeEach(async () => { | ||
await setTimeout(100); | ||
}); | ||
|
||
test("some test", async () => { | ||
await setTimeout(100); | ||
}); | ||
|
||
afterEach(async () => { | ||
await setTimeout(100); | ||
}); | ||
}); | ||
|
||
test("Fast test 1", () => { | ||
// | ||
}); | ||
|
||
test.skip("Skipped test 1", () => { | ||
// | ||
}); | ||
|
||
test.concurrent("parallel slow tests 1.1", async () => { | ||
await setTimeout(100); | ||
}); | ||
|
||
test.concurrent("parallel slow tests 1.2", async () => { | ||
await setTimeout(100); | ||
}); |
Oops, something went wrong.