Socket
Socket
Sign inDemoInstall

mocha-list-tests

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mocha-list-tests - npm Package Compare versions

Comparing version 1.0.6 to 1.1.0

141

mocha-list-tests.js

@@ -93,3 +93,3 @@ // -----------------------------------------------------------------------------

// -----------------------------------------------------------------------------
function addTestRouteToTree(testRoute, name) {
function addTestRouteToTree(testRoute, testType, name) {
let fileNameAndLine = 'unknown:0'

@@ -122,6 +122,12 @@

if (!(current in root) || typeof root[current] == 'string') {
if (i + 1 == newTestRoute.length) root[current] = fileNameAndLine
if (i + 1 == newTestRoute.length)
root[current] = {
type: testType,
file: fileNameAndLine.split(':')[0],
line: parseInt(fileNameAndLine.split(':')[1], 10),
children: {},
}
else root[current] = {}
}
root = root[current]
root = root[current].children
}

@@ -140,15 +146,17 @@ }

// -----------------------------------------------------------------------------
function captureDescribeFunctions(suiteName, suite) {
addTestRouteToTree(testRoute, suiteName)
function captureDescribeFunctions(testType) {
return function captureDescribe(suiteName, suite) {
addTestRouteToTree(testRoute, testType, suiteName)
testRoute.push(suiteName)
suites[testRoute.join('.')] = true
testRoute.push(suiteName)
suites[testRoute.join('.')] = true
// define methods that can be used inside describe using this
suite.apply({
timeout: () => {},
slow: () => {},
retries: () => {},
})
testRoute.pop()
// define methods that can be used inside describe using this
suite.apply({
timeout: () => {},
slow: () => {},
retries: () => {},
})
testRoute.pop()
}
}

@@ -162,6 +170,8 @@

// -----------------------------------------------------------------------------
function captureItFunctions(testName, ignoreFunction) {
addTestRouteToTree(testRoute, testName)
function captureItFunctions(testType) {
return function captureIt(testName, ignoreFunction) {
addTestRouteToTree(testRoute, testType, testName)
tests[(testRoute.join('.') + '.' + testName).replace(/^\.|\.$/, '')] = true
tests[(testRoute.join('.') + '.' + testName).replace(/^\.|\.$/, '')] = true
}
}

@@ -178,6 +188,52 @@

return function capture(ignoreFunction) {
addTestRouteToTree(testRoute, ':' + name)
addTestRouteToTree(testRoute, name, ':' + name)
}
}
// -----------------------------------------------------------------------------
// cleanEmptyChildren
//
// Removes all nodes whose children are empty
// -----------------------------------------------------------------------------
function cleanEmptyChildren(extendedTree) {
for (const [key, subtree] of Object.entries(extendedTree)) {
if (typeof subtree !== 'object') continue
if (
subtree.hasOwnProperty('children') &&
Object.keys(subtree.children).length === 0
) {
delete subtree.children
} else {
cleanEmptyChildren(subtree)
}
}
}
// -----------------------------------------------------------------------------
// buildSimpleTree
//
// Creates a simple tree where leaf nodes contain the file:line of the test
// -----------------------------------------------------------------------------
function buildSimpleTree(extendedTree) {
if (
!extendedTree.hasOwnProperty('children') &&
extendedTree.hasOwnProperty('line')
) {
return `${extendedTree.file}:${extendedTree.line}`
}
const simpleTree = {}
for (const [key, subtree] of Object.entries(extendedTree)) {
if (subtree.hasOwnProperty('children')) {
simpleTree[key] = buildSimpleTree(subtree.children)
} else {
simpleTree[key] = `${subtree.file}:${subtree.line}`
}
}
return simpleTree
}
// -----------------------------------------------------------------------------
// findSuitesAndTests

@@ -193,3 +249,32 @@ //

// {
// "suites" : [
// "suite-1",
// "suite-2",
// "suite-2.suite-2.1",
// ...
// ],
//
// "tests": [
// "test-0",
// "suite-1.test-1",
// "suite-2.suite-2.1.test-2.1.1",
// ...
// ],
// "tree": {
// "test-0": {
// "type": "it.skip",
// "file": "testFile.js",
// "line": 14
// },
// "suite-1": {
// "type": "describe",
// "file": "testFile.js",
// "line": 23,
// "test-1": {
// "type": "it",
// "file": "testFile.js",
// "line": 27
// }
// },
// }
// }

@@ -203,9 +288,9 @@ // -----------------------------------------------------------------------------

// HOOK: describe/it function hooks
global.describe = captureDescribeFunctions
global.it = captureItFunctions
global.describe = captureDescribeFunctions('describe')
global.it = captureItFunctions('it')
global.describe.skip = global.describe
global.describe.only = global.describe
global.it.skip = global.it
global.it.only = global.it
global.describe.skip = captureDescribeFunctions('describe.skip')
global.describe.only = captureDescribeFunctions('describe.only')
global.it.skip = captureItFunctions('it.skip')
global.it.only = captureItFunctions('it.only')

@@ -228,6 +313,12 @@ global.before = captureHookFunctions('before')

cleanEmptyChildren(tree)
// build a simple tree out of the current extended tree
const simpleTree = buildSimpleTree(tree)
return {
suites: Object.keys(suites),
tests: Object.keys(tests),
tree: tree,
tree: simpleTree,
extended_tree: tree,
}

@@ -234,0 +325,0 @@ }

2

package.json
{
"name": "mocha-list-tests",
"version": "1.0.6",
"version": "1.1.0",
"description": "List mocha suites and tests without running anything",

@@ -5,0 +5,0 @@ "main": "mocha-list-tests.js",

@@ -28,2 +28,3 @@ # mocha-list-tests

console.log (result.tree)
console.log (result.extended_tree)
```

@@ -39,2 +40,78 @@

Output will be a valid json, although there is some data in this example that
has been removed for simplification purposes:
```json
{
"suites": [
"suite-1",
"suite-2",
"suite-2.suite-2.1",
"suite-3",
...
],
"tests": [
"test-0",
"suite-1.test-1",
"suite-2.suite-2.1.test-2.1.1",
"suite-3.test-3.1",
...
],
"tree": {
"test-0": "test/example.js:3",
"suite-1": {
"test-1": "test/example.js:8"
},
"suite-2": {
"suite-2.1": {
"test-2.1.1": "test/example.js:15"
}
},
...
},
"extended_tree": {
"test-0": {
"type": "it",
"file": "test/example.js",
"line": 3
},
"suite-1": {
"type": "describe",
"file": "test/example.js",
"line": 7,
"children": {
"test-1": {
"type": "it",
"file": "test/example.js",
"line": 8
}
}
},
"suite-2": {
"type": "describe",
"file": "test/example.js",
"line": 13,
"children": {
"suite-2.1": {
"type": "describe",
"file": "test/example.js",
"line": 14,
"children": {
"test-2.1.1": {
"type": "it",
"file": "test/example.js",
"line": 15
}
}
}
}
},
...
}
}
```
## License

@@ -41,0 +118,0 @@

Sorry, the diff of this file is not supported yet

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