@@ -6,11 +6,8 @@ const yaml = require('js-yaml');
6
6
7
7
const filePath = 'dedupData.yaml' ;
8
8
9
-
10
9
// middleware
11
- export default function middleware (
12
-
13
- ) : ( req : Request , res : Response , next : NextFunction ) => void {
10
+ export default function middleware ( ) : ( req : Request , res : Response , next : NextFunction ) => void {
14
11
// console.log("Inside middleware...");
15
12
16
13
// @ts -ignore
@@ -22,15 +19,12 @@ export default function middleware(
22
19
} ) ;
23
20
return ( req : Request , res : Response , next : NextFunction ) => {
24
21
res . on ( "finish" , ( ) => {
25
-
26
22
afterMiddleware ( req , res ) ;
27
23
} ) ;
28
24
next ( ) ;
29
-
30
25
} ;
31
26
}
32
27
33
-
34
28
export function afterMiddleware ( req : Request , res : Response ) {
35
29
let id = req . get ( "KEPLOY-TEST-ID" ) ;
36
30
if ( ! id ) {
@@ -44,7 +38,6 @@ export function afterMiddleware(req: Request, res: Response) {
44
38
executedLinesByFile : executedLinesByFile
45
39
} ;
46
40
47
-
48
41
let existingData = [ ] ;
49
42
50
43
try {
@@ -55,7 +48,6 @@ export function afterMiddleware(req: Request, res: Response) {
55
48
console . error ( "Error reading existing file:" , error ) ;
56
49
}
57
50
58
-
59
51
if ( ! Array . isArray ( existingData ) ) {
60
52
console . error ( 'Expected an array for existingData, but got:' , typeof existingData ) ;
61
53
existingData = [ ] ; // Reset to an empty array or handle accordingly
@@ -75,58 +67,58 @@ export function afterMiddleware(req: Request, res: Response) {
75
67
// console.log("Data has been appended and logged to", filePath);
76
68
}
77
69
78
- // isJsonValid checks whether o is a valid JSON or not
79
-
80
70
let count = 0 ;
81
- const executedLinebyEachTest = new Array ( ) ;
82
- function GetCoverage ( ) {
83
- console . log ( "Calculating per request coverage..." ) ;
84
- count ++ ;
85
- let executedLinesByFile = { } ;
86
- // iterate over global.__coverage__
87
- // @ts -ignore
88
- for ( const filename in global . __coverage__ ) {
89
- // console.log("FIlenamae", filename);
90
- // while (1) {
91
- // @ts -ignore
92
- let coverageData = global . __coverage__ [ filename ] ;
93
- // console.log("Inside GetCoverage " + count);
94
- // console.log(coverageData);
95
-
96
-
97
- // for (const filePath of Object.keys(coverageData)) {
98
- const executedLines = new Set ( ) ;
99
- const fileCoverage = coverageData ;
100
- const statementMap = fileCoverage . statementMap ;
101
- const hitCounts = fileCoverage . s ;
102
- if ( count > 1 ) {
103
- // iterate over hitcounts and subtract the previous hitcounts
104
- // @ts -ignore
105
- var prevHitCounts = executedLinebyEachTest [ count - 2 ] ;
106
-
107
- for ( const statementId in hitCounts ) {
108
- hitCounts [ statementId ] = Math . abs (
109
- hitCounts [ statementId ] - prevHitCounts [ statementId ]
110
- ) ;
111
- }
112
- }
71
+ type HitCounts = { [ statementId : string ] : number } ;
72
+ type CoverageData = { [ filename : string ] : { statementMap : any , s : HitCounts } } ;
73
+ type ExecutedLineByEachTest = { [ filename : string ] : HitCounts } [ ] ;
74
+
75
+ const executedLinebyEachTest : ExecutedLineByEachTest = [ ] ;
76
+ const executedLinesByFile : { [ filename : string ] : number [ ] } = { } ;
113
77
114
- for ( const statementId in statementMap ) {
115
- if ( hitCounts [ statementId ] > 0 ) {
116
- const executedLine = statementMap [ statementId ] . start . line ;
117
- executedLines . add ( executedLine ) ;
118
- }
78
+ declare const global : { __coverage__ : CoverageData } ;
79
+
80
+ function GetCoverage ( ) {
81
+ count ++ ;
82
+
83
+ for ( const filename in global . __coverage__ ) {
84
+ let coverageData = global . __coverage__ [ filename ] ;
85
+ const executedLines = new Set < number > ( ) ;
86
+ const fileCoverage = coverageData ;
87
+ const statementMap = fileCoverage . statementMap ;
88
+ const hitCounts = fileCoverage . s ;
89
+ // console.log("hitcounts", hitCounts);
90
+
91
+ if ( count > 1 ) {
92
+ if ( ! executedLinebyEachTest [ count - 2 ] ) {
93
+ executedLinebyEachTest [ count - 2 ] = { } ;
94
+ }
95
+ const prevHitCounts = executedLinebyEachTest [ count - 2 ] [ filename ] || { } ;
96
+ for ( const statementId in hitCounts ) {
97
+ const currentHitCount = isNaN ( hitCounts [ statementId ] ) ? 0 : hitCounts [ statementId ] ;
98
+ const previousHitCount = isNaN ( prevHitCounts [ statementId ] ) ? 0 : prevHitCounts [ statementId ] ;
99
+ hitCounts [ statementId ] = Math . abs ( currentHitCount - previousHitCount ) ;
100
+ }
101
+ }
102
+
103
+ for ( const statementId in statementMap ) {
104
+ if ( hitCounts [ statementId ] > 0 ) {
105
+ const executedLine = statementMap [ statementId ] . start . line ;
106
+ // console.log("executedLine", executedLine);
107
+ executedLines . add ( executedLine ) ;
108
+ }
109
+ }
110
+
111
+ executedLinesByFile [ filename ] = Array . from ( executedLines ) . sort ( ( a , b ) => a - b ) ;
112
+
113
+ if ( ! executedLinebyEachTest [ count - 1 ] ) {
114
+ executedLinebyEachTest [ count - 1 ] = { } ;
115
+ }
116
+
117
+ executedLinebyEachTest [ count - 1 ] [ filename ] = { ...hitCounts } ;
118
+
119
+ // console.log("Executed lines by file executedLinebyEachTest:", executedLinebyEachTest);
119
120
}
120
- // @ts -ignore
121
- executedLinesByFile [ filename ] = Array . from ( executedLines ) . sort ( ( a , b ) => a - b ) ;
122
- // }
123
- // @ts -ignore
124
- executedLinebyEachTest . push ( { ...hitCounts } ) ;
125
-
126
- // console.log("Executed lines by file:", executedLinesByFile);
127
- // extract s from the coverage data
128
- }
129
- return executedLinesByFile ;
121
+ return executedLinesByFile ;
130
122
}
131
123
132
124
module . exports = middleware ;
0 commit comments