Generated with sparks and insights from 3 sources
Introduction
-
Jest's
[beforeEach](prompt://ask_markdown?question=beforeEach)
function is used to run a specific setup code before each test in a test file. This ensures that each test starts with a clean state. -
[test.each](prompt://ask_markdown?question=test.each)
in Jest allows you to run the same test with different data sets. This is useful for parameterized testing where the same logic needs to be tested with various inputs. -
Using
beforeEach
helps in avoiding code repetition by setting up common test conditions once for all tests. -
test.each
can be used with arrays of arrays or arrays of objects to provide different sets of parameters to the test function. -
Both
beforeEach
andtest.each
can handle asynchronous code, making them versatile for various testing scenarios.
beforeEach Function [1]
-
Purpose:
beforeEach
runs a function before each test in a file, ensuring a clean state for each test. -
Usage: Commonly used to initialize or reset data, such as databases or mock objects, before each test.
-
Asynchronous Support: Can handle asynchronous code by returning a promise or using the
done
callback. -
Example:
beforeEach(() => { [initializeCityDatabase](prompt://ask_markdown?question=initializeCityDatabase)(); });
-
Scoping:
beforeEach
inside adescribe
block only applies to tests within that block.
test.each Function [2]
-
Purpose:
test.each
allows running the same test with different data sets, useful for parameterized testing. -
Usage: Can be used with arrays of arrays or arrays of objects to provide different sets of parameters.
-
Example:
test.each([[1, 1, 2], [1, 2, 3], [2, 1, 3]])('.add(%i, %i)', (a, b, expected) => { expect(a + b).toBe(expected); });
-
Asynchronous Support: The test function can be asynchronous, returning a promise or using async/await.
-
Flexibility: Supports various data formats and can generate unique test titles using printf-style formatting.
Setup and Teardown [1]
-
Setup:
beforeEach
and[beforeAll](prompt://ask_markdown?question=beforeAll)
are used for setup tasks before tests run. -
Teardown:
[afterEach](prompt://ask_markdown?question=afterEach)
and[afterAll](prompt://ask_markdown?question=afterAll)
are used for cleanup tasks after tests run. -
Example:
beforeEach(() => { initializeCityDatabase(); }); afterEach(() => { [clearCityDatabase](prompt://ask_markdown?question=clearCityDatabase)(); });
-
Asynchronous Support: Both setup and teardown functions can handle asynchronous code.
-
Scoping: Setup and teardown functions inside a
describe
block only apply to tests within that block.
Scoping in Jest [1]
-
Global Scope:
beforeEach
andafterEach
at the top level apply to all tests in the file. -
describe block Scope: Hooks inside a
describe
block only apply to tests within that block. -
Nested Describe Blocks: Hooks in nested
describe
blocks follow a hierarchical order of execution. -
Example:
describe('outer', () => { beforeEach(() => { /* setup */ }); describe('inner', () => { beforeEach(() => { /* inner setup */ }); }); });
-
Order: Top-level hooks run before nested hooks, ensuring proper setup and teardown.
Order of Execution [1]
-
Describe Handlers: Jest executes all
describe
handlers before running any tests. -
Hook Order:
beforeAll
runs beforebeforeEach
, andafterEach
runs beforeafterAll
. -
Example:
beforeAll(() => console.log('1 - beforeAll')); beforeEach(() => console.log('1 - beforeEach')); test('', () => console.log('1 - test'));
-
Nested Hooks: Hooks in nested
describe
blocks follow a hierarchical order. -
execution flow: Ensures proper setup and teardown, avoiding interference between tests.
Related Videos
<br><br>
<div class="-md-ext-youtube-widget"> { "title": "JEST beforeEach and afterEach (JavaScript)", "link": "https://www.youtube.com/watch?v=UFjtOmvmAU0", "channel": { "name": ""}, "published_date": "Nov 25, 2023", "length": "" }</div>
<div class="-md-ext-youtube-widget"> { "title": "Jest tutorial #7 Before Each | run before every test case", "link": "https://www.youtube.com/watch?v=WbUTR_E4osg", "channel": { "name": ""}, "published_date": "Apr 7, 2020", "length": "" }</div>
<div class="-md-ext-youtube-widget"> { "title": "beforeall and afterall (JEST JavaScript)", "link": "https://www.youtube.com/watch?v=vaO1PA7MiuY", "channel": { "name": ""}, "published_date": "Nov 28, 2023", "length": "" }</div>