Skip to content
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
3 changes: 3 additions & 0 deletions src/test-reader/mocha-reader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const { TreeBuilderDecorator } = require("./tree-builder-decorator");
const { TestReaderEvents } = require("../../events");
const { MasterEvents } = require("../../events");
const { getMethodsByInterface } = require("./utils");
const { enableSourceMaps } = require("../../utils/typescript");

async function readFiles(files, { esmDecorator, config, eventBus, runnableOpts }) {
const mocha = new Mocha(config);
Expand Down Expand Up @@ -104,6 +105,8 @@ function addLocationToRunnables(inBus, config, runnableOpts) {
return;
}

enableSourceMaps();

const sourceMapSupport = tryToRequireSourceMapSupport();
const { suiteMethods, testMethods } = getMethodsByInterface(config.ui);

Expand Down
59 changes: 44 additions & 15 deletions src/utils/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as logger from "./logger";

const TESTPLANE_TRANSFORM_HOOK = Symbol.for("testplane.transform.hook");

const TRANSFORM_EXTENSIONS = [".js", ".jsx", ".ts", ".tsx", ".mjs", ".cjs", ".mts", ".cts"];
const TRANSFORM_CODE_EXTENSIONS = [".js", ".jsx", ".ts", ".tsx", ".mjs", ".cjs", ".mts", ".cts"];
const ASSET_EXTENSIONS = [
".css",
".scss",
Expand All @@ -20,6 +20,10 @@ const ASSET_EXTENSIONS = [
".woff2",
];

type ProcessWithTransformHook = typeof process & {
[TESTPLANE_TRANSFORM_HOOK]?: { revert: () => void; enableSourceMaps: () => void };
};

let transformFunc: null | ((code: string, sourceFile: string, sourceMaps: boolean) => string) = null;

export const transformCode = (
Expand Down Expand Up @@ -83,37 +87,62 @@ export const transformCode = (
};

export const registerTransformHook = (isSilent: boolean = false): void => {
const processWithEsbuildSymbol = process as typeof process & {
[TESTPLANE_TRANSFORM_HOOK]?: { revert: () => void };
};
const processWithTranspileSymbol = process as ProcessWithTransformHook;

if (processWithEsbuildSymbol[TESTPLANE_TRANSFORM_HOOK] || process.env.TS_ENABLE === "false") {
if (processWithTranspileSymbol[TESTPLANE_TRANSFORM_HOOK] || process.env.TS_ENABLE === "false") {
return;
}

try {
const revertTransformHook = addHook(
(code, filename) => transformCode(code, { sourceFile: filename, sourceMaps: false, isSilent }),
{
exts: TRANSFORM_EXTENSIONS,
matcher: filename => !filename.includes("node_modules"),
ignoreNodeModules: false,
},
);
const mkTransformCodeHook =
(sourceMaps = false): Parameters<typeof addHook>[0] =>
(code, sourceFile) =>
transformCode(code, { sourceFile, sourceMaps, isSilent });

const transformCodeOptions: Parameters<typeof addHook>[1] = {
exts: TRANSFORM_CODE_EXTENSIONS,
ignoreNodeModules: true,
};

let areSourceMapsEnabled = false;

let revertTransformHook = addHook(mkTransformCodeHook(), transformCodeOptions);

const revertAssetHook = addHook(() => "module.exports = {};", {
exts: ASSET_EXTENSIONS,
ignoreNodeModules: false,
});

const enableSourceMaps = (): void => {
if (areSourceMapsEnabled) {
return;
}

areSourceMapsEnabled = true;

revertTransformHook();

revertTransformHook = addHook(mkTransformCodeHook(true), transformCodeOptions);
};

const revertAll = (): void => {
revertTransformHook();
revertAssetHook();
delete processWithEsbuildSymbol[TESTPLANE_TRANSFORM_HOOK];
delete processWithTranspileSymbol[TESTPLANE_TRANSFORM_HOOK];
};

processWithEsbuildSymbol[TESTPLANE_TRANSFORM_HOOK] = { revert: revertAll };
processWithTranspileSymbol[TESTPLANE_TRANSFORM_HOOK] = { revert: revertAll, enableSourceMaps };
} catch (err) {
logger.warn(`testplane: an error occurred while trying to register transform hook.`, err);
}
};

export const enableSourceMaps = (): void => {
const processWithTranspileSymbol = process as ProcessWithTransformHook;

if (!processWithTranspileSymbol[TESTPLANE_TRANSFORM_HOOK]) {
return;
}

processWithTranspileSymbol[TESTPLANE_TRANSFORM_HOOK].enableSourceMaps();
};
10 changes: 10 additions & 0 deletions test/src/test-reader/mocha-reader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe("test-reader/mocha-reader", () => {
let MochaConstructorStub;
let SourceMapSupportStub;
let getMethodsByInterfaceStub;
let enableSourceMapsStub;
let readFiles;

const mkMochaSuiteStub_ = () => {
Expand Down Expand Up @@ -47,11 +48,13 @@ describe("test-reader/mocha-reader", () => {
install: sinon.stub(),
};
getMethodsByInterfaceStub = sinon.stub().returns({ suiteMethods: [], testMethods: [] });
enableSourceMapsStub = sinon.stub();

readFiles = proxyquire("src/test-reader/mocha-reader", {
mocha: MochaConstructorStub,
"@cspotcode/source-map-support": SourceMapSupportStub,
"./utils": { getMethodsByInterface: getMethodsByInterfaceStub },
"../../utils/typescript": { enableSourceMaps: enableSourceMapsStub },
}).readFiles;

sandbox.stub(MochaEventBus, "create").returns(Object.create(MochaEventBus.prototype));
Expand Down Expand Up @@ -262,6 +265,13 @@ describe("test-reader/mocha-reader", () => {
assert.doesNotThrow(() => globalCtx.describe());
});

it("should enable testplane source maps before installing 'source-map-support'", async () => {
await readFiles_({ config: { ui: "bdd" }, runnableOpts: { saveLocations: true } });

assert.calledOnce(enableSourceMapsStub);
assert.callOrder(enableSourceMapsStub, SourceMapSupportStub.install);
});

it("should set 'hookRequire' option on install source-map-support", async () => {
await readFiles_({ config: { ui: "bdd" }, runnableOpts: { saveLocations: true } });

Expand Down
24 changes: 22 additions & 2 deletions test/src/utils/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ describe("utils/typescript", () => {

let ts: typeof import("src/utils/typescript");
let addHookStub: SinonStub;
let revertHookStub: SinonStub;
const TESTPLANE_TRANSFORM_HOOK = Symbol.for("testplane.transform.hook");

beforeEach(() => {
addHookStub = sinon.stub();
revertHookStub = sinon.stub();
addHookStub = sinon.stub().returns(revertHookStub);
ts = proxyquire.noCallThru().load("src/utils/typescript", {
pirates: {
addHook: addHookStub,
Expand All @@ -27,7 +29,7 @@ describe("utils/typescript", () => {
it("should add pirates hook", () => {
ts.registerTransformHook();

assert.calledOnce(addHookStub);
assert.calledTwice(addHookStub);
});

it("should not call register if typescript was already installed", () => {
Expand All @@ -48,4 +50,22 @@ describe("utils/typescript", () => {
process.env.TS_ENABLE = "undefined";
});
});

describe("enableSourceMaps", () => {
it("should not do anything if transform hook is not registered", () => {
ts.enableSourceMaps();

assert.notCalled(addHookStub);
});

it("should re-register transform hook with source maps", () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But you didn't check that the flag sourceMap: true is specified on second call

Copy link
Member Author

@KuznetsovRoman KuznetsovRoman Sep 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We cant.
We just pass callback (code, filename) => transformCode(...) to "addHook"
So "addHook" was just called with some function and thats all we can get.
"flag sourceMap: true" is internal detail of "registerTransformHook" function implementation

ts.registerTransformHook();
const addHookPrevCallCount = addHookStub.callCount;

ts.enableSourceMaps();

assert.calledOnce(revertHookStub);
assert.equal(addHookStub.callCount, addHookPrevCallCount + 1);
});
});
});
Loading