New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

find-test-names

Package Overview
Dependencies
Maintainers
1
Versions
85
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

find-test-names - npm Package Compare versions

Comparing version 1.1.1 to 1.2.0

2

package.json
{
"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 [![ci](https://github.com/bahmutov/find-test-names/actions/workflows/ci.yml/badge.svg?branch=main)](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 @@ }

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc