Skip to content

Commit

Permalink
refactoring: reduce amount of coverage info collected
Browse files Browse the repository at this point in the history
  • Loading branch information
Demivan committed Jan 28, 2022
1 parent 1b5a5ff commit 25c7e18
Showing 1 changed file with 29 additions and 27 deletions.
56 changes: 29 additions & 27 deletions packages/vitest/src/runtime/run.ts
Expand Up @@ -44,7 +44,7 @@ async function sendTasksUpdate() {
}
}

export async function runTest(test: Test) {
export async function runTest(test: Test, config: ResolvedConfig) {
if (test.mode !== 'run')
return

Expand Down Expand Up @@ -72,7 +72,26 @@ export async function runTest(test: Test) {
testPath: test.suite.file?.filepath,
currentTestName: getFullName(test),
})

let session!: inspector.Session
if (config.coverage.enabled) {
session = new inspector.Session()
session.connect()

session.post('Profiler.enable')
session.post('Profiler.startPreciseCoverage', { callCount: true, detailed: true })
}

await getFn(test)()

if (config.coverage.enabled) {
session.post('Profiler.takePreciseCoverage', (_, coverage) => {
rpc().coverageCollected(coverage)
})

session.disconnect()
}

const { assertionCalls, expectedAssertionsNumber, expectedAssertionsNumberError, isExpectingAssertions, isExpectingAssertionsError } = getState()
if (expectedAssertionsNumber !== null && assertionCalls !== expectedAssertionsNumber)
throw expectedAssertionsNumberError
Expand Down Expand Up @@ -115,7 +134,7 @@ export async function runTest(test: Test) {
updateTask(test)
}

export async function runSuite(suite: Suite) {
export async function runSuite(suite: Suite, config: ResolvedConfig) {
if (suite.result?.state === 'fail')
return

Expand All @@ -139,11 +158,11 @@ export async function runSuite(suite: Suite) {

for (const tasksGroup of partitionSuiteChildren(suite)) {
if (tasksGroup[0].concurrent === true) {
await Promise.all(tasksGroup.map(c => runSuiteChild(c)))
await Promise.all(tasksGroup.map(c => runSuiteChild(c, config)))
}
else {
for (const c of tasksGroup)
await runSuiteChild(c)
await runSuiteChild(c, config)
}
}

Expand Down Expand Up @@ -173,40 +192,23 @@ export async function runSuite(suite: Suite) {
updateTask(suite)
}

async function runSuiteChild(c: Task) {
async function runSuiteChild(c: Task, config: ResolvedConfig) {
return c.type === 'test'
? runTest(c)
: runSuite(c)
? runTest(c, config)
: runSuite(c, config)
}

export async function runSuites(suites: Suite[]) {
export async function runSuites(suites: Suite[], config: ResolvedConfig) {
for (const suite of suites)
await runSuite(suite)
await runSuite(suite, config)
}

export async function startTests(paths: string[], config: ResolvedConfig) {
const files = await collectTests(paths, config)

rpc().onCollected(files)

let session!: inspector.Session
if (config.coverage.enabled) {
session = new inspector.Session()
session.connect()

session.post('Profiler.enable')
session.post('Profiler.startPreciseCoverage', { detailed: true })
}

await runSuites(files)

if (config.coverage.enabled) {
session.post('Profiler.takePreciseCoverage', (_, coverage) => {
rpc().coverageCollected(coverage)
})

session.disconnect()
}
await runSuites(files, config)

await getSnapshotClient().saveSnap()

Expand Down

0 comments on commit 25c7e18

Please sign in to comment.