Jest and recurring actions

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Sometimes you want to have Jest perform a recurring action between each task.
Some examples: Query a database, clear storage, clear mocked data, or reset a mocked route.
We don't really want to be bothered with having this recurring code in each test, and luckily Jest has a solution for us.
There are four functions we can hook into:
beforeEach: Runs before each testafterEach: Runs after each testbeforeAll: Runs before all testsafterAll: Runs after all testsLet's sketch an example. We have a database function to call, so the steps we want to achieve are:
This scenario is a perfect case for all four functions to hook into.
The first thing we want is to create our database, for which we'll use the beforeAll function.
beforeAll(() => {
return createDatabase();
});
The next step is to populate the database with demo data we can alter in our tests.
beforeEach(() => {
return populateDatabase();
});
Our test might alter/remove/create elements in this database, so we want to clear it between each test.
afterEach(() => {
return clearDatabase();
});
And once we are all done, we should remove the database so the next run will be fresh again.
afterAll(() => {
return removeDatabase();
});
And that's it, these four steps will now run at the needed times. To showcase this, let's create this sample test file and see when each call is used.
test('user database has Chris', () => {
expect(db.user.hasName('Chris')).toBeTruthy();
});
test('user database doesnt have Thomas', () => {
expect(db.user.hasName('Thomas')).not.toBeTruthy();
});
The firing order is as follows:
beforeAll: Created the databasebeforeEach: Populates databaseafterEach: Clear databasebeforeEach: Populates databaseafterEach: Clear databaseafterAll: Remove databaseAnd that's the flow it will take.
We can quickly make our test more manageable and work in specific ways to ensure each test is solid and fresh.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter