diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml index cb033ec7..4fb432e2 100644 --- a/.github/workflows/node.js.yml +++ b/.github/workflows/node.js.yml @@ -67,9 +67,15 @@ jobs: run: wget -q -O factorio.tar.gz https://www.factorio.com/get-download/${{ matrix.factorio-version }}/headless/linux64 && tar -xf factorio.tar.gz && rm factorio.tar.gz - name: Run tests if: ${{ !matrix.coverage }} + env: + # Re-run with debug logging to see output from passing tests + VERBOSE_TEST: ${{ runner.debug }} run: pnpm test --grep "^(Integration of Clusterio (clusterioctl|plugin system)|Clusterio Instance) " ${{ matrix.tests == 'other' && '--invert' || '' }} - name: Run coverage if: ${{ matrix.coverage }} + env: + # Re-run with debug logging to see output from passing tests + VERBOSE_TEST: ${{ runner.debug }} run: pnpm run ci-cover --grep "^(Integration of Clusterio (clusterioctl|plugin system)|Clusterio Instance) " ${{ matrix.tests == 'other' && '--invert' || '' }} - name: Upload coverage to Codecov if: ${{ matrix.coverage }} diff --git a/docs/contributing.md b/docs/contributing.md index 0692fd8b..6e5b4925 100644 --- a/docs/contributing.md +++ b/docs/contributing.md @@ -197,6 +197,8 @@ Use `pnpm run fast-test` to skip tests marked as slow, or `pnpm run no-external- These correspond to setting the `FAST_TEST` and `NO_EXTERNAL_TEST` environment variables and can be combined. For example, `FAST_TEST=y NO_EXTERNAL_TEST=y pnpm test` combines both exclusions. These flags still run the remaining integration setup and tests; neither produces an isolated unit-test run. +Console and log output is only printed for tests and hooks that fail, just above the failure. +Set `VERBOSE_TEST=y` to print all of it as it happens, or re-run a CI job with debug logging enabled. When writing slow tests call `slowTest(this)`, and for external API tests call `externalTest(this)`; both are exported from `test/common.js`. For tests that need a Factorio installation, call `requiresFactorio(this)` in the test or pass the suite to `requiresFactorio`, imported from `test/integration/index.js`. `test/common.js` also provides JSON round-trip/schema helpers, and `test/mock.js` provides shared mocks. diff --git a/package.json b/package.json index 7c8c4b7d..66909833 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "build": "tsc --build", "watch": "tsc --build --watch", "build-mod": "pnpm run -r build-mod --output-dir ../../dist", - "test": "pnpm run build && NODE_EXTRA_CA_CERTS=$(pwd)/test/file/tls/cert.pem mocha --enable-source-maps --check-leaks -R spec --recursive test \"plugins/*/test/**\" --exclude \"test/manual/**\"", + "test": "pnpm run build && NODE_EXTRA_CA_CERTS=$(pwd)/test/file/tls/cert.pem mocha --enable-source-maps --check-leaks -R ./test/reporter.cjs --recursive test \"plugins/*/test/**\" --exclude \"test/manual/**\"", "silent-test": "SILENT_TEST=y pnpm test", "fast-test": "FAST_TEST=y pnpm test", "no-external-test": "NO_EXTERNAL_TEST=y pnpm test", diff --git a/test/reporter.cjs b/test/reporter.cjs new file mode 100644 index 00000000..561cc823 --- /dev/null +++ b/test/reporter.cjs @@ -0,0 +1,38 @@ +// Spec reporter that holds back console output and prints it only for +// failing tests and hooks. Set VERBOSE_TEST to print everything as it happens. +"use strict"; +const util = require("node:util"); +const Mocha = require("mocha"); + +const { Spec } = Mocha.reporters; +const { EVENT_TEST_FAIL, EVENT_TEST_PASS, EVENT_TEST_PENDING } = Mocha.Runner.constants; +const methods = ["log", "info", "debug", "warn", "error"]; + +class CaptureReporter extends Spec { + constructor(runner, options) { + super(runner, options); + if (process.env.VERBOSE_TEST) { + return; + } + + const original = {}; + const captured = []; + for (const method of methods) { + original[method] = console[method]; + console[method] = (...args) => { captured.push([method, util.format(...args)]); }; + } + + const discard = () => { captured.length = 0; }; + runner.on(EVENT_TEST_PASS, discard); + runner.on(EVENT_TEST_PENDING, discard); + // Prepend so the output lands above the failure line. + runner.prependListener(EVENT_TEST_FAIL, () => { + for (const [method, text] of captured) { + original[method](text); + } + discard(); + }); + } +} + +module.exports = CaptureReporter;