|
| 1 | +/* |
| 2 | +* Parser for both Mocha Json report and Mochawesome json |
| 3 | +*/ |
| 4 | +const path = require('path'); |
| 5 | + |
| 6 | +const TestResult = require('../models/TestResult'); |
| 7 | +const TestSuite = require('../models/TestSuite'); |
| 8 | +const TestCase = require('../models/TestCase'); |
| 9 | + |
| 10 | +function getTestCase(rawCase) { |
| 11 | + const test_case = new TestCase(); |
| 12 | + test_case.name = rawCase["name"]; |
| 13 | + test_case.duration = rawCase["duration"]; |
| 14 | + if (rawCase.state && rawCase.state === "failed") { |
| 15 | + test_case.status = 'FAIL'; |
| 16 | + test_case.failure = rawCase.errorStack; |
| 17 | + } |
| 18 | + else { |
| 19 | + test_case.status = 'PASS'; |
| 20 | + } |
| 21 | + return test_case; |
| 22 | +} |
| 23 | + |
| 24 | +function getTestSuite(rawSuite) { |
| 25 | + const suite = new TestSuite(); |
| 26 | + suite.name = rawSuite["name"]; |
| 27 | + suite.total = rawSuite["tests"]; |
| 28 | + suite.passed = rawSuite["passes"]; |
| 29 | + suite.failed = rawSuite["failures"]; |
| 30 | + suite.duration = rawSuite["duration"]; |
| 31 | + suite.status = suite.total === suite.passed ? 'PASS' : 'FAIL'; |
| 32 | + const raw_test_cases = rawSuite.elements; |
| 33 | + if (raw_test_cases) { |
| 34 | + for (let i = 0; i < raw_test_cases.length; i++) { |
| 35 | + suite.cases.push(getTestCase(raw_test_cases[i])); |
| 36 | + } |
| 37 | + } |
| 38 | + return suite; |
| 39 | +} |
| 40 | + |
| 41 | +function getTestResult(json) { |
| 42 | + const result = new TestResult(); |
| 43 | + const { stats, suites } = preprocess(json); |
| 44 | + result.name = suites["name"] || ""; |
| 45 | + result.total = stats["tests"]; |
| 46 | + result.passed = stats["passes"]; |
| 47 | + result.failed = stats["failures"]; |
| 48 | + const errors = stats["errors"]; |
| 49 | + if (errors) { |
| 50 | + result.errors = errors; |
| 51 | + } |
| 52 | + result.duration = stats["duration"] || 0; |
| 53 | + |
| 54 | + if (suites.length > 0) { |
| 55 | + for (let i = 0; i < suites.length; i++) { |
| 56 | + result.suites.push(getTestSuite(suites[i])); |
| 57 | + } |
| 58 | + } |
| 59 | + result.status = result.total === result.passed ? 'PASS' : 'FAIL'; |
| 60 | + return result; |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Function to format the raw json report |
| 65 | + * @param {*} rawjson |
| 66 | + * @returns formatted json object |
| 67 | + */ |
| 68 | +function preprocess(rawjson) { |
| 69 | + const formattedResult = { stats: {}, suites: [] }; |
| 70 | + |
| 71 | + rawjson.forEach(testSuite => { |
| 72 | + testSuite.elements.forEach(testCase => { |
| 73 | + testCase.state = testCase.steps.every(step => step.result.status === "passed") ? "passed" : "failed"; |
| 74 | + testCase.duration = testCase.steps.map(step => step.result.duration).reduce((total, currVal) => total + currVal, 0) / 1000000; |
| 75 | + testCase.duration = parseFloat(testCase.duration.toFixed(2)); |
| 76 | + testCase.errorStack = testCase.steps.filter(step => step.result.status === "failed").map(step => step.result.error_message)[0] || ""; |
| 77 | + }) |
| 78 | + testSuite.tests = testSuite.elements.length; |
| 79 | + |
| 80 | + if (testSuite.tests) { |
| 81 | + testSuite.failures = testSuite.elements.filter(testCase => testCase.state === "failed").length; |
| 82 | + testSuite.passes = testSuite.elements.filter(testCase => testCase.state === "passed").length; |
| 83 | + testSuite.duration = testSuite.elements.map(testCase => testCase.duration).reduce((total, currVal) => total + currVal, 0); |
| 84 | + } |
| 85 | + formattedResult.suites.push(testSuite); |
| 86 | + }); |
| 87 | + |
| 88 | + formattedResult.stats.suites = formattedResult.suites.length; |
| 89 | + for (const statsType of ["tests", "passes", "failures", "errors", "duration"]) { |
| 90 | + formattedResult.stats[statsType] = formattedResult.suites.map(testSuite => testSuite[statsType]).reduce((total, currVal) => total + currVal, 0) || 0; |
| 91 | + } |
| 92 | + return formattedResult; |
| 93 | +} |
| 94 | + |
| 95 | +function parse(options) { |
| 96 | + const cwd = process.cwd(); |
| 97 | + const json = require(path.join(cwd, options.files[0])); |
| 98 | + return getTestResult(json); |
| 99 | +} |
| 100 | + |
| 101 | +module.exports = { |
| 102 | + parse |
| 103 | +} |
0 commit comments