Repository navigation
fix(report-file): surface write stream errors instead of a TypeError - #2058
Merged
johanneswuerbach merged 1 commit intoSep 7, 2026
Merged
johanneswuerbach merged 1 commit into
johanneswuerbach merged 1 commit into
Conversation
When the report file could not be opened, testem died with "TypeError: Cannot read properties of undefined (reading 'end')" and the real filesystem error was never shown. lib/utils/report-file.js registered `finish` as an 'error' listener on the write stream. `finish` is a plain function, so `this` inside it is the stream that emitted the event, not the ReportFile instance, and `this.outputStream` is undefined. The listener threw before the second 'error' listener could reject closePromise, so the throw escaped as an uncaught exception and the underlying error was lost. The same handler was also registered for 'end', which a Writable never emits. Drop both listeners. close() already ends the stream, and closePromise already resolves on 'finish' and rejects on 'error', so the fs error now reaches the caller. Attach a no-op catch to closePromise as well, because an open failure rejects on the next tick, long before close() attaches its handler, and that would otherwise be reported as an unhandled rejection.
Gaurav0
pushed a commit
to Gaurav0/testem
that referenced
this pull request
Sep 9, 2026
…estem#2058) When the report file could not be opened, testem died with "TypeError: Cannot read properties of undefined (reading 'end')" and the real filesystem error was never shown. lib/utils/report-file.js registered `finish` as an 'error' listener on the write stream. `finish` is a plain function, so `this` inside it is the stream that emitted the event, not the ReportFile instance, and `this.outputStream` is undefined. The listener threw before the second 'error' listener could reject closePromise, so the throw escaped as an uncaught exception and the underlying error was lost. The same handler was also registered for 'end', which a Writable never emits. Drop both listeners. close() already ends the stream, and closePromise already resolves on 'finish' and rejects on 'error', so the fs error now reaches the caller. Attach a no-op catch to closePromise as well, because an open failure rejects on the next tick, long before close() attaches its handler, and that would otherwise be reported as an unhandled rejection.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
testem cidies withTypeError: Cannot read properties of undefined (reading 'end')when it cannot open the report file, and the real filesystem error never reaches the user. I hit it with atestem.jsonwhosereport_filepoints at an existing directory, so opening it for writing fails with EISDIR.The cause is in
lib/utils/report-file.js. Line 26 registersfinishas anerrorlistener on the write stream, butfinishis a plain function, sothisinside it is the stream that emitted the event, not theReportFileinstance.this.outputStreamis undefined there and line 21 throws. That throw happens inside theerroremit, before the seconderrorlistener on line 30 can rejectclosePromise, so it escapes as an uncaught exception and the open error is lost. Line 25 registers the same handler forend, which a writable stream never emits. Both are leftovers from whenReportFilewrapped two separate streams, an output stream and the file stream.This removes both listeners.
close()already ends the stream, andclosePromisealready resolves onfinishand rejects onerror, so the fs error now reaches the caller and testem shuts down through its normal path with exit code 1, the same as it already does when the report file's directory cannot be created.closePromisealso gets a no-op catch, because an open failure rejects on the next tick, long beforeclose()attaches its handler, and node would otherwise kill the process for an unhandled rejection.Verification on macOS, node 22.23.2:
All 8 failures are 90 second timeouts in
tests/ci/ci_tests.jsandtests/ci/dev_tests.js, which launch Headless Firefox, and Firefox is not installed on the machine I ran this on.npx mocha tests/ci/ci_tests.js tests/ci/dev_tests.js -g "handles todos correctly|keeps runner association"fails identically on unmodified master. Every report file test passes with the change: the four intests/ci/report_file_tests.jsand the two intests/utils/report-file_tests.js.