Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/nodejs/esm-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ const requireModule = async (file, esmDecorator) => {
try {
return require(file);
} catch (requireErr) {
if (requireErr.name === 'TSError') {
Comment thread
mark-wiemer marked this conversation as resolved.
Resolved
throw requireErr;
}
debug("requireModule caught err: %O", requireErr.message);
Comment thread
mark-wiemer marked this conversation as resolved.
Resolved
try {
return dealWithExports(await formattedImport(file, esmDecorator));
Expand Down
14 changes: 14 additions & 0 deletions test/node-unit/esm-utils.spec.js
Comment thread
mark-wiemer marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ describe("esm-utils", function () {
},
);
});

it("should surface the ts-node TSError error rather than falling back to `import(...)`", async function () {
return expect(
() =>
esmUtils.requireOrImport(
"../../test/node-unit/fixtures/mock-ts-node-compile-err.ts",
),
"to be rejected with error satisfying",
{
name: "TSError",
message: /A TS compilation error/,
},
);
});
});

describe("loadFilesAsync()", function () {
Expand Down
13 changes: 13 additions & 0 deletions test/node-unit/fixtures/mock-ts-node-compile-err.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
Stripped down version of this error from ts-node:
https://github.com/TypeStrong/ts-node/blob/ddb05ef23be92a90c3ecac5a0220435c65ebbd2a/src/index.ts#L435
It's thrown when ts-node hits a compilation error.
*/
class TSError extends Error {
constructor(...args) {
super(...args);

this.name = 'TSError';
}
}
throw new TSError('A TS compilation error');