-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
61 lines (55 loc) · 2.07 KB
/
index.ts
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
import * as path from 'path';
import * as fs from 'fs';
import { AggregatedResult, TestResult } from '@jest/test-result';
import { Circus, Config } from '@jest/types';
import { Test, Context, ReporterOnStartOptions } from '@jest/reporters';
function mkdirs(dirpath: string) {
if (!fs.existsSync(path.dirname(dirpath))) {
mkdirs(path.dirname(dirpath));
}
fs.mkdirSync(dirpath);
}
type HtmlReporter4JestOptions = {
title?: string;
reportPath?: string;
reportFileName?: string;
};
declare module '@jest/test-result' {
interface AggregatedResult {
endTime: number;
reporterOptions: HtmlReporter4JestOptions & Config.DefaultOptions;
}
}
class HTMLReport4Jest {
private _globalConfig: Config.GlobalConfig;
private _options: Config.DefaultOptions;
constructor(
globalConfig: Config.GlobalConfig,
options: Config.DefaultOptions,
) {
this._globalConfig = globalConfig;
this._options = options;
}
onRunComplete(context: Set<Context>, result: AggregatedResult) {
result.endTime = Date.now();
result.reporterOptions = { ...this._options };
const data = JSON.stringify(result);
const templatePath = path.resolve(__dirname, './index.html');
const htmlTemplate = fs.readFileSync(templatePath, 'utf-8');
result.reporterOptions.title = result.reporterOptions.title
? result.reporterOptions.title
: 'Jest Html Report';
const outputContext = htmlTemplate
.replace('%RESULTDATA%', data)
.replace('%TITLE%', result.reporterOptions.title);
const publicPath = result.reporterOptions.reportPath
? result.reporterOptions.reportPath
: './temp/';
const filename = result.reporterOptions.reportFileName
? result.reporterOptions.reportFileName
: 'result.html';
fs.existsSync(publicPath) === false && publicPath && mkdirs(publicPath);
fs.writeFileSync(publicPath.concat(filename), outputContext, 'utf-8');
}
}
module.exports = HTMLReport4Jest;