From 1f87ab1e17e5f85c1eb5c71368ac925ac265d244 Mon Sep 17 00:00:00 2001 From: Sander Koenders Date: Wed, 3 Oct 2018 09:48:19 +0200 Subject: [PATCH] fix(JestTestRunner): fix stryker error on skipping tests (#1168) Implement `TestStatus.Skipped` for pending jest results so Stryker does not fail on the first testrun when tests are marked as `skip` Fixes #1152 --- .../stryker-jest-runner/src/JestTestRunner.ts | 13 +- .../test/helpers/testResultProducer.ts | 278 ++++++++++-------- .../test/unit/JestTestRunnerSpec.ts | 19 ++ 3 files changed, 188 insertions(+), 122 deletions(-) diff --git a/packages/stryker-jest-runner/src/JestTestRunner.ts b/packages/stryker-jest-runner/src/JestTestRunner.ts index fbf51a83b1..2949bb77f7 100644 --- a/packages/stryker-jest-runner/src/JestTestRunner.ts +++ b/packages/stryker-jest-runner/src/JestTestRunner.ts @@ -55,7 +55,7 @@ export default class JestTestRunner implements TestRunner { testResults.push({ failureMessages: testResult.failureMessages, name: testResult.fullName, - status: (testResult.status === 'passed') ? TestStatus.Success : TestStatus.Failed, + status: this.determineTestResultStatus(testResult.status), timeSpentMs: testResult.duration ? testResult.duration : 0 }); } @@ -63,4 +63,15 @@ export default class JestTestRunner implements TestRunner { return testResults; } + + private determineTestResultStatus(status: string) { + switch (status) { + case 'passed': + return TestStatus.Success; + case 'pending': + return TestStatus.Skipped; + default: + return TestStatus.Failed; + } + } } diff --git a/packages/stryker-jest-runner/test/helpers/testResultProducer.ts b/packages/stryker-jest-runner/test/helpers/testResultProducer.ts index 51e800f6f7..ccfec1a9bd 100644 --- a/packages/stryker-jest-runner/test/helpers/testResultProducer.ts +++ b/packages/stryker-jest-runner/test/helpers/testResultProducer.ts @@ -1,122 +1,158 @@ -export function createFailResult() { - return { - numFailedTests: 2, - numFailedTestSuites: 1, - numPassedTests: 0, - numPassedTestSuites: 0, - numPendingTests: 0, - numPendingTestSuites: 0, - numRuntimeErrorTestSuites: 0, - numTotalTests: 2, - numTotalTestSuites: 1, - startTime: 1513852010583, - success: false, - testResults: [ - { - console: null, - coverage: undefined, - failureMessage: 'test failed - App.test.js', - numFailingTests: 2, - numPassingTests: 0, - numPendingTests: 0, - skipped: false, - sourceMaps: {}, - testFilePath: 'App.test.js', - testResults: [ - { - ancestorTitles: ['App'], - duration: 2, - failureMessages: [ - 'Fail message 1', - 'Fail message 2' - ], - fullName: 'App render renders without crashing', - numPassingAsserts: 0, - status: 'failed', - title: 'renders without crashing' - }, - { - ancestorTitles: ['App'], - duration: 0, - failureMessages: [ - 'Fail message 3', - 'Fail message 4' - ], - fullName: 'App render renders without crashing', - numPassingAsserts: 0, - status: 'failed', - title: 'renders without crashing' - } - ] - }, - { - console: null, - coverage: undefined, - failureMessage: null, - numFailingTests: 0, - numPassingTests: 1, - numPendingTests: 0, - perfStats: [Object], - skipped: false, - snapshot: [Object], - sourceMaps: {}, - testFilePath: 'App.test.js', - testResults: [ - { - ancestorTitles: ['App'], - duration: 23, - failureMessages: [], - fullName: 'App renders without crashing', - numPassingAsserts: 0, - status: 'passed', - title: 'renders without crashing' - } - ] - } - ], - wasInterrupted: false - }; -} +export const createFailResult = () => ({ + numFailedTests: 2, + numFailedTestSuites: 1, + numPassedTests: 0, + numPassedTestSuites: 0, + numPendingTests: 0, + numPendingTestSuites: 0, + numRuntimeErrorTestSuites: 0, + numTotalTests: 2, + numTotalTestSuites: 1, + startTime: 1513852010583, + success: false, + testResults: [ + { + console: null, + coverage: undefined, + failureMessage: 'test failed - App.test.js', + numFailingTests: 2, + numPassingTests: 0, + numPendingTests: 0, + skipped: false, + sourceMaps: {}, + testFilePath: 'App.test.js', + testResults: [ + { + ancestorTitles: ['App'], + duration: 2, + failureMessages: [ + 'Fail message 1', + 'Fail message 2' + ], + fullName: 'App render renders without crashing', + numPassingAsserts: 0, + status: 'failed', + title: 'renders without crashing' + }, + { + ancestorTitles: ['App'], + duration: 0, + failureMessages: [ + 'Fail message 3', + 'Fail message 4' + ], + fullName: 'App render renders without crashing', + numPassingAsserts: 0, + status: 'failed', + title: 'renders without crashing' + } + ] + }, + { + console: null, + coverage: undefined, + failureMessage: null, + numFailingTests: 0, + numPassingTests: 1, + numPendingTests: 0, + perfStats: [Object], + skipped: false, + snapshot: [Object], + sourceMaps: {}, + testFilePath: 'App.test.js', + testResults: [ + { + ancestorTitles: ['App'], + duration: 23, + failureMessages: [], + fullName: 'App renders without crashing', + numPassingAsserts: 0, + status: 'passed', + title: 'renders without crashing' + } + ] + } + ], + wasInterrupted: false +}); -export function createSuccessResult() { - return { - numFailedTests: 0, - numFailedTestSuites: 0, - numPassedTests: 1, - numPassedTestSuites: 1, - numPendingTests: 0, - numPendingTestSuites: 0, - numRuntimeErrorTestSuites: 0, - numTotalTests: 1, - numTotalTestSuites: 1, - startTime: 1513857548132, - success: true, - testResults: [ - { - console: null, - coverage: undefined, - failureMessage: null, - numFailingTests: 0, - numPassingTests: 1, - numPendingTests: 0, - perfStats: [Object], - skipped: false, - snapshot: [Object], - sourceMaps: {}, - testFilePath: 'App.test.js', - testResults: [ - { - ancestorTitles: ['App'], - duration: 23, - failureMessages: [], - fullName: 'App renders without crashing', - numPassingAsserts: 0, - status: 'passed', - title: 'renders without crashing' - } - ] - } - ], - wasInterrupted: false - }; -} +export const createSuccessResult = () => ({ + numFailedTests: 0, + numFailedTestSuites: 0, + numPassedTests: 1, + numPassedTestSuites: 1, + numPendingTests: 0, + numPendingTestSuites: 0, + numRuntimeErrorTestSuites: 0, + numTotalTests: 1, + numTotalTestSuites: 1, + startTime: 1513857548132, + success: true, + testResults: [ + { + console: null, + coverage: undefined, + failureMessage: null, + numFailingTests: 0, + numPassingTests: 1, + numPendingTests: 0, + perfStats: [Object], + skipped: false, + snapshot: [Object], + sourceMaps: {}, + testFilePath: 'App.test.js', + testResults: [ + { + ancestorTitles: ['App'], + duration: 23, + failureMessages: [], + fullName: 'App renders without crashing', + numPassingAsserts: 0, + status: 'passed', + title: 'renders without crashing' + } + ] + } + ], + wasInterrupted: false +}); + +export const createPendingResult = () => ({ + numFailedTests: 0, + numFailedTestSuites: 0, + numPassedTests: 1, + numPassedTestSuites: 1, + numPendingTests: 0, + numPendingTestSuites: 0, + numRuntimeErrorTestSuites: 0, + numTotalTests: 1, + numTotalTestSuites: 1, + startTime: 1513857548132, + success: true, + testResults: [ + { + console: null, + coverage: undefined, + failureMessage: null, + numFailingTests: 0, + numPassingTests: 1, + numPendingTests: 0, + perfStats: [Object], + skipped: false, + snapshot: [Object], + sourceMaps: {}, + testResults: [ + { + ancestorTitles: ['App'], + duration: 0, + failureMessages: [], + fullName: 'App renders without crashing', + numPassingAsserts: 0, + status: 'pending', + title: 'renders without crashing' + } + ] + } + ], + wasInterrupted: false +}); diff --git a/packages/stryker-jest-runner/test/unit/JestTestRunnerSpec.ts b/packages/stryker-jest-runner/test/unit/JestTestRunnerSpec.ts index a93602dbbc..2b095f6cd8 100644 --- a/packages/stryker-jest-runner/test/unit/JestTestRunnerSpec.ts +++ b/packages/stryker-jest-runner/test/unit/JestTestRunnerSpec.ts @@ -74,6 +74,25 @@ describe('JestTestRunner', () => { }); }); + it('should call the jestTestRunner run method and return a skipped runResult', async () => { + runJestStub.resolves({ results: fakeResults.createPendingResult() }); + + const result = await jestTestRunner.run(); + + expect(result).to.deep.equal({ + errorMessages: [], + status: RunStatus.Complete, + tests: [ + { + failureMessages: [], + name: 'App renders without crashing', + status: TestStatus.Skipped, + timeSpentMs: 0 + } + ] + }); + }); + it('should call the jestTestRunner run method and return a negative runResult', async () => { runJestStub.resolves({ results: fakeResults.createFailResult() });