find-test-names
Advanced tools
Comparing version 1.17.2 to 1.18.0
{ | ||
"name": "find-test-names", | ||
"version": "1.17.2", | ||
"version": "1.18.0", | ||
"description": "Given a Mocha / Cypress spec file, returns the list of suite and test names", | ||
@@ -5,0 +5,0 @@ "main": "src", |
112
src/index.js
@@ -196,6 +196,13 @@ const babel = require('@babel/parser') | ||
*/ | ||
const getSuiteAncestorsForTest = (test, source, ancestors, nodes) => { | ||
const getSuiteAncestorsForTest = ( | ||
test, | ||
source, | ||
ancestors, | ||
nodes, | ||
fullSuiteNames, | ||
) => { | ||
let knownNode = false | ||
let suiteBranches = [] | ||
let prevSuite | ||
let describeFound = false | ||
let directParentSuite = null | ||
let suiteCount = 0 | ||
@@ -226,7 +233,5 @@ | ||
if (!describeFound) { | ||
// found this test's describe | ||
suite.tests.push(test) | ||
describeFound = true | ||
if (!directParentSuite) { | ||
// found this test's direct parent suite | ||
directParentSuite = suite | ||
} | ||
@@ -238,16 +243,27 @@ | ||
prevSuite = knownNode ? null : suite | ||
suiteBranches.unshift(suite) | ||
} | ||
} | ||
if (!knownNode) { | ||
// walked tree to the top | ||
if (describeFound) { | ||
return prevSuite | ||
} else { | ||
// top level test | ||
return test | ||
// walked tree to the top | ||
if (suiteBranches.length) { | ||
// Compute the full names of suite and test, i.e. prepend all parent suite names | ||
const suiteNameWithParentSuiteNames = computeParentSuiteNames( | ||
suiteBranches, | ||
fullSuiteNames, | ||
) | ||
test.fullName = `${suiteNameWithParentSuiteNames} ${test.name}` | ||
directParentSuite.tests.push(test) | ||
return { | ||
suite: !knownNode && prevSuite, // only return the suite if it hasn't been found before | ||
topLevelTest: false, | ||
} | ||
} else { | ||
// top level test | ||
test.fullName = test.name | ||
return { suite: null, topLevelTest: true } | ||
} | ||
return null | ||
} | ||
@@ -264,4 +280,10 @@ | ||
*/ | ||
const getOrphanSuiteAncestorsForSuite = (ancestors, source, nodes) => { | ||
const getOrphanSuiteAncestorsForSuite = ( | ||
ancestors, | ||
source, | ||
nodes, | ||
fullSuiteNames, | ||
) => { | ||
let prevSuite | ||
let suiteBranches = [] | ||
let knownNode = false | ||
@@ -295,2 +317,3 @@ let suiteCount = 0 | ||
suite.suiteCount += suiteCount | ||
suiteBranches.unshift(suite) | ||
} else { | ||
@@ -308,2 +331,3 @@ const { suite } = getDescribe(ancestor, source, skip) | ||
prevSuite = knownNode ? null : suite | ||
suiteBranches.unshift(suite) | ||
} | ||
@@ -313,2 +337,4 @@ } | ||
computeParentSuiteNames(suiteBranches, fullSuiteNames) | ||
if (!knownNode) { | ||
@@ -322,2 +348,18 @@ // walked tree to the top and found new suite(s) | ||
/** | ||
* Compute the full names of suites in an array of branches, i.e. prepend all parent suite names | ||
*/ | ||
function computeParentSuiteNames(suiteBranches, fullSuiteNames) { | ||
let suiteNameWithParentSuiteNames = '' | ||
suiteBranches.forEach((suite) => { | ||
suite.fullName = `${suiteNameWithParentSuiteNames} ${suite.name}`.trim() | ||
fullSuiteNames.add(suite.fullName) | ||
suiteNameWithParentSuiteNames = suite.fullName | ||
}) | ||
return suiteNameWithParentSuiteNames | ||
} | ||
function countPendingTests(suite) { | ||
@@ -523,2 +565,6 @@ if (!suite.type === 'suite') { | ||
const testNames = [] | ||
// suite names with parent suite names prepended | ||
const fullSuiteNames = new Set() | ||
// test names with parent suite names prepended | ||
const fullTestNames = [] | ||
// mixed entries for describe and tests | ||
@@ -547,2 +593,3 @@ // each entry has name and possibly a list of tags | ||
nodes, | ||
fullSuiteNames, | ||
) | ||
@@ -565,2 +612,3 @@ | ||
nodes, | ||
fullSuiteNames, | ||
) | ||
@@ -579,3 +627,3 @@ | ||
const suiteOrTest = getSuiteAncestorsForTest( | ||
const { suite, topLevelTest } = getSuiteAncestorsForTest( | ||
test, | ||
@@ -585,6 +633,9 @@ source, | ||
nodes, | ||
fullSuiteNames, | ||
) | ||
if (suiteOrTest) { | ||
structure.push(suiteOrTest) | ||
if (suite) { | ||
structure.push(suite) | ||
} else if (topLevelTest) { | ||
structure.push(test) | ||
} | ||
@@ -594,3 +645,5 @@ | ||
testNames.push(testInfo.name) | ||
fullTestNames.push(test.fullName) | ||
} | ||
tests.push(testInfo) | ||
@@ -601,3 +654,3 @@ } else if (isItSkip(node)) { | ||
const suiteOrTest = getSuiteAncestorsForTest( | ||
const { suite, topLevelTest } = getSuiteAncestorsForTest( | ||
test, | ||
@@ -607,9 +660,16 @@ source, | ||
nodes, | ||
fullSuiteNames, | ||
) | ||
if (suiteOrTest) { | ||
structure.push(suiteOrTest) | ||
if (suite) { | ||
structure.push(suite) | ||
} else if (topLevelTest) { | ||
structure.push(test) | ||
} | ||
testNames.push(testInfo.name) | ||
if (typeof testInfo.name !== 'undefined') { | ||
testNames.push(testInfo.name) | ||
fullTestNames.push(test.fullName) | ||
} | ||
tests.push(testInfo) | ||
@@ -624,2 +684,4 @@ } | ||
const sortedTestNames = testNames.sort() | ||
const sortedFullTestNames = [...fullTestNames].sort() | ||
const sortedFullSuiteNames = [...fullSuiteNames].sort() | ||
const result = { | ||
@@ -636,2 +698,4 @@ suiteNames: sortedSuiteNames, | ||
result.pendingTestCount = counts.pendingTestCount | ||
result.fullTestNames = sortedFullTestNames | ||
result.fullSuiteNames = sortedFullSuiteNames | ||
} | ||
@@ -638,0 +702,0 @@ |
29810
747
1