Skip to content

Commit aa209e7

Browse files
Keep imports/requires statements for relatedFunctions source code
1 parent 34bf8b1 commit aa209e7

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

src/helpers/unitTestsExpand.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,11 @@ async function createAdditionalTests(filePath, prefix) {
100100

101101
sortFolderTree(folderStructureTree);
102102

103-
const relatedTestCode = getRelatedTestImports(ast, filePath, functionList);
103+
const relatedCode = getRelatedTestImports(ast, filePath, functionList);
104104
const formattedData = reformatDataForPythagoraAPI(
105105
filePath,
106106
testCode,
107-
relatedTestCode,
107+
relatedCode,
108108
syntaxType
109109
);
110110

src/utils/code.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ function getRelatedFunctions(node, ast, filePath, functionList) {
138138
}
139139

140140
let requiredPath = requiresFromFile.find(require => require.includes(funcName));
141+
const importPath = requiredPath;
141142
if (!requiredPath) {
142143
requiredPath = filePath;
143144
} else {
@@ -147,7 +148,8 @@ function getRelatedFunctions(node, ast, filePath, functionList) {
147148
let functionFromList = functionList[requiredPath + ':' + funcName];
148149
if (functionFromList) {
149150
relatedFunctions.push(_.extend(functionFromList, {
150-
fileName: requiredPath
151+
fileName: requiredPath,
152+
importPath
151153
}));
152154
}
153155
}
@@ -362,7 +364,7 @@ function collectTestRequires(node) {
362364
}
363365

364366
function getRelatedTestImports(ast, filePath, functionList) {
365-
let relatedFunctions = [];
367+
let relatedCode = [];
366368
let requiresFromFile = collectTestRequires(ast);
367369

368370
for (let fileImport in requiresFromFile) {
@@ -372,14 +374,27 @@ function getRelatedTestImports(ast, filePath, functionList) {
372374
_.forEach(requiresFromFile[fileImport].functionNames, (funcName) => {
373375
let functionFromList = functionList[requiredPath + ':' + funcName];
374376
if (functionFromList) {
375-
relatedFunctions.push(_.extend(functionFromList, {
377+
relatedCode.push(_.extend(functionFromList, {
376378
fileName: requiredPath
377379
}));
378380
}
379381
})
380382
}
381383

382-
return relatedFunctions;
384+
for (let relCode of relatedCode) {
385+
let relatedCodeImports = '';
386+
for (let func of relCode.relatedFunctions) {
387+
if (func.importPath) {
388+
relatedCodeImports += `${func.importPath}\n`;
389+
}
390+
}
391+
392+
if (relatedCodeImports) {
393+
relCode.code = `${relatedCodeImports}\n${relCode.code}`;
394+
}
395+
}
396+
397+
return relatedCode;
383398
}
384399

385400
module.exports = {

0 commit comments

Comments
 (0)