Skip to content

Commit

Permalink
feat: report coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ph-fritsche committed Dec 6, 2022
1 parent 3f183ec commit f01079b
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 29 deletions.
8 changes: 3 additions & 5 deletions src/conductor/TestConductor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,9 @@ export abstract class TestConductor extends Entity {
run.state = 'running'
this.reporterServer.reportStart(run, this.setupFiles, testFiles)

await Promise.all(testFiles.map(f => this.runTestSuite(runId, f.url, f.id, f.name)
.then(
() => this.reporterServer.reportComplete(run, f.id),
async e => this.reporterServer.reportError(run, f.id, e),
)
await Promise.all(testFiles
.map(f => this.runTestSuite(runId, f.url, f.id, f.name)
.catch(e => this.reporterServer.reportError(run, f.id, e))
))

run.state = 'done'
Expand Down
6 changes: 6 additions & 0 deletions src/reporter/ReporterMessage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { TestGroup, TestResult, TestError } from '../test'
import type { CoverageMapData } from 'istanbul-lib-coverage'

export type ReporterMessageMap<
TGroup extends TestGroup,
Expand All @@ -14,6 +15,11 @@ export type ReporterMessageMap<
testId: string
result: TResult
}
complete: {
runId: string
groupId: string
coverage: CoverageMapData
}
error: {
runId: string
groupId: string
Expand Down
48 changes: 27 additions & 21 deletions src/reporter/ReporterServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export type ReporterEventMap = {
complete: {
run: TestRun
group: TestGroup
coverage: object
}
error: {
run: TestRun
Expand Down Expand Up @@ -102,6 +103,12 @@ export class ReporterServer {
group: run.groups.get(report.groupId),
error: report.error,
})
} else if ('coverage' in report) {
this.emitter.dispatch('complete', {
run,
group: run.groups.get(report.groupId),
coverage: report.coverage,
})
}
} catch (e) {
reject(e)
Expand Down Expand Up @@ -157,15 +164,6 @@ export class ReporterServer {
this.emitter.dispatch('error', {run, error, group})
}

async reportComplete(
run: TestRun,
groupId: string,
) {
const group = run.groups.get(groupId)

this.emitter.dispatch('complete', {run, group })
}

async reportDone(
run: TestRun
) {
Expand Down Expand Up @@ -221,20 +219,28 @@ function parseReport(t: string) {
ReporterMessageMap<TestGroup, TestResult, TestError>[keyof ReporterMessageMap<TestGroup, TestResult, TestError>]
}

type BaseEntities = {
TestGroup: BaseEntities.TestGroup
Test: BaseEntities.Test
TestResult: BaseEntities.TestResult
TestError: BaseEntities.TestError
}
function isRevivedType<K extends keyof BaseEntities>(
value: object,
type: K,
): value is BaseEntities[K] {
return '__T' in value && value['__T'] === type
}
function reviveReportProps(key: string, value: unknown) {
if (typeof value === 'object') {
if (key === 'result') {
return new TestResult(value as BaseEntities.TestResult)
} else if (key === 'error') {
return new TestError(value as BaseEntities.TestError)
} else if (key === 'group') {
return new TestGroup(value as BaseEntities.TestGroup)
} else if (String(Number(key)) === key) {
if ('children' in value) {
return new TestGroup(value as BaseEntities.TestGroup)
} else {
return new Test(value as BaseEntities.Test)
}
if (isRevivedType(value, 'Test')) {
return new Test(value)
} else if (isRevivedType(value, 'TestGroup')) {
return new TestGroup(value)
} else if (isRevivedType(value, 'TestError')) {
return new TestError(value)
} else if (isRevivedType(value, 'TestResult')) {
return new TestResult(value)
}
}
return value
Expand Down
6 changes: 6 additions & 0 deletions src/runner/TestRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ export class TestRunner {
result,
})
}

await this.report('complete', {
runId,
groupId: group.id,
coverage: globalThis.__coverage__,
})
}

protected async *execTestsIterator(
Expand Down
1 change: 1 addition & 0 deletions src/test/Test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class Test extends Entity {

toJSON() {
return {
__T: 'Test',
id: this.id,
title: this.title,
}
Expand Down
1 change: 1 addition & 0 deletions src/test/TestError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export class TestError extends Error {

toJSON() {
return {
__T: 'TestError',
name: this.name,
message: this.message,
stack: this.stack,
Expand Down
1 change: 1 addition & 0 deletions src/test/TestGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class TestGroup extends Entity {

toJSON() {
return {
__T: 'TestGroup',
id: this.id,
title: this.title,
children: this._children,
Expand Down
5 changes: 2 additions & 3 deletions src/test/TestResult.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Serializable } from "./types"

export class TestResult {
readonly duration?: number
readonly error?: Error
Expand All @@ -13,8 +11,9 @@ export class TestResult {
this.error = props.error
}

toJSON(): Serializable<TestResult> {
toJSON() {
return {
__T: 'TestResult',
status: this.status,
duration: this.duration,
error: this.error && {
Expand Down

0 comments on commit f01079b

Please sign in to comment.