Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

fix: surface ts-node compile errors (#5572)

Sam Adamsadd4cf8166b3
diff --git a/lib/nodejs/esm-utils.js b/lib/nodejs/esm-utils.js
index 363e7182..00837b80 100644
--- a/lib/nodejs/esm-utils.js
+++ b/lib/nodejs/esm-utils.js
@@ -98,6 +98,9 @@ const requireModule = async (file, esmDecorator) => {
return require(file);
} catch (requireErr) {
debug("requireModule caught err: %O", requireErr.message);
+ if (requireErr.name === 'TSError') {
+ throw requireErr;
+ }
try {
return dealWithExports(await formattedImport(file, esmDecorator));
} catch (importErr) {
diff --git a/test/node-unit/esm-utils.spec.js b/test/node-unit/esm-utils.spec.js
index caaaf249..c86af8a6 100644
--- a/test/node-unit/esm-utils.spec.js
+++ b/test/node-unit/esm-utils.spec.js
@@ -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 () {
diff --git a/test/node-unit/fixtures/mock-ts-node-compile-err.ts b/test/node-unit/fixtures/mock-ts-node-compile-err.ts
new file mode 100644
index 00000000..1ae39ebb
--- /dev/null
+++ b/test/node-unit/fixtures/mock-ts-node-compile-err.ts
@@ -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');