Skip to content

Commit 6347939

Browse files
committed
add support for requires when expanding existing unit tests
1 parent 8a01d0a commit 6347939

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/utils/code.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,21 @@ function collectTestRequires(node) {
356356
if (s.local && s.local.name) requireData.functionNames.push(s.local.name)
357357
})
358358

359+
requires.push(requireData);
360+
}
361+
},
362+
CallExpression(path) {
363+
if (path.node.callee.name === 'require' && path.node.arguments && path.node.arguments.length > 0) {
364+
const requireData = {
365+
code: generator(path.node).code,
366+
functionNames: []
367+
}
368+
369+
// In case of a CommonJS require, the function name is usually the variable identifier of the parent node
370+
if (path.parentPath && path.parentPath.node.type === 'VariableDeclarator' && path.parentPath.node.id) {
371+
requireData.functionNames.push(path.parentPath.node.id.name)
372+
}
373+
359374
requires.push(requireData);
360375
}
361376
}

0 commit comments

Comments
 (0)