Skip to content

Commit 27b2bf3

Browse files
fix: dedup changes (#115)
Signed-off-by: shivamsouravjha <shivamsouravjha@gmail.com>
1 parent 03bae50 commit 27b2bf3

File tree

1 file changed

+50
-58
lines changed

1 file changed

+50
-58
lines changed

v2/dedup/middleware.ts

+50-58
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@ const yaml = require('js-yaml');
66

77
const filePath = 'dedupData.yaml';
88

9-
109
// 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 {
1411
// console.log("Inside middleware...");
1512

1613
// @ts-ignore
@@ -22,15 +19,12 @@ export default function middleware(
2219
});
2320
return (req: Request, res: Response, next: NextFunction) => {
2421
res.on("finish", () => {
25-
2622
afterMiddleware(req, res);
2723
});
2824
next();
29-
3025
};
3126
}
3227

33-
3428
export function afterMiddleware(req: Request, res: Response) {
3529
let id = req.get("KEPLOY-TEST-ID");
3630
if (!id) {
@@ -44,7 +38,6 @@ export function afterMiddleware(req: Request, res: Response) {
4438
executedLinesByFile: executedLinesByFile
4539
};
4640

47-
4841
let existingData = [];
4942

5043
try {
@@ -55,7 +48,6 @@ export function afterMiddleware(req: Request, res: Response) {
5548
console.error("Error reading existing file:", error);
5649
}
5750

58-
5951
if (!Array.isArray(existingData)) {
6052
console.error('Expected an array for existingData, but got:', typeof existingData);
6153
existingData = []; // Reset to an empty array or handle accordingly
@@ -75,58 +67,58 @@ export function afterMiddleware(req: Request, res: Response) {
7567
// console.log("Data has been appended and logged to", filePath);
7668
}
7769

78-
// isJsonValid checks whether o is a valid JSON or not
79-
8070
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[] } = {};
11377

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);
119120
}
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;
130122
}
131123

132124
module.exports = middleware;

0 commit comments

Comments
 (0)