find-cypress-specs 1.6.0 → 1.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -31,22 +31,26 @@ found 2 specs (4 tests, 1 pending)
31
31
 
32
32
  Where the tags are listed inside `[ ... ]` (see [cypress-grep](https://github.com/cypress-io/cypress-grep)) and the [pending tests](https://glebbahmutov.com/blog/cypress-test-statuses/) are marked with `⊙` character.
33
33
 
34
+ You can print the results in JSON format using `--json` or `-j` option.
35
+
34
36
  ## Test tags
35
37
 
36
- You can count tags attached to the individual tests using `--names --tags` arguments
38
+ You can count tags attached to the individual tests using `--tags` arguments
37
39
 
38
40
  ```
39
- $ npx find-cypress-specs --names --tags
40
- # prints the specs and tests and at the end prints the tags table
41
+ $ npx find-cypress-specs --tags
42
+ # prints the tags table sorted by tag
41
43
 
42
44
  Tag Tests
43
45
  ----- ----------
44
- @user 2
45
46
  @sign 1
47
+ @user 2
46
48
  ```
47
49
 
48
50
  Each tag count includes the tests that use the tag directly, and the _effective_ tags applied from the parent suites.
49
51
 
52
+ You can print the results in JSON format using `--json` or `-j` option.
53
+
50
54
  ## Details
51
55
 
52
56
  Cypress uses the resolved [configuration values](https://on.cypress.io/configuration) to find the spec files to run. It searches the `integrationFolder` for all patterns listed in `testFiles` and removes any files matching the `ignoreTestFiles` patterns.
@@ -57,6 +61,10 @@ You can see how Cypress finds the specs using `DEBUG=cypress:cli,cypress:server:
57
61
 
58
62
  Run the utility with environment variable `DEBUG=find-cypress-specs` to see the verbose logs
59
63
 
64
+ ## Videos
65
+
66
+ - [Use Ava Snapshots And Execa-wrap To Write End-to-End Tests For CLI Utilities](https://youtu.be/rsw17RqP0G0)
67
+
60
68
  ## Small print
61
69
 
62
70
  Author: Gleb Bahmutov <gleb.bahmutov@gmail.com> © 2022
package/bin/find.js CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  const arg = require('arg')
4
- const { getSpecs } = require('../src')
4
+ const { getSpecs, collectResults } = require('../src')
5
5
  const fs = require('fs')
6
6
  const pluralize = require('pluralize')
7
7
  const { getTestNames, formatTestList, countTags } = require('find-test-names')
@@ -10,16 +10,18 @@ const consoleTable = require('console.table')
10
10
  const args = arg({
11
11
  '--names': Boolean,
12
12
  '--tags': Boolean,
13
+ '--json': Boolean,
13
14
 
14
15
  // aliases
15
16
  '-n': '--names',
16
17
  '--name': '--names',
17
18
  '-t': '--tags',
18
19
  '--tag': '--tags',
20
+ '-j': '--json',
19
21
  })
20
22
 
21
23
  const specs = getSpecs()
22
- if (args['--names']) {
24
+ if (args['--names'] || args['--tags']) {
23
25
  if (!specs.length) {
24
26
  console.log('no specs found')
25
27
  } else {
@@ -30,7 +32,10 @@ if (args['--names']) {
30
32
  // counts the number of tests for each tag across all specs
31
33
  const tagTestCounts = {}
32
34
 
35
+ const jsonResults = {}
36
+
33
37
  specs.forEach((filename) => {
38
+ jsonResults[filename] = []
34
39
  const source = fs.readFileSync(filename, 'utf8')
35
40
  const result = getTestNames(source, true)
36
41
  // enable if need to debug the parsed test
@@ -40,18 +45,24 @@ if (args['--names']) {
40
45
  const testCount = pluralize('test', result.testNames.length, true)
41
46
  pendingTestsN += result.pendingTestCount
42
47
 
43
- if (result.pendingTestCount) {
44
- console.log(
45
- '%s (%s, %d pending)',
46
- filename,
47
- testCount,
48
- result.pendingTestCount,
49
- )
50
- } else {
51
- console.log('%s (%s)', filename, testCount)
48
+ if (args['--names']) {
49
+ if (args['--json']) {
50
+ collectResults(result.structure, jsonResults[filename])
51
+ } else {
52
+ if (result.pendingTestCount) {
53
+ console.log(
54
+ '%s (%s, %d pending)',
55
+ filename,
56
+ testCount,
57
+ result.pendingTestCount,
58
+ )
59
+ } else {
60
+ console.log('%s (%s)', filename, testCount)
61
+ }
62
+ console.log(formatTestList(result.structure))
63
+ console.log('')
64
+ }
52
65
  }
53
- console.log(formatTestList(result.structure))
54
- console.log('')
55
66
 
56
67
  if (args['--tags']) {
57
68
  const specTagCounts = countTags(result.structure)
@@ -65,29 +76,42 @@ if (args['--names']) {
65
76
  }
66
77
  })
67
78
 
68
- if (pendingTestsN) {
69
- console.log(
70
- 'found %s (%s, %d pending)',
71
- pluralize('spec', specs.length, true),
72
- pluralize('test', testsN, true),
73
- pendingTestsN,
74
- )
75
- } else {
76
- console.log(
77
- 'found %s (%s)',
78
- pluralize('spec', specs.length, true),
79
- pluralize('test', testsN, true),
80
- )
79
+ if (args['--names']) {
80
+ if (args['--json']) {
81
+ console.log(JSON.stringify(jsonResults, null, 2))
82
+ } else {
83
+ if (pendingTestsN) {
84
+ console.log(
85
+ 'found %s (%s, %d pending)',
86
+ pluralize('spec', specs.length, true),
87
+ pluralize('test', testsN, true),
88
+ pendingTestsN,
89
+ )
90
+ } else {
91
+ console.log(
92
+ 'found %s (%s)',
93
+ pluralize('spec', specs.length, true),
94
+ pluralize('test', testsN, true),
95
+ )
96
+ }
97
+ }
98
+ console.log('')
81
99
  }
82
- console.log('')
83
100
 
84
101
  if (args['--tags']) {
85
- const table = consoleTable.getTable(
86
- ['Tag', 'Tests'],
87
- Object.entries(tagTestCounts),
88
- )
89
- console.log(table)
90
- console.log('')
102
+ const tagEntries = Object.entries(tagTestCounts)
103
+ const sortedTagEntries = tagEntries.sort((a, b) => {
104
+ // every entry is [tag, count], so compare the tags
105
+ return a[0].localeCompare(b[0])
106
+ })
107
+ if (args['--json']) {
108
+ // assemble a json object with the tag counts
109
+ const tagResults = Object.fromEntries(sortedTagEntries)
110
+ console.log(JSON.stringify(tagResults, null, 2))
111
+ } else {
112
+ const table = consoleTable.getTable(['Tag', 'Tests'], sortedTagEntries)
113
+ console.log(table)
114
+ }
91
115
  }
92
116
  }
93
117
  } else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "find-cypress-specs",
3
- "version": "1.6.0",
3
+ "version": "1.10.0",
4
4
  "description": "Find Cypress spec files using the config settings",
5
5
  "main": "src",
6
6
  "files": [
@@ -15,7 +15,11 @@
15
15
  "cy:run": "DEBUG=cypress:cli,cypress:server:specs cypress run",
16
16
  "demo": "DEBUG=find-cypress-specs node ./bin/find",
17
17
  "demo-names": "node ./bin/find --names",
18
- "demo-tags": "node ./bin/find --names --tags",
18
+ "demo-tags": "node ./bin/find --tags",
19
+ "demo-tags-json": "node ./bin/find --tags --json",
20
+ "demo-names-and-tags": "node ./bin/find --names --tags",
21
+ "demo-names-and-tags-json": "node ./bin/find --names --tags --json",
22
+ "demo-names-json": "node ./bin/find --names --json",
19
23
  "semantic-release": "semantic-release"
20
24
  },
21
25
  "repository": {
@@ -34,6 +38,7 @@
34
38
  "devDependencies": {
35
39
  "ava": "^4.0.0",
36
40
  "cypress": "^9.2.0",
41
+ "execa-wrap": "^1.4.0",
37
42
  "prettier": "^2.5.1",
38
43
  "semantic-release": "^18.0.1"
39
44
  },
@@ -41,7 +46,7 @@
41
46
  "arg": "^5.0.1",
42
47
  "console.table": "^0.10.0",
43
48
  "debug": "^4.3.3",
44
- "find-test-names": "^1.14.0",
49
+ "find-test-names": "^1.14.1",
45
50
  "globby": "^11.0.4",
46
51
  "minimatch": "^3.0.4",
47
52
  "pluralize": "^8.0.0"
package/src/index.js CHANGED
@@ -72,9 +72,37 @@ function getSpecs() {
72
72
  return specs
73
73
  }
74
74
 
75
+ function collectResults(structure, results) {
76
+ structure.forEach((t) => {
77
+ const info = {
78
+ name: t.name,
79
+ type: t.type,
80
+ tags: t.tags,
81
+ }
82
+ if (t.pending) {
83
+ info.pending = t.pending
84
+ }
85
+ results.push(info)
86
+ if (t.type === 'suite') {
87
+ if (t.suites && t.suites.length) {
88
+ // skip empty nested suites
89
+ info.suites = []
90
+ collectResults(t.suites, info.suites)
91
+ }
92
+
93
+ if (t.tests && t.tests.length) {
94
+ // skip empty nested tests
95
+ info.tests = []
96
+ collectResults(t.tests, info.tests)
97
+ }
98
+ }
99
+ })
100
+ }
101
+
75
102
  module.exports = {
76
103
  getSpecs,
77
104
  // individual utilities
78
105
  getConfig,
79
106
  findCypressSpecs,
107
+ collectResults,
80
108
  }