Skip to content

Commit 56a12cd

Browse files
fix: tests location when calling list-tests cli command
1 parent 4c084ed commit 56a12cd

File tree

4 files changed

+79
-17
lines changed

4 files changed

+79
-17
lines changed

src/test-reader/mocha-reader/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const { TreeBuilderDecorator } = require("./tree-builder-decorator");
88
const { TestReaderEvents } = require("../../events");
99
const { MasterEvents } = require("../../events");
1010
const { getMethodsByInterface } = require("./utils");
11+
const { enableSourceMaps } = require("../../utils/typescript");
1112

1213
async function readFiles(files, { esmDecorator, config, eventBus, runnableOpts }) {
1314
const mocha = new Mocha(config);
@@ -104,6 +105,8 @@ function addLocationToRunnables(inBus, config, runnableOpts) {
104105
return;
105106
}
106107

108+
enableSourceMaps();
109+
107110
const sourceMapSupport = tryToRequireSourceMapSupport();
108111
const { suiteMethods, testMethods } = getMethodsByInterface(config.ui);
109112

src/utils/typescript.ts

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as logger from "./logger";
44

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

7-
const TRANSFORM_EXTENSIONS = [".js", ".jsx", ".ts", ".tsx", ".mjs", ".cjs", ".mts", ".cts"];
7+
const TRANSFORM_CODE_EXTENSIONS = [".js", ".jsx", ".ts", ".tsx", ".mjs", ".cjs", ".mts", ".cts"];
88
const ASSET_EXTENSIONS = [
99
".css",
1010
".scss",
@@ -20,6 +20,10 @@ const ASSET_EXTENSIONS = [
2020
".woff2",
2121
];
2222

23+
type ProcessWithTransformHook = typeof process & {
24+
[TESTPLANE_TRANSFORM_HOOK]?: { revert: () => void; enableSourceMaps: () => void };
25+
};
26+
2327
let transformFunc: null | ((code: string, sourceFile: string, sourceMaps: boolean) => string) = null;
2428

2529
export const transformCode = (
@@ -83,37 +87,62 @@ export const transformCode = (
8387
};
8488

8589
export const registerTransformHook = (isSilent: boolean = false): void => {
86-
const processWithEsbuildSymbol = process as typeof process & {
87-
[TESTPLANE_TRANSFORM_HOOK]?: { revert: () => void };
88-
};
90+
const processWithTranspileSymbol = process as ProcessWithTransformHook;
8991

90-
if (processWithEsbuildSymbol[TESTPLANE_TRANSFORM_HOOK] || process.env.TS_ENABLE === "false") {
92+
if (processWithTranspileSymbol[TESTPLANE_TRANSFORM_HOOK] || process.env.TS_ENABLE === "false") {
9193
return;
9294
}
9395

9496
try {
95-
const revertTransformHook = addHook(
96-
(code, filename) => transformCode(code, { sourceFile: filename, sourceMaps: false, isSilent }),
97-
{
98-
exts: TRANSFORM_EXTENSIONS,
99-
matcher: filename => !filename.includes("node_modules"),
100-
ignoreNodeModules: false,
101-
},
102-
);
97+
const mkTransformCodeHook =
98+
(sourceMaps = false): Parameters<typeof addHook>[0] =>
99+
(code, sourceFile) =>
100+
transformCode(code, { sourceFile, sourceMaps, isSilent });
101+
102+
const transformCodeOptions: Parameters<typeof addHook>[1] = {
103+
exts: TRANSFORM_CODE_EXTENSIONS,
104+
ignoreNodeModules: true,
105+
};
106+
107+
let areSourceMapsEnabled = false;
108+
109+
let revertTransformHook = addHook(mkTransformCodeHook(), transformCodeOptions);
103110

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

116+
const enableSourceMaps = (): void => {
117+
if (areSourceMapsEnabled) {
118+
return;
119+
}
120+
121+
areSourceMapsEnabled = true;
122+
123+
revertTransformHook();
124+
125+
revertTransformHook = addHook(mkTransformCodeHook(true), transformCodeOptions);
126+
};
127+
109128
const revertAll = (): void => {
110129
revertTransformHook();
111130
revertAssetHook();
112-
delete processWithEsbuildSymbol[TESTPLANE_TRANSFORM_HOOK];
131+
delete processWithTranspileSymbol[TESTPLANE_TRANSFORM_HOOK];
113132
};
114133

115-
processWithEsbuildSymbol[TESTPLANE_TRANSFORM_HOOK] = { revert: revertAll };
134+
processWithTranspileSymbol[TESTPLANE_TRANSFORM_HOOK] = { revert: revertAll, enableSourceMaps };
116135
} catch (err) {
117136
logger.warn(`testplane: an error occurred while trying to register transform hook.`, err);
118137
}
119138
};
139+
140+
export const enableSourceMaps = (): void => {
141+
const processWithTranspileSymbol = process as ProcessWithTransformHook;
142+
143+
if (!processWithTranspileSymbol[TESTPLANE_TRANSFORM_HOOK]) {
144+
return;
145+
}
146+
147+
processWithTranspileSymbol[TESTPLANE_TRANSFORM_HOOK].enableSourceMaps();
148+
};

test/src/test-reader/mocha-reader/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ describe("test-reader/mocha-reader", () => {
1717
let MochaConstructorStub;
1818
let SourceMapSupportStub;
1919
let getMethodsByInterfaceStub;
20+
let enableSourceMapsStub;
2021
let readFiles;
2122

2223
const mkMochaSuiteStub_ = () => {
@@ -47,11 +48,13 @@ describe("test-reader/mocha-reader", () => {
4748
install: sinon.stub(),
4849
};
4950
getMethodsByInterfaceStub = sinon.stub().returns({ suiteMethods: [], testMethods: [] });
51+
enableSourceMapsStub = sinon.stub();
5052

5153
readFiles = proxyquire("src/test-reader/mocha-reader", {
5254
mocha: MochaConstructorStub,
5355
"@cspotcode/source-map-support": SourceMapSupportStub,
5456
"./utils": { getMethodsByInterface: getMethodsByInterfaceStub },
57+
"../../utils/typescript": { enableSourceMaps: enableSourceMapsStub },
5558
}).readFiles;
5659

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

268+
it("should enable testplane source maps before installing 'source-map-support'", async () => {
269+
await readFiles_({ config: { ui: "bdd" }, runnableOpts: { saveLocations: true } });
270+
271+
assert.calledOnce(enableSourceMapsStub);
272+
assert.callOrder(enableSourceMapsStub, SourceMapSupportStub.install);
273+
});
274+
265275
it("should set 'hookRequire' option on install source-map-support", async () => {
266276
await readFiles_({ config: { ui: "bdd" }, runnableOpts: { saveLocations: true } });
267277

test/src/utils/typescript.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ describe("utils/typescript", () => {
77

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

1213
beforeEach(() => {
13-
addHookStub = sinon.stub();
14+
revertHookStub = sinon.stub();
15+
addHookStub = sinon.stub().returns(revertHookStub);
1416
ts = proxyquire.noCallThru().load("src/utils/typescript", {
1517
pirates: {
1618
addHook: addHookStub,
@@ -27,7 +29,7 @@ describe("utils/typescript", () => {
2729
it("should add pirates hook", () => {
2830
ts.registerTransformHook();
2931

30-
assert.calledOnce(addHookStub);
32+
assert.calledTwice(addHookStub);
3133
});
3234

3335
it("should not call register if typescript was already installed", () => {
@@ -48,4 +50,22 @@ describe("utils/typescript", () => {
4850
process.env.TS_ENABLE = "undefined";
4951
});
5052
});
53+
54+
describe("enableSourceMaps", () => {
55+
it("should not do anything if transform hook is not registered", () => {
56+
ts.enableSourceMaps();
57+
58+
assert.notCalled(addHookStub);
59+
});
60+
61+
it("should re-register transform hook with source maps", () => {
62+
ts.registerTransformHook();
63+
const addHookPrevCallCount = addHookStub.callCount;
64+
65+
ts.enableSourceMaps();
66+
67+
assert.calledOnce(revertHookStub);
68+
assert.equal(addHookStub.callCount, addHookPrevCallCount + 1);
69+
});
70+
});
5171
});

0 commit comments

Comments
 (0)