Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add perfStats to json reporter #933

Merged
merged 6 commits into from Mar 13, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/vitest/src/node/reporters/json.ts
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
4 changes: 4 additions & 0 deletions packages/vitest/src/runtime/run.ts
Expand Up @@ -67,6 +67,7 @@ export async function runTest(test: Test) {
return
}

const startDate = Date.now()
const start = performance.now()

test.result = {
Expand Down Expand Up @@ -127,6 +128,7 @@ export async function runTest(test: Test) {

getSnapshotClient().clearTest()

test.result.startTime = startDate
zxch3n marked this conversation as resolved.
Show resolved Hide resolved
test.result.duration = performance.now() - start

__vitest_worker__.current = undefined
Expand All @@ -150,6 +152,7 @@ export async function runSuite(suite: Suite) {
return
}

const startDate = Date.now()
const start = performance.now()

suite.result = {
Expand Down Expand Up @@ -185,6 +188,7 @@ export async function runSuite(suite: Suite) {
}
}
suite.result.duration = performance.now() - start
suite.result.startTime = startDate

if (suite.mode === 'run') {
if (!hasTests(suite)) {
Expand Down
1 change: 1 addition & 0 deletions packages/vitest/src/types/tasks.ts
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
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/reporters.spec.ts
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()
})