FAQ: Automate and Organize Tests - Hooks

This community-built FAQ covers the “Hooks” exercise from the lesson “Automate and Organize Tests”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Learn JavaScript Unit Testing

FAQs on the exercise Hooks

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

the solution is having a bug,
having bug in chapter about bugs, and unit testing is a meme situation

const contents = fs.readileSync(path);
should become
const contents = fs.readFileSync(path);

1 Like

Bug still present nearly a year later… what’s the best way to raise these issues?

Edit: Raised a bug report

Well, wasn’t the whole exercise based on maneuvering around that misspelling error?
That is exactly why we need:

afterEach(() => {

That’s the point of the whole exercise, it’s not a bug :wink:
I like my code to run smoothly too, so I just took care to edit it to:

const contents = fs.readFileSync(path);
1 Like

I make the code to dlete the path of the “message.txt” inside a forEach hook but no thing happen and an error is executed instead;

If path = ‘./message.txt’ needed to be performed before each test and fs.unlinkSync(path); needed to be performed after each test, how come we used the before() callback in the first step instead of beforeEach()?


Where is better to declare a variable that is supposed to be used in a Hook?
In the example let testValue; is inside the describe block, but let path, str; is outside of the describe block.

Wouldn’t it be better to move let path, str; inside the describe block?
Instead of this:

const assert = require('assert');
const fs = require('fs');
let path, str;

describe('appendFileSync', () => {
// .... omitted

why not this?:

const assert = require('assert');
const fs = require('fs');

describe('appendFileSync', () => {
let path, str;
// .... omitted

What’s the point of the before(callback) function, when we can just set path = './message.txt' once above both tests?


I came here for the same question, what is the use of beforeEach then? Seems like it still hasn’t been answered

I think it’s because path only needs to be declared once. Its value does not change in either tests, it’s always './message.txt.'

So you use before(), which runs once before both tests, to declare path a single time, and use that value for both tests. You could probably use beforeEach() too if you wanted to, but there’s no reason to.

The idea is to lesson the amount of code you’re writing. In this specific case it’s not the best example I guess, because in the time it takes you to write a before() function declaring path you could just declare path twice in each test. But if you had like, I dunno, twenty tests to run and they all worked with the same path value, it’d prolly make a lot more sense to only have to declare it once prior to running the testing suite, instead of writing './message.txt.' twenty times.

I ran this question by chatGPT and it appears you are correct.

The before hook runs once before all the tests within its context, whereas the beforeEach hook runs before each test.

Therefore, if there is common setup work required for a set of more than one tests running under the same context then we use before so that the setup is done only once for all the tests in the context. This saves execution time by reducing redundant setup operations. On the other hand, If setup operations such as loading data from database differ between tests, then we use beforeEach so that each test can be setup with new data.

To summarise -

  • Use before if you want to run some code once in the beginning of a test suite.
  • Use beforeEach if you want to run some code before every single test case in your test suite.
1 Like

The question from @ micro4950816268 remains unanswered.
If it is possible to declare the variables at the top of our test using vanilla JS, why to use the before(callback) hook?

1 Like

i straight up used “path = ‘./message.txt’” instead of “before()” and the output was reaming the same. So why do i need to add another line of code to use “before()”?
soo confused:(

Hello, I’m late to the party. If the assignment of the path, str happened inside the describe function then those variables would be bound to the scope of the one function. While assigning them outside the describe function also assigns them in the global scope (within this module), it allows the second function to use the same variables. And also assignment as such keeps the DRY (don’t repeat yourself) ideology ongoing.

I have this question also? Why not beforeEach() when we’re also using afterEach()?

Could it be because path only needs to be set once and remains set after the first test so only needs to be set before the first test? But the file needs to be disposed of after each test completes so afterEach is required?