Skip to content

Commit

Permalink
feat: add verbose specs mode
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Oct 3, 2023
1 parent c129ace commit 8a1a5b8
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ jobs:
- name: Demo finding test ids in specs
run: npm run demo:specs

- name: Demo finding test ids in specs and printing verbose info
run: npm run demo:specs -- --verbose

- name: Demo warning about untested ids
run: npm run demo:warn

Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ If you want to pick both JS and TS tests, use glob syntax. For example, let's gr
--specs 'tests/fixtures/*.cy.{js,ts}'
```

To produce verbose output showing specs for each test id, add argument `--verbose`

### Warn on untested ids

If you specify both sources and specs, then it will find all test ids used in the source files NOT used by the specs and list them one at a time
Expand Down Expand Up @@ -76,19 +78,19 @@ If running on GitHub Actions, use `--set-gha-outputs` to set the list of detecte
### Find specs that use particular test ids

```
$ npx find-ids --specs ... --command ...
$ npx find-ids --specs ... --command ... --test-ids one,two
```

For example, to find all test ids used in `.cy.js` and `.cy.ts` files and that use custom command `cy.getBy`
For example, to find all specs ending in `.cy.js` and `.cy.ts` files and that use custom command `cy.getBy` and use test id `greeting`

```
$ npx find-ids --specs 'cypress/e2e/**/*.cy.{js,ts}' --command getBy
$ npx find-ids --specs 'cypress/e2e/**/*.cy.{js,ts}' --command getBy --test-ids greeting
```

You can output detailed information showing each spec using a test id by adding parameter `--verbose`

```
$ npx find-ids --specs 'cypress/e2e/**/*.cy.{js,ts}' --command getBy --verbose
$ npx find-ids --specs 'cypress/e2e/**/*.cy.{js,ts}' --command getBy --test-ids greeting --verbose
```

## Debugging
Expand Down
32 changes: 25 additions & 7 deletions bin/find-ids.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,13 @@ if (specsForTestIdsMode) {
testIds.forEach((testId) => {
if (testIdToSpecs[testId].length) {
console.log(
'%s used in %d spec(s)',
'"%s" used in %d spec(s)',
testId,
testIdToSpecs[testId].length,
)
console.log(' %s', testIdToSpecs[testId].join(', '))
} else {
console.log('%s not found in any of the specs', testId)
console.log('"%s" not found in any of the specs', testId)
}
})
} else {
Expand Down Expand Up @@ -338,7 +338,10 @@ if (specsForTestIdsMode) {
const commands = args['--command'].split(',').filter(Boolean)
options.commands.push(...commands)
}
const testIds = findTestQueriesInFiles(specFiles, options)
const { testIds, testIdToFilenames } = findTestQueriesInFiles(
specFiles,
options,
)
debug(
'found %d test ids across %d specs',
testIds.length,
Expand All @@ -348,10 +351,25 @@ if (specsForTestIdsMode) {
console.log('Could not find any test ids in %d specs', specFiles.length)
} else {
if (!warnMode) {
// will report test ids later
testIds.forEach((testId) => {
console.log(testId)
})
if (verbose) {
Object.keys(testIdToFilenames).forEach((testId) => {
if (testIdToFilenames[testId].length) {
console.log(
'"%s" used in %d spec(s)',
testId,
testIdToFilenames[testId].length,
)
console.log(' %s', testIdToFilenames[testId].join(', '))
} else {
console.log('"%s" not found in any of the specs', testId)
}
})
} else {
// will report test ids later
testIds.forEach((testId) => {
console.log(testId)
})
}
}
testIdsInSpecs.push(...testIds)
}
Expand Down
14 changes: 12 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,24 @@ function findTestQueriesInFile(filename, options = {}) {
*/
function findTestQueriesInFiles(filenames, options = {}) {
const names = new Set()

// each test id is a key, and the value
// is a list of specs that use that test id
const testIdToFilenames = {}

filenames.forEach((filename) => {
const ids = findTestQueriesInFile(filename, options)
ids.forEach((testId) => {
names.add(testId)
if (!testIdToFilenames[testId]) {
testIdToFilenames[testId] = [filename]
} else {
testIdToFilenames[testId].push(filename)
}
})
})
const ids = [...names].sort()
return ids
const testIds = [...names].sort()
return { testIds, testIdToFilenames }
}

module.exports = {
Expand Down

0 comments on commit 8a1a5b8

Please sign in to comment.