jest: Describe, bail and each don't work as written in the doc

šŸ› Bug Report

Describe doesnā€™t start a test suite, bail doesnā€™t stop the tests after a fail, each doesnā€™t format names correctly, and jest output is so verbose that it overflows my terminal.

To Reproduce

  • git clone https://github.com/Sharcoux/GoldMines.git
  • npm i
  • npm run test
  • Try to read the results of the tests

Expected behavior

describe

According to the documentation:

describe(name, fn) creates a block that groups together several related tests in one ā€œtest suiteā€.

This should start 2 tests suite:

    describe('test 1',() => {
      test('one space, one mine', () => {
        expect(findBestSpot(1, 1, 1, 1, [{"x":0,"y":0}])).toEqual({"coordinates":{"x":0,"y":0},"goldMines":1});
      });
    });
  
    describe('test 2',() => {
      test('one space, no mine', () => {
        expect(findBestSpot(1, 1, 1, 1, [])).toEqual({"coordinates":{"x":0,"y":0},"goldMines":0});
      });
    });

Actual result: => Test Suites: 1 failed, 1 total

bail

According to the documentation:

The bail config option can be used here to have Jest stop running tests after the first failure.

Running: npx jest -b

Actual result: => Tests: 23 failed, 10 passed, 33 total

How could 23 tests fail if jest did stop at first failure?

each

According to the documentation:

Generate unique test titles by positionally injecting parameters with printf formatting:

So this testā€™s name should be ā€˜nameā€™, but instead, itā€™s name is ā€˜1ā€™ā€¦

const A = [[1, 2, 3, "name"]];
test.each(A)('%s', (a,b,c) => expect(a+b).toBe(c));

Actual result:

 PASS  ./index.test.js
  āœ“ 1 (5ms)

By the way, ā€œ%4$sā€ doesnā€™t work eitherā€¦

Link to repl or repo (highly encouraged)

A small repo with a single test file that perfectly illustrate the issue

Run npx envinfo --preset jest

Paste the results here:

  System:
    OS: macOS High Sierra 10.13.6
    CPU: (4) x64 Intel(R) Core(TM) i7-5650U CPU @ 2.20GHz
  Binaries:
    Node: 8.9.2 - ~/.nvm/versions/node/v8.9.2/bin/node
    Yarn: 1.12.1 - ~/.yarn/bin/yarn
    npm: 6.3.0 - ~/.nvm/versions/node/v8.9.2/bin/npm
  npmPackages:
    jest: ^23.6.0 => 23.6.0

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 13
  • Comments: 23 (6 by maintainers)

Most upvoted comments

Sorry guys, but I slept on this, and it really bugs me.

When you decide to use an option to stop the tests after the first failure, in what kind of context would it be the expected behavior that it keeps failing all the tests in the current file?

Moreover, you are telling me that with jest, if I want to create 20 tests and I want the tests to stop at the first failure, my only choice is to create 20 test files?!

Every other test system Iā€™ve used means I can add --bail and then the output of the failed test is the most recent thing in scroll.

Not jest.

Please, please, please, fix this?

With all due respect (and do mean respect), Michal, the ā€˜collaboratorā€™ badge that you and several other people sport here, suggests you are more capable to address this issue (with no need of assuming the offensive). I apologize if I sounded rude, sorry - I wanted to draw attention to this issue.

Apparently it is an issue - it was reported a year ago - there was an argument, yet the OPā€™s reasons seem to outweigh the responses. There is no working bail, no way to abruptly stop the tests - if itā€™s the part of the bigger design, maybe it should be shared? Or fixed accordingly?

You might find me narrow-minded, but I donā€™t think that

The bail config option can be used here to have Jest stop running tests after the first failure.

that I can read there, would normally be interpreted by:

If the bail config option is set, Jest will stop the tests if at least one test failed in the current file.

Actually, I think that most people would understand: Jest will stop running tests after the first failure šŸ˜œ

Iā€™ll check if Iā€™m able to provide a PR about this if you are willing to change the way bail is working.

@cpojer what do you think about --bail stopping at the first test instead of the first test file?

@Sharcoux if the docs arenā€™t clear about this then Iā€™m happy to merge a clarification šŸ‘Œ

Just got bitten by this as well. Iā€™d say that the current description of bail is even more misleading:

Alias: -b. Exit the test suite immediately upon n number of failing test suite. Defaults to 1.

Bail currently keeps running the remaining tests in a file even when the first one fails i.e. it does not appear to ā€œexit the suite immediatelyā€.

Or does it mean that it should exit jest (not the test suite) immediately after a single test suite fails? Either way the language and grammar are rather confusing.

Thanks for your reply.

A ā€œtest suiteā€ in jestā€™s terminology means a test file, not a describe

Iā€™m not sure about the relevance of the describe function in this caseā€¦ But ok. The doc should really be corrected, though, because it is very confusing.

bail also just stops at the file level, it doesnā€™t abort running more tests within the same file.

Shouldnā€™t this be reflected into the doc?

Regarding verbosity

In my use case, I provide students with a project that have already the tests written for them. In this case, if they run npm run test they will be flooded by test failures and they will only be able to read the last tests, whereas the tests are written with progressive difficulty. But the one they are interested in are probably the first ones.

Fortunately, I finally could work around this by hacking my way through each.

Opened 2 years ago and still no progressā€¦Well, I have a similar question. How can I force the ā€œdescribeā€ test to be counted as a suite? I donā€™t care now if you have a bug or misunderstanding in ocumentation, I just want to tell the Jest what to consider as a test suite, cause now I have 1 test file and several ā€œdescribeā€ tests inside and I want each of this ā€œdescribeā€ tests to be considered as a Suite, not the file entirely. I need this to see the success/fail rate through the ā€œdescribeā€ tests.

That is the thing, you canā€™t. In the end I had to separate the describe blocks into different files.