Skip to content

Commit

Permalink
fix: consider all tags from the parent suite
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Dec 27, 2023
1 parent 3f6ce8a commit 306f443
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,10 @@ function getTests(specs, options = {}) {
.filter(Boolean)
debug('filtering all tests by tag %o', splitTags)
pickTaggedTestsFrom(jsonResults, splitTags)
// debug(jsonResults)
debug('after picking tagged tests')
debug(jsonResults)
// recompute the number of tests
debug('adding counts to json results')
addCounts(jsonResults)
} else if (skipped) {
debug('leaving only skipped (pending) tests')
Expand Down
6 changes: 5 additions & 1 deletion src/tagged.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const debug = require('debug')('find-cypress-specs:tagged')
const { addCounts } = require('./count')

function arraysOverlap(a, b) {
Expand Down Expand Up @@ -36,7 +37,8 @@ function pickTaggedTests(tests, tag) {
return allTags && arraysOverlap(allTags, tags)
} else if (test.type === 'suite') {
const allTags = combineTags(test.tags, test.requiredTags)
if (allTags && arraysOverlap(test.tags, tags)) {
debug('allTags %o', allTags)
if (allTags && arraysOverlap(allTags, tags)) {
return true
}

Expand Down Expand Up @@ -87,8 +89,10 @@ function removeEmptyNodes(json) {
* Modifies the given object in place.
*/
function pickTaggedTestsFrom(json, tag) {
// console.log(JSON.stringify(json, null, 2))
Object.keys(json).forEach((filename) => {
const fileTests = json[filename].tests
debug('picking tagged tests from %s', filename)
pickTaggedTests(fileTests, tag)
})

Expand Down
2 changes: 2 additions & 0 deletions test-required-tags/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"scripts": {
"test": "cypress run",
"test-names": "DEBUG=find-cypress-specs,find-test-names node ../bin/find --names",
"test-parent1": "DEBUG=find-cypress-specs,find-test-names,find-cypress-specs:tagged DEBUG_DEPTH=5 node ../bin/find --names --tagged @parent1",
"test-parent2": "DEBUG=find-cypress-specs,find-test-names,find-cypress-specs:tagged DEBUG_DEPTH=5 node ../bin/find --names --tagged @parent2",
"count-all-tags": "DEBUG=find-cypress-specs,find-test-names DEBUG_DEPTH=10 node ../bin/find --tags"
},
"keywords": [],
Expand Down
92 changes: 92 additions & 0 deletions test/tagged.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,95 @@ test('applies multiple tags', (t) => {
// console.dir(result, { depth: null })
t.deepEqual(result, expected)
})

test('includes tests required by the parent required tag', (t) => {
const json = {
'cypress/integration/spec1.js': {
counts: {
tests: 2,
pending: 0,
},
tests: [
{
name: 'works',
type: 'test',
tags: ['user'],
requiredTags: ['@foo'],
},
{
name: 'does not work',
type: 'test',
requiredTags: ['@foo', '@bar'],
},
],
},
'cypress/integration/spec2.js': {
counts: {
tests: 1,
pending: 0,
},
tests: [
{
name: 'spec 2 works',
type: 'test',
tags: ['user'],
requiredTags: ['@foo'],
},
],
},
'cypress/integration/spec3.js': {
counts: {
tests: 2,
pending: 0,
},
tests: [
{
name: 'parent1',
type: 'suite',
requiredTags: ['@parent1'],
tests: [
{
name: 'child1',
type: 'test',
},
],
},
{
name: 'parent2',
type: 'suite',
tags: ['@parent2'],
tests: [
{
name: 'child2',
type: 'test',
},
],
},
],
},
}
const picked = pickTaggedTestsFrom(json, '@parent1')
// console.log(JSON.stringify(picked, null, 2))
// only spec 3 has a test whose parent has the required tag "@parent1"
t.deepEqual(picked, {
'cypress/integration/spec3.js': {
counts: {
tests: 1,
pending: 0,
},
tests: [
{
name: 'parent1',
type: 'suite',
requiredTags: ['@parent1'],
tests: [
{
name: 'child1',
type: 'test',
},
],
},
],
},
})
})

0 comments on commit 306f443

Please sign in to comment.