Skip to content

Commit 07dbda2

Browse files
committed
Add code coverage for unit tests running in node
It's a first step to add code coverage. In order to get the code coverage report locally, you can run the following command: ```bash npx gulp unittestcli --coverage ``` The code coverage report will be generated in the `./build/coverage` directory. And the report can be consulted by opening: http://localhost:8888/build/coverage/index.html A GitHub workflow has also been added to run the unit tests with code coverage on each push and pull request. The report will be uploaded to Codecov.
1 parent f22fb6b commit 07dbda2

5 files changed

Lines changed: 386 additions & 7 deletions

File tree

.c8rc.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"reporter": ["text", "html", "lcov"],
3+
"include": [
4+
"build/lib-legacy/core/**/*.js",
5+
"build/lib-legacy/display/**/*.js",
6+
"build/lib-legacy/shared/**/*.js"
7+
],
8+
"exclude": ["build/lib-legacy/test/**"],
9+
"all": false,
10+
"check-coverage": false,
11+
"report-dir": "./build/coverage",
12+
"temp-directory": "./build/.c8_output"
13+
}

.github/workflows/coverage.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: Code Coverage
2+
on: [push, pull_request]
3+
permissions:
4+
contents: read
5+
6+
jobs:
7+
test:
8+
name: Test
9+
runs-on: ubuntu-latest
10+
11+
strategy:
12+
fail-fast: false
13+
matrix:
14+
node-version: [lts/*]
15+
16+
steps:
17+
- name: Checkout repository
18+
uses: actions/checkout@v6
19+
with:
20+
fetch-depth: 0
21+
22+
- name: Use Node.js ${{ matrix.node-version }}
23+
uses: actions/setup-node@v6
24+
with:
25+
node-version: ${{ matrix.node-version }}
26+
27+
- name: Install dependencies
28+
run: npm ci
29+
30+
- name: Run CLI unit tests with code coverage
31+
run: npx gulp unittestcli --coverage
32+
33+
- name: Upload results to Codecov
34+
uses: codecov/codecov-action@v5
35+
with:
36+
token: ${{ secrets.CODECOV_TOKEN }}
37+
fail_ci_if_error: true
38+
files: ./build/coverage/lcov.info
39+
flags: unittestcli
40+
name: codecov-umbrella
41+
verbose: true

gulpfile.mjs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1938,16 +1938,40 @@ gulp.task(
19381938
"generic-legacy",
19391939
"lib-legacy",
19401940
function runUnitTestCli(done) {
1941-
const options = [
1942-
"node_modules/jasmine/bin/jasmine",
1943-
"JASMINE_CONFIG_PATH=test/unit/clitests.json",
1944-
];
1945-
const jasmineProcess = startNode(options, { stdio: "inherit" });
1941+
const useCoverage = process.argv.includes("--coverage");
1942+
1943+
if (useCoverage) {
1944+
console.log("\n### Running unit tests with code coverage");
1945+
}
1946+
1947+
let jasmineProcess;
1948+
if (useCoverage) {
1949+
const options = [
1950+
"node_modules/c8/bin/c8.js",
1951+
"node",
1952+
"--max-http-header-size=80000",
1953+
"node_modules/jasmine/bin/jasmine",
1954+
"JASMINE_CONFIG_PATH=test/unit/clitests.json",
1955+
];
1956+
jasmineProcess = spawn("node", options, { stdio: "inherit" });
1957+
} else {
1958+
const options = [
1959+
"node_modules/jasmine/bin/jasmine",
1960+
"JASMINE_CONFIG_PATH=test/unit/clitests.json",
1961+
];
1962+
jasmineProcess = startNode(options, { stdio: "inherit" });
1963+
}
1964+
19461965
jasmineProcess.on("close", function (code) {
19471966
if (code !== 0) {
19481967
done(new Error("Unit tests failed."));
19491968
return;
19501969
}
1970+
if (useCoverage) {
1971+
console.log(
1972+
"\n### Code coverage report generated in ./build/coverage directory"
1973+
);
1974+
}
19511975
done();
19521976
});
19531977
}

0 commit comments

Comments
 (0)