find-test-names
Advanced tools
Comparing version 1.15.1 to 1.16.0
{ | ||
"name": "find-test-names", | ||
"version": "1.15.1", | ||
"version": "1.16.0", | ||
"description": "Given a Mocha / Cypress spec file, returns the list of suite and test names", | ||
@@ -5,0 +5,0 @@ "main": "src", |
@@ -46,2 +46,24 @@ # 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) | ||
### setEffectiveTags | ||
Often, you want to have each test and see which tags it has and what parent tags apply to it. You can compute for each test a list of _effective_ tags and set it for each test. | ||
```js | ||
// example spec code | ||
describe('parent', { tags: '@user' }, () => { | ||
describe('parent', { tags: '@auth' }, () => { | ||
it('works a', { tags: '@one' }, () => {}) | ||
it('works b', () => {}) | ||
}) | ||
}) | ||
``` | ||
```js | ||
const { getTestNames, setEffectiveTags } = require('find-test-names') | ||
const result = getTestNames(source, true) | ||
setEffectiveTags(result.structure) | ||
``` | ||
If you traverse the `result.structure`, the test "works a" will have the `effectiveTags` list with `@user, @auth, @one`, and the test "works b" will have the `effectiveTags` list with `@user, @auth, @one`. | ||
### Bin | ||
@@ -48,0 +70,0 @@ |
@@ -435,2 +435,24 @@ const babel = require('@babel/parser') | ||
/** | ||
* Visits each test and counts its tags and its parents' tags | ||
* to compute the "effective" tags list. | ||
*/ | ||
function setEffectiveTags(structure) { | ||
setParentSuite(structure) | ||
visitEachTest(structure, (test, parentSuite) => { | ||
// normalize the tags to be an array of strings | ||
const ownTags = [].concat(test.tags || []) | ||
// also consider the effective tags by traveling up | ||
// the parent chain of suites | ||
const suiteTags = collectSuiteTagsUp(parentSuite) | ||
const allTags = [...ownTags, ...suiteTags] | ||
const uniqueTags = [...new Set(allTags)] | ||
test.effectiveTags = uniqueTags.sort() | ||
}) | ||
return structure | ||
} | ||
function setParentSuite(structure) { | ||
@@ -588,2 +610,3 @@ visitEachNode(structure, (test, parentSuite) => { | ||
setParentSuite, | ||
setEffectiveTags, | ||
} |
26550
669
173