Skip to content

Commit 8d09a3a

Browse files
fix: tests location when calling list-tests cli command
1 parent 6fd3ea5 commit 8d09a3a

File tree

4 files changed

+75
-9
lines changed

4 files changed

+75
-9
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: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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,16 +87,16 @@ 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(
97+
let areSourceMapsEnabled = false;
98+
99+
let revertTransformHook = addHook(
96100
(code, filename) => transformCode(code, { sourceFile: filename, sourceMaps: false, isSilent }),
97101
{
98102
exts: TRANSFORM_EXTENSIONS,
@@ -106,14 +110,43 @@ export const registerTransformHook = (isSilent: boolean = false): void => {
106110
ignoreNodeModules: false,
107111
});
108112

113+
const enableSourceMaps = (): void => {
114+
if (areSourceMapsEnabled) {
115+
return;
116+
}
117+
118+
areSourceMapsEnabled = true;
119+
120+
revertTransformHook();
121+
122+
revertTransformHook = addHook(
123+
(code, filename) => transformCode(code, { sourceFile: filename, sourceMaps: true, isSilent }),
124+
{
125+
exts: TRANSFORM_EXTENSIONS,
126+
matcher: filename => !filename.includes("node_modules"),
127+
ignoreNodeModules: false,
128+
},
129+
);
130+
};
131+
109132
const revertAll = (): void => {
110133
revertTransformHook();
111134
revertAssetHook();
112-
delete processWithEsbuildSymbol[TESTPLANE_TRANSFORM_HOOK];
135+
delete processWithTranspileSymbol[TESTPLANE_TRANSFORM_HOOK];
113136
};
114137

115-
processWithEsbuildSymbol[TESTPLANE_TRANSFORM_HOOK] = { revert: revertAll };
138+
processWithTranspileSymbol[TESTPLANE_TRANSFORM_HOOK] = { revert: revertAll, enableSourceMaps };
116139
} catch (err) {
117140
logger.warn(`testplane: an error occurred while trying to register transform hook.`, err);
118141
}
119142
};
143+
144+
export const enableSourceMaps = (): void => {
145+
const processWithTranspileSymbol = process as ProcessWithTransformHook;
146+
147+
if (!processWithTranspileSymbol[TESTPLANE_TRANSFORM_HOOK]) {
148+
return;
149+
}
150+
151+
processWithTranspileSymbol[TESTPLANE_TRANSFORM_HOOK].enableSourceMaps();
152+
};

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)