Skip to content

Commit

Permalink
fix: fix ts-node (#979)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kabir-Ivan authored Aug 5, 2024
1 parent dc6217b commit 2e0b023
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 22 deletions.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,15 @@
"uglifyify": "3.0.4"
},
"peerDependencies": {
"ts-node": ">=10.5.0"
"ts-node": ">=10.5.0",
"@swc/core": ">=1.3.96"
},
"peerDependenciesMeta": {
"ts-node": {
"optional": true
},
"@swc/core": {
"optional": true
}
}
}
54 changes: 43 additions & 11 deletions src/utils/typescript.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import _ from "lodash";
import logger from "./logger";

export const tryToRegisterTsNode = (): void => {
if (process.env.TS_ENABLE === "false") {
return;
}
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { REGISTER_INSTANCE } = require("ts-node");
Expand All @@ -11,31 +15,59 @@ export const tryToRegisterTsNode = (): void => {

// eslint-disable-next-line @typescript-eslint/no-var-requires
const { register }: typeof import("ts-node") = require("ts-node");
let swc = false;

try {
require("@swc/core");
swc = true;
} catch {} // eslint-disable-line no-empty

const skipProjectRaw = process.env.TS_NODE_SKIP_PROJECT ?? "true";
const transpileOnlyRaw = process.env.TS_NODE_TRANSPILE_ONLY ?? "true";
const swcRaw = process.env.TS_NODE_SWC ?? swc.toString();
const swcRaw = process.env.TS_NODE_SWC ?? "true";

if (JSON.parse(swcRaw)) {
try {
register({
skipProject: JSON.parse(skipProjectRaw),
transpileOnly: JSON.parse(transpileOnlyRaw),
swc: true,
});
return;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (err: any) {
if (err.code === "MODULE_NOT_FOUND") {
logger.warn(
`testplane: you may install @swc/core for significantly faster reading of typescript tests.`,
);
} else {
logger.warn(`testplane: could not load @swc/core:`, err);
}
}
}

try {
register({
skipProject: JSON.parse(skipProjectRaw),
transpileOnly: JSON.parse(transpileOnlyRaw),
swc: JSON.parse(swcRaw),
swc: false,
compilerOptions: {
allowJs: true,
module: "nodenext",
moduleResolution: "nodenext",
},
});
return;
// eslint-disable-next-line no-empty
} catch (err) {}

try {
register({
skipProject: JSON.parse(skipProjectRaw),
transpileOnly: JSON.parse(transpileOnlyRaw),
swc: false,
});
return;
// eslint-disable-next-line no-empty
} catch (err) {
const params = `swc: "${swcRaw}", transpileOnly: "${transpileOnlyRaw}", skipProject: "${skipProjectRaw}"`;
console.error(`testplane: an error occured while trying to register ts-node (${params}):`, err);
const params = `swc: "false", transpileOnly: "${transpileOnlyRaw}", skipProject: "${skipProjectRaw}"`;
logger.warn(
`testplane: an error occured while trying to register ts-node (${params}). TypeScript tests won't be read:`,
err,
);
}
} catch {} // eslint-disable-line no-empty
};
34 changes: 24 additions & 10 deletions test/src/utils/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,14 @@ describe("utils/typescript", () => {
assert.notCalled(registerStub);
});

it("should pass 'allowJs' option", () => {
it("should not call register if TS_ENABLE is false", () => {
process.env.TS_ENABLE = "false";

ts.tryToRegisterTsNode();

assert.calledOnceWith(
registerStub,
sinon.match({
compilerOptions: {
allowJs: true,
},
}),
);
assert.notCalled(registerStub);

process.env.TS_ENABLE = "undefined";
});

it("should respect env vars", () => {
Expand Down Expand Up @@ -80,6 +77,13 @@ describe("utils/typescript", () => {
});

it("should not use swc if not installed", () => {
registerStub
.withArgs(
sinon.match({
swc: true,
}),
)
.throws("MODULE_NOT_FOUND");
ts = proxyquire.noCallThru().load("src/utils/typescript", {
"ts-node": {
register: registerStub,
Expand All @@ -89,14 +93,24 @@ describe("utils/typescript", () => {

ts.tryToRegisterTsNode();

assert.calledOnceWith(
assert.calledWith(
registerStub,
sinon.match({
swc: false,
}),
);
});

it("should not throw if ts-node throws", () => {
registerStub.throws();
ts = proxyquire.noCallThru().load("src/utils/typescript", {
"ts-node": registerStub,
"@swc/core": null,
});

assert.doesNotThrow(() => ts.tryToRegisterTsNode());
});

it("should not throw if nothing is installed", () => {
ts = proxyquire.noCallThru().load("src/utils/typescript", {
"ts-node": null,
Expand Down

0 comments on commit 2e0b023

Please sign in to comment.