Skip to content

Commit fb071ca

Browse files
committed
add typescript support
1 parent 627fdea commit fb071ca

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

src/helpers/unitTests.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ let functionList = {},
2929
testsGenerated = [],
3030
errors = [],
3131
ignoreFolders = ['node_modules', 'pythagora_tests'],
32-
processExtensions = ['.js'],
32+
processExtensions = ['.js', '.ts'],
3333
ignoreErrors = ['BABEL_PARSER_SYNTAX_ERROR'],
3434
force
3535
;
@@ -118,6 +118,7 @@ async function reformatDataForPythagoraAPI(funcData, filePath, testFilePath) {
118118

119119
async function createTests(filePath, prefix, funcToTest) {
120120
try {
121+
let extension = path.extname(filePath);
121122
let ast = await getAstFromFilePath(filePath);
122123
const fileIndex = folderStructureTree.findIndex(item => item.absolutePath === filePath);
123124

@@ -142,16 +143,22 @@ async function createTests(filePath, prefix, funcToTest) {
142143
}
143144
});
144145

145-
for (const [i, funcData] of foundFunctions.entries()) {
146-
let isLast = foundFunctions.indexOf(funcData) === foundFunctions.length - 1;
146+
const uniqueFoundFunctions = foundFunctions.filter((item, index, self) =>
147+
index === self.findIndex((t) => (
148+
t.functionName === item.functionName && t.functionCode === item.functionCode
149+
))
150+
);
151+
152+
for (const [i, funcData] of uniqueFoundFunctions.entries()) {
153+
let isLast = uniqueFoundFunctions.indexOf(funcData) === uniqueFoundFunctions.length - 1;
147154
let indexToPush = fileIndex + 1 + i;
148155
folderStructureTree.splice(
149156
indexToPush,
150157
0,
151158
getFolderTreeItem(
152159
prefix,
153160
isLast,
154-
`${funcData.functionName}.test.js`,
161+
`${funcData.functionName}.test${extension}`,
155162
filePath + ':' + funcData.functionName
156163
)
157164
);
@@ -187,7 +194,7 @@ async function createTests(filePath, prefix, funcToTest) {
187194
}
188195
}
189196

190-
if (foundFunctions.length > 0) {
197+
if (uniqueFoundFunctions.length > 0) {
191198
folderStructureTree[fileIndex].line = `${green+bold}${folderStructureTree[fileIndex].line}${reset}`;
192199
}
193200

@@ -198,12 +205,13 @@ async function createTests(filePath, prefix, funcToTest) {
198205

199206
async function saveTests(filePath, name, testData) {
200207
let dir = getTestFolderPath(filePath, rootPath);
208+
let extension = path.extname(filePath);
201209

202210
if (!await checkDirectoryExists(dir)) {
203211
fs.mkdirSync(dir, { recursive: true });
204212
}
205213

206-
let testPath = path.join(dir, `/${name}.test.js`);
214+
let testPath = path.join(dir, `/${name}.test${extension}`);
207215
fs.writeFileSync(testPath, testData);
208216
return testPath;
209217
}
@@ -273,7 +281,7 @@ async function generateTestsForDirectory(args) {
273281
screen.destroy();
274282
process.stdout.write('\x1B[2J\x1B[0f');
275283
if (errors.length) {
276-
let errLogPath = `${path.resolve(PYTHAGORA_UNIT_DIR, 'errorLogs.log')}`
284+
let errLogPath = `${path.resolve(PYTHAGORA_UNIT_DIR, 'errorLogs.log')}`;
277285
fs.writeFileSync(errLogPath, JSON.stringify(errors, null, 2));
278286
console.error('There were errors encountered while trying to generate unit tests.\n');
279287
console.error(`You can find logs here: ${errLogPath}`);

src/scripts/unit.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
const {generateTestsForDirectory} = require("../helpers/unitTests");
22
let args = require('../utils/getArgs.js');
3+
const {setUpPythagoraDirs} = require("../helpers/starting.js");
34

45
if (!args.path && !args.func) {
56
console.log("Please provide a path or a function name to test");
67
process.exit(1);
78
}
89
if (!args.path) args.path = process.cwd();
10+
setUpPythagoraDirs();
911
generateTestsForDirectory(args);

src/utils/code.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ async function getAstFromFilePath(filePath) {
4646
const ast = babelParser.parse(data, {
4747
sourceType: "module", // Consider input as ECMAScript module
4848
locations: true,
49-
plugins: ["jsx", "objectRestSpread"] // Enable JSX and object rest/spread syntax
49+
plugins: ["jsx", "objectRestSpread", "typescript"] // Enable JSX, typescript and object rest/spread syntax
5050
});
5151

5252
return ast;
@@ -217,7 +217,7 @@ function processAst(ast, cb) {
217217
left.object.property.name === 'exports') {
218218
if (expression.right.type === 'Identifier') {
219219
// module.exports.func1 = func1
220-
return cb(left.property.name, null, 'exportObj');
220+
return cb(left.property.name, path, 'exportObj');
221221
} else if (expression.right.type === 'FunctionExpression') {
222222
// module.exports.funcName = function() { ... }
223223
// module.exports = function() { ... }
@@ -230,7 +230,7 @@ function processAst(ast, cb) {
230230
left.property.name === 'exports') {
231231
if (expression.right.type === 'Identifier') {
232232
// module.exports = func1
233-
return cb(expression.right.name, null, 'exportFn');
233+
return cb(expression.right.name, path, 'exportFn');
234234
} else if (expression.right.type === 'FunctionExpression') {
235235
let funcName;
236236
if (expression.right.id) {
@@ -246,7 +246,7 @@ function processAst(ast, cb) {
246246
expression.right.properties.forEach(prop => {
247247
if (prop.type === 'ObjectProperty') {
248248
// module.exports = { func1 };
249-
return cb(prop.key.name, null, 'exportObj');
249+
return cb(prop.key.name, path, 'exportObj');
250250
}
251251
});
252252
}
@@ -256,7 +256,7 @@ function processAst(ast, cb) {
256256
left.object.name === 'exports') {
257257
// exports.func1 = function() { ... }
258258
// exports.func1 = func1
259-
return cb(left.property.name, null, 'exportObj');
259+
return cb(left.property.name, path, 'exportObj');
260260
}
261261
}
262262
}
@@ -268,37 +268,37 @@ function processAst(ast, cb) {
268268
// export default func1;
269269
// TODO export default function() { ... }
270270
// TODO cover anonimous functions - add "anon_" name
271-
return cb(declaration.id ? declaration.id.name : declaration.name, null, 'exportFn');
271+
return cb(declaration.id ? declaration.id.name : declaration.name, path, 'exportFn');
272272
} else if (declaration.type === 'ObjectExpression') {
273273
declaration.properties.forEach(prop => {
274274
if (prop.type === 'ObjectProperty') {
275275
// export default { func1: func }
276276
// export default { func1 }
277-
return cb(prop.key.name, null, 'exportObj');
277+
return cb(prop.key.name, path, 'exportObj');
278278
}
279279
});
280280
} else if (declaration.type === 'ClassDeclaration') {
281281
// export default class Class1 { ... }
282-
return cb(declaration.id ? declaration.id.name : declaration.name, null, 'exportFnDef');
282+
return cb(declaration.id ? declaration.id.name : declaration.name, path, 'exportFnDef');
283283
}
284284
} else if (path.isExportNamedDeclaration()) {
285285
if (path.node.declaration) {
286286
if (path.node.declaration.type === 'FunctionDeclaration') {
287287
// export function func1 () { ... }
288288
// export class Class1 () { ... }
289-
return cb(path.node.declaration.id.name, null, 'exportFnDef');
289+
return cb(path.node.declaration.id.name, path, 'exportObj');
290290
} else if (path.node.declaration.type === 'VariableDeclaration') {
291291
path.node.declaration.declarations.forEach(declaration => {
292-
return cb(declaration.id.name, null, 'exportFn');
292+
return cb(declaration.id.name, path, 'exportFn');
293293
});
294294
} else if (path.node.declaration.type === 'ClassDeclaration') {
295295
// export class Class1 { ... }
296-
return cb(path.node.declaration.id.name, null, 'exportFnDef');
296+
return cb(path.node.declaration.id.name, path, 'exportFnDef');
297297
}
298298
} else if (path.node.specifiers.length > 0) {
299299
path.node.specifiers.forEach(spec => {
300300
// export { func as func1 }
301-
return cb(spec.exported.name, null, 'exportObj');
301+
return cb(spec.exported.name, path, 'exportObj');
302302
});
303303
}
304304
}

0 commit comments

Comments
 (0)