Skip to content

Commit 95bd169

Browse files
test: suppress logs except errors
1 parent 1e136d0 commit 95bd169

File tree

3 files changed

+136
-2
lines changed

3 files changed

+136
-2
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
"start-server": "node test/testUtils/test-server.js",
1111
"local-run-script": "node local-run.js",
1212
"local-run": "start-server-and-test start-server http://localhost:8080 local-run-script",
13-
"test-run": "mocha --parallel --reporter json --reporter-option output=src-report.json --jobs 100 test/**.js",
13+
"test-run": "mocha --require test/setup.js --parallel --reporter json --reporter-option output=src-report.json --jobs 100 test/**.js",
1414
"test": "yarn run lint && start-server-and-test start-server http://localhost:8080 test-run",
15-
"test-run-dist": "DIST=true mocha --parallel --reporter json --reporter-option output=dist-report.json --jobs 100 test/**.js",
15+
"test-run-dist": "DIST=true mocha --require test/setup.js --parallel --reporter json --reporter-option output=dist-report.json --jobs 100 test/**.js",
1616
"test-dist": "yarn run lint && start-server-and-test start-server http://localhost:8080 test-run-dist",
1717
"prepare": "husky"
1818
},

test/setup.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const {
2+
setupLogSuppression,
3+
restoreLogsOnFailure,
4+
restoreMethods,
5+
} = require('./testUtils/suppress-logs');
6+
7+
// Root hooks for Mocha
8+
exports.mochaHooks = {
9+
beforeEach() {
10+
setupLogSuppression();
11+
},
12+
13+
afterEach() {
14+
restoreLogsOnFailure.call(this);
15+
},
16+
17+
afterAll() {
18+
restoreMethods();
19+
},
20+
};

test/testUtils/suppress-logs.js

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
const core = require('@actions/core');
2+
const path = require('node:path');
3+
4+
// Get project root directory (where package.json is)
5+
const projectRoot = path.dirname(path.dirname(__dirname));
6+
7+
// Store original methods
8+
const originalMethods = {
9+
info: core.info,
10+
debug: core.debug,
11+
warning: core.warning,
12+
error: core.error,
13+
setSecret: core.setSecret,
14+
setOutput: core.setOutput,
15+
setFailed: core.setFailed,
16+
notice: core.notice,
17+
startGroup: core.startGroup,
18+
endGroup: core.endGroup,
19+
};
20+
21+
// Store logs during test execution
22+
let logs = [];
23+
24+
/**
25+
* Setup log suppression before each test
26+
*/
27+
const setupLogSuppression = () => {
28+
// Clear logs
29+
logs = [];
30+
31+
// Mock core methods to capture logs instead of printing
32+
core.info = (...args) => {
33+
logs.push({ level: 'info', args });
34+
};
35+
36+
core.debug = (...args) => {
37+
logs.push({ level: 'debug', args });
38+
};
39+
40+
core.warning = (...args) => {
41+
logs.push({ level: 'warning', args });
42+
};
43+
44+
core.error = (...args) => {
45+
logs.push({ level: 'error', args });
46+
};
47+
48+
core.setSecret = () => {
49+
// Suppress ::add-mask:: output
50+
};
51+
52+
core.setOutput = () => {
53+
// Suppress ::set-output:: output
54+
};
55+
56+
core.setFailed = (...args) => {
57+
logs.push({ level: 'setFailed', args });
58+
};
59+
60+
core.notice = (...args) => {
61+
logs.push({ level: 'notice', args });
62+
};
63+
64+
core.startGroup = () => {
65+
// Suppress group output
66+
};
67+
68+
core.endGroup = () => {
69+
// Suppress group output
70+
};
71+
};
72+
73+
/**
74+
* Restore original methods and dump logs if test failed
75+
*/
76+
const restoreLogsOnFailure = function () {
77+
// Check if current test failed
78+
if (this.currentTest && this.currentTest.state === 'failed') {
79+
// Dump all captured logs
80+
const testName = this.currentTest.title;
81+
const testFile = path.relative(projectRoot, this.currentTest.file);
82+
console.log(`\n--- Logs from failed test: "${testName}" (${testFile}) ---`);
83+
for (const log of logs) {
84+
const message = log.args.join(' ');
85+
console.log(`[${log.level.toUpperCase()}] ${message}`);
86+
}
87+
console.log('--- End of logs ---\n');
88+
}
89+
90+
// Clear logs array
91+
logs = [];
92+
};
93+
94+
/**
95+
* Restore original core methods
96+
*/
97+
const restoreMethods = () => {
98+
core.info = originalMethods.info;
99+
core.debug = originalMethods.debug;
100+
core.warning = originalMethods.warning;
101+
core.error = originalMethods.error;
102+
core.setSecret = originalMethods.setSecret;
103+
core.setOutput = originalMethods.setOutput;
104+
core.setFailed = originalMethods.setFailed;
105+
core.notice = originalMethods.notice;
106+
core.startGroup = originalMethods.startGroup;
107+
core.endGroup = originalMethods.endGroup;
108+
};
109+
110+
module.exports = {
111+
setupLogSuppression,
112+
restoreLogsOnFailure,
113+
restoreMethods,
114+
};

0 commit comments

Comments
 (0)