-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetCoverageForSha.ts
112 lines (93 loc) · 3.33 KB
/
getCoverageForSha.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
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
import fs from "fs";
import * as core from "@actions/core";
import * as cache from "@actions/cache";
import { exec } from "@actions/exec";
import * as glob from "@actions/glob";
const appName = "jest-reports";
const coverageCachePath = `coverage`;
interface CovActionParams {
sha: string;
sinceSha?: string;
}
export const getCoverageForSha = async ({ sha, sinceSha }: CovActionParams) => {
if (sinceSha) core.info("computing PR coverage since base...");
else core.info("computing coverage on all tests...");
let mainCoverage = { coverageSummary: {}, testsOutput: {} };
const coverageatShaCacheKey = sinceSha
? `${appName}-cache-coverage-for-${sha}-since-${sinceSha}`
: `${appName}-cache-coverage-for-${sha}`;
core.info(`restoring coverage outputs for ${coverageatShaCacheKey}...`);
const foundCoverageOutputs = await cache.restoreCache(
[coverageCachePath],
coverageatShaCacheKey
);
if (foundCoverageOutputs) {
core.info(`found.`);
const summaryFile = fs.readFileSync(
`${process.cwd()}/coverage/coverage-summary.json`
);
mainCoverage.coverageSummary = JSON.parse(summaryFile.toString());
const testsOutputFile = fs.readFileSync(
`${process.cwd()}/coverage/tests-output.json`
);
mainCoverage.testsOutput = JSON.parse(testsOutputFile.toString());
} else {
core.info(`not found, let's checkout and run jest...`);
mainCoverage = await computeCoverageForSha({
sha,
sinceSha,
});
core.info("done. caching...");
await cache.saveCache([coverageCachePath], coverageatShaCacheKey);
}
return mainCoverage;
};
const installNodeModules = async () => {
const lockFileHash = await glob.hashFiles(`**/yarn.lock`);
const dependenciesCacheKey = `${appName}-cache-dependencies-${lockFileHash}`;
core.info(`restoring node_modules with key: ${dependenciesCacheKey}`);
const found = await cache.restoreCache(
["**/node_modules"],
dependenciesCacheKey
);
if (found) {
core.info("cache was restored, no need to install the dependencies.");
} else {
core.info("cache not found, installing dependencies...");
await exec(`yarn`);
core.info("saving node_modules to cache...");
await cache.saveCache(["**/node_modules"], dependenciesCacheKey);
}
};
const computeCoverageForSha = async ({ sha, sinceSha }: CovActionParams) => {
await exec(`git fetch`);
await exec(`git -c advice.detachedHead=false checkout ${sha}`);
await installNodeModules();
const since = sinceSha ? `--changedSince=${sinceSha}` : "";
// --coverageReporters=json-summary reports the small summary used to build the markdown tables in the PR comment
// --json outputs `coverage/tests-output.json` which includes `coverageMap` used for coverage annotations
await exec(
`npx`,
[
`jest`,
since,
"--ci",
"--coverage",
"--coverageReporters=json-summary",
"--json",
"--outputFile=coverage/tests-output.json",
],
{
ignoreReturnCode: true,
}
);
const summaryFile = fs.readFileSync(
`${process.cwd()}/coverage/coverage-summary.json`
);
const coverageSummary = JSON.parse(summaryFile.toString());
const testsOutputFile = fs.readFileSync(
`${process.cwd()}/coverage/tests-output.json`
);
const testsOutput = JSON.parse(testsOutputFile.toString());
return { coverageSummary, testsOutput };
};