find-test-names
Advanced tools
Comparing version 1.1.1 to 1.2.0
{ | ||
"name": "find-test-names", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "Given a Mocha / Cypress spec file, returns the list of suite and test names", | ||
@@ -5,0 +5,0 @@ "main": "src", |
@@ -18,5 +18,14 @@ # find-test-names [](https://github.com/bahmutov/find-test-names/actions/workflows/ci.yml) | ||
const result = getTestNames(specSourceCode) | ||
// { "suiteNames": [], "testNames": [] } | ||
// { "suiteNames": [], "testNames": [], "tests": [] } | ||
``` | ||
The `tests` is a list with each test and suite name, and optional list of tags. | ||
```js | ||
// spec.js | ||
it('works', {tags: ['@user']}, () => { ... }) | ||
// found test names | ||
// { tests: [{ name: 'works', tags: ['@user'] }] } | ||
``` | ||
### Bin | ||
@@ -23,0 +32,0 @@ |
@@ -10,2 +10,24 @@ const acorn = require('acorn') | ||
const getTags = (source, node) => { | ||
if (node.arguments[1].type === 'ObjectExpression') { | ||
// extract any possible tags | ||
const tags = node.arguments[1].properties.find((node) => { | ||
return node.key.name === 'tags' | ||
}) | ||
if (tags) { | ||
if (tags.value.type === 'ArrayExpression') { | ||
const tagsText = source.slice(tags.start, tags.end) | ||
return eval(tagsText) | ||
} else if (tags.value.type === 'Literal') { | ||
return [tags.value.value] | ||
} | ||
} | ||
} | ||
} | ||
/** | ||
* Returns all suite and test names found in the given JavaScript | ||
* source code (Mocha / Cypress syntax) | ||
* @param {string} source | ||
*/ | ||
function getTestNames(source) { | ||
@@ -22,2 +44,5 @@ // should we pass the ecma version here? | ||
const testNames = [] | ||
// mixed entries for describe and tests | ||
// each entry has name and possibly a list of tags | ||
const tests = [] | ||
@@ -27,5 +52,23 @@ walk.simple(AST, { | ||
if (isDescribe(node)) { | ||
suiteNames.push(node.arguments[0].value) | ||
const suiteInfo = { | ||
name: node.arguments[0].value, | ||
} | ||
const tags = getTags(source, node) | ||
if (Array.isArray(tags) && tags.length > 0) { | ||
suiteInfo.tags = tags | ||
} | ||
suiteNames.push(suiteInfo.name) | ||
tests.push(suiteInfo) | ||
} else if (isIt(node)) { | ||
testNames.push(node.arguments[0].value) | ||
const testInfo = { | ||
name: node.arguments[0].value, | ||
} | ||
const tags = getTags(source, node) | ||
if (Array.isArray(tags) && tags.length > 0) { | ||
testInfo.tags = tags | ||
} | ||
testNames.push(testInfo.name) | ||
tests.push(testInfo) | ||
} | ||
@@ -40,2 +83,3 @@ }, | ||
testNames: sortedTestNames, | ||
tests, | ||
} | ||
@@ -42,0 +86,0 @@ } |
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
6469
90
79
2
1