From b6c68d3ed849fc9ed80e000c8be196c2f2717c34 Mon Sep 17 00:00:00 2001 From: Mike Hedman Date: Thu, 24 Aug 2017 05:19:26 -0700 Subject: [PATCH] Adding ancestorTitles property to JSON test output (#4293) --- .../coverage_report.test.js.snap | 2 +- .../__tests__/json_reporter.test.js | 46 +++++++++++++++++-- .../json_reporter/__tests__/sum.test.js | 10 +++- packages/jest-util/src/format_test_results.js | 1 + 4 files changed, 51 insertions(+), 8 deletions(-) diff --git a/integration_tests/__tests__/__snapshots__/coverage_report.test.js.snap b/integration_tests/__tests__/__snapshots__/coverage_report.test.js.snap index 8d542645ef81..bdb23d499aa9 100644 --- a/integration_tests/__tests__/__snapshots__/coverage_report.test.js.snap +++ b/integration_tests/__tests__/__snapshots__/coverage_report.test.js.snap @@ -22,7 +22,7 @@ All files | 85.71 | 100 | 50 | 85.71 | | exports[`json reporter printing with --coverage 1`] = ` "Test Suites: 1 failed, 1 total -Tests: 1 failed, 1 passed, 2 total +Tests: 1 failed, 2 passed, 3 total Snapshots: 0 total Time: <> Ran all test suites. diff --git a/integration_tests/__tests__/json_reporter.test.js b/integration_tests/__tests__/json_reporter.test.js index 0ceea808d703..fc921cb80cbd 100644 --- a/integration_tests/__tests__/json_reporter.test.js +++ b/integration_tests/__tests__/json_reporter.test.js @@ -39,12 +39,30 @@ describe('JSON Reporter', () => { ); } - expect(jsonResult.numTotalTests).toBe(2); + expect(jsonResult.numTotalTests).toBe(3); expect(jsonResult.numTotalTestSuites).toBe(1); expect(jsonResult.numRuntimeErrorTestSuites).toBe(0); - expect(jsonResult.numPassedTests).toBe(1); + expect(jsonResult.numPassedTests).toBe(2); expect(jsonResult.numFailedTests).toBe(1); expect(jsonResult.numPendingTests).toBe(0); + + const noAncestors = jsonResult.testResults[0].assertionResults.find( + item => item.title == 'no ancestors', + ); + let expected = {ancestorTitles: []}; + expect(noAncestors).toEqual(expect.objectContaining(expected)); + + const addsNumbers = jsonResult.testResults[0].assertionResults.find( + item => item.title == 'adds numbers', + ); + expected = {ancestorTitles: ['sum']}; + expect(addsNumbers).toEqual(expect.objectContaining(expected)); + + const failsTheTest = jsonResult.testResults[0].assertionResults.find( + item => item.title == 'fails the test', + ); + expected = {ancestorTitles: ['sum', 'failing tests']}; + expect(failsTheTest).toEqual(expect.objectContaining(expected)); }); it('outputs coverage report', () => { @@ -53,7 +71,7 @@ describe('JSON Reporter', () => { const stderr = result.stderr.toString(); let jsonResult; - expect(stderr).toMatch(/1 failed, 1 passed/); + expect(stderr).toMatch(/1 failed, 2 passed/); expect(result.status).toBe(1); try { @@ -64,11 +82,29 @@ describe('JSON Reporter', () => { ); } - expect(jsonResult.numTotalTests).toBe(2); + expect(jsonResult.numTotalTests).toBe(3); expect(jsonResult.numTotalTestSuites).toBe(1); expect(jsonResult.numRuntimeErrorTestSuites).toBe(0); - expect(jsonResult.numPassedTests).toBe(1); + expect(jsonResult.numPassedTests).toBe(2); expect(jsonResult.numFailedTests).toBe(1); expect(jsonResult.numPendingTests).toBe(0); + + const noAncestors = jsonResult.testResults[0].assertionResults.find( + item => item.title == 'no ancestors', + ); + let expected = {ancestorTitles: []}; + expect(noAncestors).toEqual(expect.objectContaining(expected)); + + const addsNumbers = jsonResult.testResults[0].assertionResults.find( + item => item.title == 'adds numbers', + ); + expected = {ancestorTitles: ['sum']}; + expect(addsNumbers).toEqual(expect.objectContaining(expected)); + + const failsTheTest = jsonResult.testResults[0].assertionResults.find( + item => item.title == 'fails the test', + ); + expected = {ancestorTitles: ['sum', 'failing tests']}; + expect(failsTheTest).toEqual(expect.objectContaining(expected)); }); }); diff --git a/integration_tests/json_reporter/__tests__/sum.test.js b/integration_tests/json_reporter/__tests__/sum.test.js index bdd9fd02556c..17e8b69511ce 100644 --- a/integration_tests/json_reporter/__tests__/sum.test.js +++ b/integration_tests/json_reporter/__tests__/sum.test.js @@ -9,12 +9,18 @@ const sum = require('../sum'); +it('no ancestors', () => { + expect(true).toBeTruthy(); +}); + describe('sum', () => { it('adds numbers', () => { expect(sum(1, 2)).toEqual(3); }); - it('fails the test', () => { - expect(sum(1, 2)).toEqual(4); + describe('failing tests', () => { + it('fails the test', () => { + expect(sum(1, 2)).toEqual(4); + }); }); }); diff --git a/packages/jest-util/src/format_test_results.js b/packages/jest-util/src/format_test_results.js index 4ce2ddfad20f..8c844fe4bb2f 100644 --- a/packages/jest-util/src/format_test_results.js +++ b/packages/jest-util/src/format_test_results.js @@ -60,6 +60,7 @@ function formatTestAssertion( assertion: AssertionResult, ): FormattedAssertionResult { const result: FormattedAssertionResult = { + ancestorTitles: assertion.ancestorTitles, failureMessages: null, fullName: assertion.fullName, status: assertion.status,