Skip to content

Commit

Permalink
feat: add perfStats to json reporter (#933)
Browse files Browse the repository at this point in the history
  • Loading branch information
zxch3n authored Mar 13, 2022
1 parent 2b9b1ae commit 89c8421
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 50 deletions.
12 changes: 12 additions & 0 deletions packages/vitest/src/node/reporters/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@ import { getSuites, getTests } from '../../utils'

// for compatibility reasons, the reporter produces a JSON similar to the one produced by the Jest JSON reporter
// the following types are extracted from the Jest repository (and simplified)
type Milliseconds = number
interface TestResult {
displayName?: string
failureMessage?: string | null
skipped: boolean
status?: string
testFilePath?: string
perfStats: {
end?: Milliseconds
runtime?: Milliseconds
start?: Milliseconds
}
}

interface AggregatedResult {
numFailedTests: number
numFailedTestSuites: number
Expand Down Expand Up @@ -54,6 +61,11 @@ export class JsonReporter implements Reporter {
const success = numFailedTestSuites === 0 && numFailedTests === 0

const testResults: Array<TestResult> = tests.map(t => ({
perfStats: {
runtime: t.result?.duration,
start: t.result?.startTime,
end: t.result?.duration && t.result?.startTime && t.result.duration + t.result.startTime,
},
displayName: t.name,
failureMessage: t.result?.error?.message,
skipped: t.mode === 'skip',
Expand Down
2 changes: 2 additions & 0 deletions packages/vitest/src/runtime/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export async function runTest(test: Test) {

test.result = {
state: 'run',
startTime: Date.now(),
}
updateTask(test)

Expand Down Expand Up @@ -154,6 +155,7 @@ export async function runSuite(suite: Suite) {

suite.result = {
state: 'run',
startTime: Date.now(),
}

updateTask(suite)
Expand Down
1 change: 1 addition & 0 deletions packages/vitest/src/types/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface TaskBase {
export interface TaskResult {
state: TaskState
duration?: number
startTime?: number
error?: ErrorWithDiff
hooks?: Partial<Record<keyof SuiteHooks, TaskState>>
}
Expand Down
117 changes: 69 additions & 48 deletions test/reporters/tests/__snapshots__/reporters.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -31,70 +31,91 @@ AssertionError: expected 2.23606797749979 to equal 2
`;
exports[`json reporter 1`] = `
"{
\\"numTotalTestSuites\\": 3,
\\"numPassedTestSuites\\": 3,
\\"numFailedTestSuites\\": 0,
\\"numPendingTestSuites\\": 0,
\\"numTotalTests\\": 8,
\\"numPassedTests\\": 7,
\\"numFailedTests\\": 1,
\\"numPendingTests\\": 0,
\\"numTodoTests\\": 0,
\\"startTime\\": 1642587001759,
\\"success\\": false,
\\"testResults\\": [
{
"numFailedTestSuites": 0,
"numFailedTests": 1,
"numPassedTestSuites": 3,
"numPassedTests": 7,
"numPendingTestSuites": 0,
"numPendingTests": 0,
"numTodoTests": 0,
"numTotalTestSuites": 3,
"numTotalTests": 8,
"startTime": 1642587001759,
"success": false,
"testResults": [
{
\\"displayName\\": \\"Math.sqrt()\\",
\\"failureMessage\\": \\"expected 2.23606797749979 to equal 2\\",
\\"skipped\\": false,
\\"status\\": \\"fail\\",
\\"testFilePath\\": \\"/vitest/test/core/test/basic.test.ts\\"
"displayName": "Math.sqrt()",
"failureMessage": "expected 2.23606797749979 to equal 2",
"perfStats": {
"runtime": 1.4422860145568848,
},
"skipped": false,
"status": "fail",
"testFilePath": "/vitest/test/core/test/basic.test.ts",
},
{
\\"displayName\\": \\"JSON\\",
\\"skipped\\": false,
\\"status\\": \\"pass\\",
\\"testFilePath\\": \\"/vitest/test/core/test/basic.test.ts\\"
"displayName": "JSON",
"perfStats": {
"runtime": 1.0237109661102295,
},
"skipped": false,
"status": "pass",
"testFilePath": "/vitest/test/core/test/basic.test.ts",
},
{
\\"displayName\\": \\"async with timeout\\",
\\"skipped\\": true,
\\"testFilePath\\": \\"/vitest/test/core/test/basic.test.ts\\"
"displayName": "async with timeout",
"perfStats": {},
"skipped": true,
"testFilePath": "/vitest/test/core/test/basic.test.ts",
},
{
\\"displayName\\": \\"timeout\\",
\\"skipped\\": false,
\\"status\\": \\"pass\\",
\\"testFilePath\\": \\"/vitest/test/core/test/basic.test.ts\\"
"displayName": "timeout",
"perfStats": {
"runtime": 100.50598406791687,
},
"skipped": false,
"status": "pass",
"testFilePath": "/vitest/test/core/test/basic.test.ts",
},
{
\\"displayName\\": \\"callback setup success \\",
\\"skipped\\": false,
\\"status\\": \\"pass\\",
\\"testFilePath\\": \\"/vitest/test/core/test/basic.test.ts\\"
"displayName": "callback setup success ",
"perfStats": {
"runtime": 20.184875011444092,
},
"skipped": false,
"status": "pass",
"testFilePath": "/vitest/test/core/test/basic.test.ts",
},
{
\\"displayName\\": \\"callback test success \\",
\\"skipped\\": false,
\\"status\\": \\"pass\\",
\\"testFilePath\\": \\"/vitest/test/core/test/basic.test.ts\\"
"displayName": "callback test success ",
"perfStats": {
"runtime": 0.33245420455932617,
},
"skipped": false,
"status": "pass",
"testFilePath": "/vitest/test/core/test/basic.test.ts",
},
{
\\"displayName\\": \\"callback setup success done(false)\\",
\\"skipped\\": false,
\\"status\\": \\"pass\\",
\\"testFilePath\\": \\"/vitest/test/core/test/basic.test.ts\\"
"displayName": "callback setup success done(false)",
"perfStats": {
"runtime": 19.738605976104736,
},
"skipped": false,
"status": "pass",
"testFilePath": "/vitest/test/core/test/basic.test.ts",
},
{
\\"displayName\\": \\"callback test success done(false)\\",
\\"skipped\\": false,
\\"status\\": \\"pass\\",
\\"testFilePath\\": \\"/vitest/test/core/test/basic.test.ts\\"
}
]
"displayName": "callback test success done(false)",
"perfStats": {
"runtime": 0.1923508644104004,
},
"skipped": false,
"status": "pass",
"testFilePath": "/vitest/test/core/test/basic.test.ts",
},
],
}
"
`;
exports[`tap reporter 1`] = `
Expand Down
2 changes: 1 addition & 1 deletion test/reporters/tests/custom-reporter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ test('custom reporters work', async() => {
})

expect(stdout).toContain('hello from custom reporter')
}, 20000)
}, 40000)
2 changes: 1 addition & 1 deletion test/reporters/tests/reporters.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,5 @@ test('json reporter', async() => {
await reporter.onFinished(files)

// Assert
expect(context.output).toMatchSnapshot()
expect(JSON.parse(context.output)).toMatchSnapshot()
})

0 comments on commit 89c8421

Please sign in to comment.