Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix ts-node #979

Merged
merged 2 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
Kabir-Ivan marked this conversation as resolved.
Show resolved Hide resolved
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
Loading