20

Is it possible to get a list of suites/specs without running the actual tests w/ Jest.

For example, I have a test that looks like this:

describe('when blah blah blah', function () {
  it('returns foobar', function () { /* test code */ })
})

Ideally, I'd like to be able to just get a list of describe/it statements to quickly look at scenarios covered.

# stdout
when blah blah blah
  returns foobar

I think this is possible with rspec, using a dry-run flag. Is there any such option with Jest?

I took a look at the Jest CLI documentation and have done an issue search at the project's Github page, but could not find anything.

skyboyer
  • 22,209
  • 7
  • 57
  • 64
dev.kho
  • 219
  • 2
  • 6

3 Answers3

24

The switch --listTests returns a JSON array of the test files and then exits (instead of running the tests).

It might not be exactly what you're looking for though since it doesn't parse the describe/it functions, nor is the output very pretty.

https://facebook.github.io/jest/docs/en/cli.html#listtests

Oskar
  • 7,945
  • 5
  • 36
  • 44
7

Although question doesn't ask about how to get count of the all tests. I wanted to get this information. One hacky way to do this would be to use testNamePattern switch with pattern which doesn't match any tests.

This will skip and print the skipped tests which is what you want.

jest ./src --testNamePattern=<pattern-which-doesnt-match-any-tests>

Output of this looks like following

$ jest ./src --testNamePattern=adafafsafa

Test Suites: 409 skipped, 0 of 409 total
Tests:       2540 skipped, 2540 total
Snapshots:   0 total
Time:        74.082 s
Ran all test suites matching /.\/src/i with tests matching "adafafsafa".
✨  Done in 75.84s.
Vishwanath
  • 6,284
  • 4
  • 38
  • 57
0

You can use terminal and regex. For example: egrep '^\s*(describe|it)\(' PATH_TO_FILE.

What it means:

  • ^ asserts position at start of a line,
  • \s* matches any whitespace character between zero and unlimited times,
  • (describe|it) matches the word "describe" or "it" literally,
  • \( matches the character "(" one time.

In my case, it yields all lines with test signatures.

Frumda Grayforce
  • 119
  • 1
  • 13