Skip to content

Commit

Permalink
Improve documentation of describe and test ordering. (#5217)
Browse files Browse the repository at this point in the history
* Improve notes on execution order of test file code

Explain that describe blocks are executed first (I've had annoying things where describes were being used to set up state instead of before* hooks), note that tests are run serially in the order they are discovered, and add a note about `test.concurrent`.

* Clarify tests are run serially and tidy up.

* Improve example.

* Update CHANGELOG.md

* Remove test.concurrent documentation

Also fix typo!
  • Loading branch information
daphtdazz authored and cpojer committed Jan 3, 2018
1 parent ca5d0fc commit 4eded0c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

### Chore & Maintenance

* `[docs]` Describe the order of execution of describe and test blocks, and a note on using `test.concurrent`.
([#5217](https://github.com/facebook/jest/pull/5217))

## jest 22.0.4

### Fixes
Expand Down
47 changes: 47 additions & 0 deletions docs/SetupAndTeardown.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,53 @@ describe('Scoped / Nested block', () => {
// 1 - afterAll
```

### Order of execution of describe and test blocks

Jest executes all describe handlers in a test file *before* it executes any of the actual tests. This is another reason to do setup and teardown in `before*` and `after*` handlers rather in the describe blocks. Once the describe blocks are complete, by default Jest runs all the tests serially in the order they were encountered in the collection phase, waiting for each to finish and be tidied up before moving on.

Consider the following illustrative test file and output:

```
describe('outer', () => {
console.log(`describe outer-a`);
describe('describe inner 1', () => {
console.log(`describe inner 1`);
test('test 1', () => {
console.log(`test for describe inner 1`);
expect(true).toEqual(true);
});
});
console.log(`describe outer-b`);
test('test 1', () => {
console.log(`test for describe outer`);
expect(true).toEqual(true);
});
describe('describe inner 2', () => {
console.log(`describe inner 2`);
test('test for describe inner 2', () => {
console.log(`test for describe inner 2`);
expect(false).toEqual(false);
})
});
console.log(`describe outer-c`);
});
// describe outer-a
// describe inner 1
// describe outer-b
// describe inner 2
// describe outer-c
// test for describe inner 1
// test for describe outer
// test for describe inner 2
```

### General Advice

If a test is failing, one of the first things to check should be whether the
Expand Down

0 comments on commit 4eded0c

Please sign in to comment.