5. Test Running with Jest Flashcards
What are the rules determining whether a JS file is a test file for Jest?
- Any JS file inside a folder named
\_\_tests\_\_
are considered tests. - Any JS files with
*.spec.js
or*.test.js
extension in their names are considered tests.
What are the pros and cons of having tests in their own folders?
- Easily distinguish between test and non-test files.
- Unrelated files can share a folder (i.e., a
loginService
test and acryptoHash
test). - It’s very easy to isolate a particular set of tests that are in the same folder.
- Tests can be named anything but must be inside an appropriately named folder (i.e. __tests__), to be recognised.
What are the pros and cons of having tests alongside their corresponding components?
- Which files are components and which files are spec is not always obvious.
- Tests are always directly adjacent to the files they apply to.
- Unrelated tests are less likely to share a folder.
- Tests must have the correct naming patterns to be recognised, i.e., *.test.js
- Possible to isolate tests based on name patterns, i.e., user-*
What are the two most important Jest globals?
describe
and it
.
What is the Jest global it
?
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.
What is the Jest global it
also known as?
The TEST global.
What is the Jest global describe
also known as?
The SUITE global.
What is the Jest global describe
?
An optional method for grouping any number of it
or TEST statements.
What happens in “watch mode”?
Tests are run automatically as files change.
What tests are re-run in watch mode?
Only the ones pertaining to changed files.
What does the BeforeEach
global do?
Runs a block of code before each and every one of your tests.
What is the BeforeEach
global useful for?
Setting up databases, mock instances, etc.
What does the BeforeAll
global do?
Runs a block of code only once before the tests inside the suit are run.
What does the AfterEach
global do?
Runs a block of code after each test.
What does the AfterAll
global do?
Runs a block of code after the last test inside the suit.