|
| 1 | +const console = require('console'); |
| 2 | + |
| 3 | +const currentTests = {}; |
| 4 | + |
| 5 | +function onInterrupt(reason) { |
| 6 | + console.error(`\n\n${reason} with ${Object.keys(currentTests).length} running tests:`); |
| 7 | + Object.entries(currentTests).forEach(([test, startTime]) => { |
| 8 | + const duration = Date.now() - startTime.getTime(); |
| 9 | + console.error(` - ${test}: started at ${startTime}, duration so far: ${duration}ms`); |
| 10 | + }); |
| 11 | + process.exit(1); |
| 12 | +} |
| 13 | + |
| 14 | +// --> not working... maybe due to run-p or containerized environment? (no root process) |
| 15 | +process.on('SIGINT', () => onInterrupt('ABORTED')); |
| 16 | +process.on('SIGKILL', () => onInterrupt('KILLED')); |
| 17 | +process.on('SIGTERM', () => onInterrupt('TERMINATED')); |
| 18 | + |
| 19 | +setInterval(() => { |
| 20 | + Object.entries(currentTests).forEach(([test, startTime]) => { |
| 21 | + const duration = Date.now() - startTime.getTime(); |
| 22 | + if (duration > 300000) { |
| 23 | + console.error(` ${test}: started at ${startTime}, duration so far: ${duration}ms`); |
| 24 | + } |
| 25 | + }); |
| 26 | +}, 300000); |
| 27 | + |
| 28 | +exports.mochaHooks = { |
| 29 | + beforeEach(done) { |
| 30 | + // TODO: allocate a UUID for the test (trace id) ; which should be added to **every** outgoing request |
| 31 | + |
| 32 | + const currentTest = this.currentTest.fullTitle(); |
| 33 | + currentTests[currentTest] = new Date(); |
| 34 | + console.log(`Starting: ${currentTest}`); |
| 35 | + |
| 36 | + done(); |
| 37 | + }, |
| 38 | + afterEach(done) { |
| 39 | + // TODO: if the test failed, log this tests' traceID |
| 40 | + // TODO: also gather the logs and print them? (or generate a report, so that GHA can extract |
| 41 | + // and display these logs) |
| 42 | + |
| 43 | + const currentTest = this.currentTest.fullTitle(); |
| 44 | + delete currentTests[currentTest]; |
| 45 | + |
| 46 | + done(); |
| 47 | + }, |
| 48 | +}; |
0 commit comments