|
| 1 | +/** |
| 2 | + * Flips the test results for fail.test.js > .fail > <test cases> |
| 3 | + */ |
| 4 | +class ExceptionlessExpectedFailureReporter { |
| 5 | + constructor(globalConfig, reporterOptions, reporterContext) { |
| 6 | + this._globalConfig = globalConfig; |
| 7 | + this._options = reporterOptions; |
| 8 | + this._context = reporterContext; |
| 9 | + } |
| 10 | + onTestCaseResult(test, testCaseResult) { |
| 11 | + this._processTestCaseResult(testCaseResult); |
| 12 | + } |
| 13 | + onTestFileResult(test, testResult, results) { |
| 14 | + if (testResult.testFilePath.endsWith('fail.test.js')) { |
| 15 | + this._processTestResults(results); |
| 16 | + } |
| 17 | + } |
| 18 | + _processTestResults(results) { |
| 19 | + for (let testSuiteResult of results.testResults) { |
| 20 | + if (testSuiteResult.testFilePath.endsWith('fail.test.js')) { |
| 21 | + let switchedToFailing = 0; |
| 22 | + let switchedToPassing = 0; |
| 23 | + for (let testCaseResult of testSuiteResult.testResults) { |
| 24 | + const processResult = this._processTestCaseResult(testCaseResult); |
| 25 | + if (processResult === 'switch-to-failing') switchedToFailing++; |
| 26 | + if (processResult === 'switch-to-passing') switchedToPassing++; |
| 27 | + } |
| 28 | + const originalFailureCount = testSuiteResult.numFailingTests; |
| 29 | + testSuiteResult.numFailingTests += switchedToFailing - switchedToPassing; |
| 30 | + results.numFailedTests += switchedToFailing - switchedToPassing; |
| 31 | + testSuiteResult.numPassingTests += switchedToPassing - switchedToFailing; |
| 32 | + results.numPassedTests += switchedToPassing - switchedToFailing; |
| 33 | + if (originalFailureCount === switchedToPassing) { |
| 34 | + testSuiteResult.failureMessage = ''; |
| 35 | + results.numFailedTestSuites -= 1; |
| 36 | + results.numPassedTestSuites += 1; |
| 37 | + if (results.numFailedTestSuites === 0) results.success = true; |
| 38 | + console.log('marking failing test suite as passing', testSuiteResult.testFilePath); |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + _processTestCaseResult(testCaseResult) { |
| 45 | + if (this._hasDotFailAncestor(testCaseResult)) { |
| 46 | + if (testCaseResult.status === 'failed') { |
| 47 | + this._markPassing(testCaseResult); |
| 48 | + return 'switch-to-passing'; |
| 49 | + } else if (testCaseResult.status === 'passed') { |
| 50 | + this._markFailing(testCaseResult); |
| 51 | + return 'switch-to-failing'; |
| 52 | + } |
| 53 | + } |
| 54 | + return 'unchanged'; |
| 55 | + } |
| 56 | + _hasDotFailAncestor(result) { |
| 57 | + return result.ancestorTitles.length > 0 && result.ancestorTitles[0] === '.fail'; |
| 58 | + } |
| 59 | + _markPassing(result) { |
| 60 | + result.status = 'passed'; |
| 61 | + result.failureDetails = []; |
| 62 | + result.failureMessages = []; |
| 63 | + result.numPassingAsserts = 1; |
| 64 | + } |
| 65 | + _markFailing(result) { |
| 66 | + const message = `${result.fullName} was expected to fail, but did not.`; |
| 67 | + result.status = 'failed'; |
| 68 | + result.failureDetails = [ |
| 69 | + { |
| 70 | + matcherResult: { |
| 71 | + pass: false, |
| 72 | + message: message, |
| 73 | + }, |
| 74 | + message: message, |
| 75 | + stack: `${message}\n\tNo stack trace.\n\tThis is a placeholder message generated inside ExceptionlessExpectedFailureReporter`, |
| 76 | + }, |
| 77 | + ]; |
| 78 | + result.failureMessages = [message]; |
| 79 | + result.numPassingAsserts = 0; |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +module.exports = ExceptionlessExpectedFailureReporter; |
0 commit comments