-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
executable file
·174 lines (153 loc) · 5.86 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env node
const fs = require("fs");
const { program } = require("commander");
const mustache = require("mustache");
const pathPkg = require("path");
const winston = require("winston");
program
.option("-p, --path <path>", "Specify the path to the report")
.option("-o, --output <output>", "Specify the path for the markdown file", "./md-reports/output.md")
.option("-t, --template <template>", "Specify the path to the template file", "./sample-template.md")
.option("-T, --title <title>", "Specify the title for the report", "Test Report")
.option("-v, --verbose", "Enable verbose mode for debug logging")
.usage("$0 -p file/path.json [options]")
.addHelpText(
"after",
"\nFor more information, visit https://github.com/403-html/mochawesome-json-to-md"
)
.parse(process.argv);
const createLogger = (verbose) => {
const level = verbose ? 'debug' : 'info';
return winston.createLogger({
level,
format: winston.format.combine(
winston.format.timestamp(),
winston.format.colorize(),
winston.format.printf(({ timestamp, level, message }) => {
return `${timestamp} | ${level} | ${message}`;
}),
),
transports: [
new winston.transports.Console({ level, handleExceptions: true }),
],
});
};
const logger = createLogger(program.opts().verbose);
/**
* Reads the JSON file and returns its content as an object.
* @param {string} filePath - Path to the JSON file.
* @returns {object} - Parsed JSON object.
* @throws Will throw an error if there's an issue parsing the JSON file.
*/
const readJsonFile = (filePath) => {
logger.debug(`Reading JSON file: ${filePath}`);
if (typeof filePath !== "string") {
logger.error(`Invalid file path provided: ${filePath}`);
throw new Error(`Invalid file path provided: ${filePath}`);
}
let jsonObj;
try {
logger.debug(`Parsing JSON file: ${filePath}`);
jsonObj = JSON.parse(fs.readFileSync(filePath));
logger.debug(`Successfully parsed JSON file: ${filePath}`);
} catch (err) {
logger.error(`Error while parsing JSON file: ${err}`);
throw new Error(`Error while parsing JSON file: ${err}`);
}
return jsonObj;
};
/**
* Extracts all necessary information from the parsed JSON object.
* @param {object} param0 - Object containing the 'results' and 'stats' properties.
* @param {Array} param0.results - List of results.
* @param {object} param0.stats - Statistics object containing information about the test run (e.g. start date, duration, number of tests, etc.).
* @returns {object} - Extracted information.
*/
const extractTestResultsInfo = ({ results, stats }) => {
logger.debug('Extracting test results information');
const { start: startDate, duration, tests: totalTests, other: otherTests } = stats;
const testTypes = ["passes", "failures", "pending", "skipped"];
const categorizedTests = testTypes.map((type) =>
results.flatMap((result) =>
collectTestsByType({
type,
suite: result,
path: result.file,
})
)
);
logger.debug('Finished extracting test results information');
return {
startDate,
duration,
passedTestsCount: categorizedTests[0].length,
failedTestsCount: categorizedTests[1].length,
skippedTestsCount: categorizedTests[2].length,
skippedCypressTestsCount: categorizedTests[3].length,
otherTestsCount: otherTests,
totalTests,
passedExists: categorizedTests[0].length > 0,
failedExists: categorizedTests[1].length > 0,
skippedExists: categorizedTests[2].length > 0,
skippedCypressExists: categorizedTests[3].length > 0,
passedTests: categorizedTests[0],
failedTests: categorizedTests[1],
skippedTests: categorizedTests[2],
skippedCypress: categorizedTests[3],
};
};
/**
* Recursively collects all tests with a given type.
* @param {object} param0 - Object containing 'type', 'suite', 'path', and 'cache' properties.
* @param {string} param0.type - Type of the test.
* @param {object} param0.suite - Suite object.
* @param {string} param0.path - Path to the test file.
* @param {Array} param0.cache - Cache array.
* @returns {Array} - List of tests with the given type.
*/
const collectTestsByType = ({ type, suite, path, cache = [] }) => {
logger.debug(`Collecting tests of type ${type}`);
const localCache = cache;
const { [type]: typeList, suites, tests } = suite;
if (typeList.length > 0) {
for (const uuid of typeList) {
const foundTestByUuid = tests.find((test) => test.uuid === uuid);
if (!foundTestByUuid) {
logger.error(`Test with uuid ${uuid} not found`);
throw new Error(`Test with uuid ${uuid} not found`);
}
logger.debug(`Found test with uuid ${uuid}`);
foundTestByUuid.path = path;
localCache.push({ path, ...foundTestByUuid });
}
}
if (suites.length > 0) {
for (const subSuite of suites) {
collectTestsByType({
type,
suite: subSuite,
path: subSuite.file || path,
cache: localCache,
});
}
}
return localCache;
};
const convertMochaToMarkdown = () => {
logger.info('Starting Mocha to Markdown conversion');
const { path, output, template, title } = program.opts();
logger.info(`Reading test results from: ${path}`);
const testResults = readJsonFile(path);
logger.info('Extracting test results information');
const extractedInfo = extractTestResultsInfo(testResults);
logger.info(`Reading template file: ${template}`);
const templateContent = fs.readFileSync(template, "utf-8");
logger.info('Rendering template with test results');
const renderedMarkdown = mustache.render(templateContent, {...extractedInfo, title});
const outputPath = pathPkg.dirname(output);
logger.info(`Creating directory structure: ${outputPath}`);
fs.mkdirSync(outputPath, { recursive: true });
logger.info(`Writing markdown to: ${output}`);
fs.writeFileSync(output, renderedMarkdown);
};
convertMochaToMarkdown();