5. Test Running with Jest Flashcards

1
Q

What are the rules determining whether a JS file is a test file for Jest?

A
  1. Any JS file inside a folder named \_\_tests\_\_ are considered tests.
  2. Any JS files with *.spec.js or *.test.js extension in their names are considered tests.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the pros and cons of having tests in their own folders?

A
  1. Easily distinguish between test and non-test files.
  2. Unrelated files can share a folder (i.e., a loginService test and a cryptoHash test).
  3. It’s very easy to isolate a particular set of tests that are in the same folder.
  4. Tests can be named anything but must be inside an appropriately named folder (i.e. __tests__), to be recognised.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the pros and cons of having tests alongside their corresponding components?

A
  1. Which files are components and which files are spec is not always obvious.
  2. Tests are always directly adjacent to the files they apply to.
  3. Unrelated tests are less likely to share a folder.
  4. Tests must have the correct naming patterns to be recognised, i.e., *.test.js
  5. Possible to isolate tests based on name patterns, i.e., user-*
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the two most important Jest globals?

A

describe and it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the Jest global it?

A

it is a method that you pass a function as an argument, and whatever that function is, it will be run as though it’s a test by Jest.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the Jest global it also known as?

A

The TEST global.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the Jest global describe also known as?

A

The SUITE global.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the Jest global describe?

A

An optional method for grouping any number of it or TEST statements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What happens in “watch mode”?

A

Tests are run automatically as files change.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What tests are re-run in watch mode?

A

Only the ones pertaining to changed files.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does the BeforeEach global do?

A

Runs a block of code before each and every one of your tests.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the BeforeEach global useful for?

A

Setting up databases, mock instances, etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does the BeforeAll global do?

A

Runs a block of code only once before the tests inside the suit are run.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does the AfterEach global do?

A

Runs a block of code after each test.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does the AfterAll global do?

A

Runs a block of code after the last test inside the suit.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the AfterEach and AfterAll globals useful for?

A

Closing open connections, terminating sub-processes, etc.

17
Q

Which one runs first — beforeEach or beforeAll?

A

beforeAll.

18
Q

What does skipping a test result in?

A

That test not being run.

19
Q

What does isolating a test result in?

A

Only that and any other isolated tests running.

20
Q

How do you isolate a test?

A

By using it.only or fit to describe it rather than just it.

21
Q

How do you skip a test?

A

By using it.skip or xit to describe it rather than just it.

22
Q

What makes an asynchronous test different?

A

An asynchronous test doesn’t complete instantaneously and can take varying amounts of time. Jest must be notified when the test is complete.

23
Q

How are the three ways that we can notify Jest that an asynchronous test has reached a conclusion?

A
  1. Invoke the done() callback that is passed to the test.
  2. Return a promise from the test.
  3. Pass an async function to describe.
24
Q

What’s the default timeout window for asynchronous Jest tests?

A

5 second or 5000 milliseconds.

25
Q

In what environment is running vs watching tests is useful?

A

Running tests is most useful for CI suites and watching tests is most useful for development.