Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest shows only one test suite, even when multiple top-level calls to describe() exist in the test file

I'm testing my library with jest, and have a file with several describe calls at the first layer, but when I run "npm test", It reports: "Test Suites: 1 passed, 1 total" "Tests: 26 passed, 26 total"

Why isn't it reporting several test suites?

As far as I can tell, jest's describe function should create its own test suite, but somehow they're all being combined...

From the jest API, "describe(name, fn) creates a block that groups together several related tests in one test suite"

like image 947
Ben Chislett Avatar asked Dec 01 '18 22:12

Ben Chislett


People also ask

What is the difference between describe and test in Jest?

describe breaks your test suite into components. Depending on your test strategy, you might have a describe for each function in your class, each module of your plugin, or each user-facing piece of functionality. You can also nest describes to further subdivide the suite. it is where you perform individual tests.

Does Jest run test suites in parallel?

To speed-up your tests, Jest can run them in parallel. By default, Jest will parallelise tests that are in different files. IMPORTANT: Paralellising tests mean using different threads to run test-cases simultaneously.

What is a test suite in Jest?

Next we call a global Jest function describe(). In our TweetUtils-test. js file we're not just creating a single test, instead we're creating a suite of tests. A suite is a collection of tests that collectively test a bigger unit of functionality.

Does Jest run tests sequentially?

Per file, it will run all describe blocks first and then run tests in sequence, in the order it encountered them while executing the describe blocks. If you want to run files in sequence as well, run Jest with the --runInBand command line flag.


1 Answers

It seems that Jest does not really count every top-level call to describe() as a single Test Suite. There's even an open issue in Jest's GitHub repo reporting this behavior as a bug.

Indeed, as you described and as this minimal example on repl.it demonstrates, two top-level calls to describe():

const add = require('./add');
describe('add', () => {
  it('should add two numbers', () => {
    expect(add(1, 2)).toBe(3);
  });
});
describe('add again', () => {
  it('should add two numbers', () => {
    expect(add(1, 0)).toBe(1);
  });
});

Are counted as a single Test Suite:

Jest v22.1.2 node v7.4.0 linux/amd64

 PASS  ./add-test.js
  add
    ✓ should add two numbers (5ms)
  add again
    ✓ should add two numbers

Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        1.025s

Current Jest documentation appears to misleading when it states:

describe(name, fn) creates a block that groups together several related tests in one "test suite"

like image 64
urig Avatar answered Nov 15 '22 03:11

urig