Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
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`.
  • Loading branch information
daphtdazz authored Jan 2, 2018
commit 2a1f40521e39967fc6ddba6287d89b5a569e55b9
55 changes: 55 additions & 0 deletions docs/SetupAndTeardown.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Often while writing tests you have some setup work that needs to happen before
tests run, and you have some finishing work that needs to happen after tests
run. Jest provides helper functions to handle this.



### Repeating Setup For Many Tests

If you have some work you need to do repeatedly for many tests, you can use
Expand Down Expand Up @@ -147,6 +149,59 @@ 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 an extra reason to use the `before*` and `after*` handlers rather than relying on closures. Once the describe blocks are complete, jest runs all the tests in the order they were encountered in the collection phase. Consider the following illustrative test file and output:

```
describe('outer', () => {

console.log(`describe outer`);

describe('describe inner 1', () => {
console.log(`describe inner 1`);
test('test 1', () => {
console.log(`test for describe inner 1`);
expect(true).toEqual(true);
});
});

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);
})
});
});

// describe outer
// describe inner 1
// describe inner 2
// test for describe inner 1
// test for describe outer
// test for describe inner 2
```

### Concurrent tests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind removing this section again? test.concurrent has some shortcomings and I would prefer not to document it as we may change it in a future release to be better supported.


If you have multiple tests in the same file that may take a long time and
are independent, then you can mark that they can be executed concurrently
using `test.concurrent()`:

```
test.concurrent('long test', () => {
return new Promise(resolve => someLongFunction(resolve));
});
```

Note that concurrent tests must return a promise.

### General Advice

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