Learn

Over the last two exercises, we learned about the four main phases of a test: setup, execute, verify, and teardown. In the last exercise, you may have noticed that the setup and teardown steps were identical between tests.

While execution and verification are unique to each test, setup and teardown are often similar or even identical for multiple tests within a test suite. The Mocha test framework provides functions that enable us to reduce repetition, simplify the scope of each test, and more finely control the execution of our tests.

These functions (also referred to as hooks) are:

  • beforeEach(callback) - callback is run before each test
  • afterEach(callback) - callback is run after each test
  • before(callback) - callback is run before the first test
  • after(callback) - callback is run after the last test

Each hook accepts a callback to be executed at various times during a test. The before... hooks naturally happen before tests and are useful for separating out the setup steps of your tests. Meanwhile, the after... hooks are executed after tests and are useful for separating out the teardown steps of your tests.

describe('messing around with hooks', () => { let testValue; // Variable used by both tests beforeEach(() => { testValue = 5; }); it('should add', () => { // testValue = 5 <-- moved to beforeEach() testValue = testValue + 5; assert.equal(testValue, 10); }); it('should multiply', () => { // testValue = 5 <-- moved to beforeEach() testValue = testValue * 5; assert.equal(testValue, 25); }); });

In this example, while each it() block could have set the testValue to 5, using the beforeEach() hook allows us to avoid that repetition while keeping each test isolated.

Keep in mind that not all setup and teardown steps should be included in these hooks. Occasionally, you may find that you need to perform some unique setup or teardown for a single test that you don’t want to include in other tests.

Instructions

1.

path = './message.txt' is repeated in both tests, but only needs to be assigned once.

First, inside the describe() block and above the first it() test, create the appropriate hook to simplify this. For now, use an empty callback function with your hook.

2.

Now, copy path = './message.txt' into the callback for your new hook and delete it from the it() blocks.

3.

Great work! We’ve moved a repetitive setup step out of the tests and into the before() hook. Run the test suite to make sure that the tests still pass.

After you’ve pressed Check Work, type clear into the terminal to clear the message.

4.

fs.unlinkSync(path); is repeated at the end of both tests. Use the appropriate hook to have this teardown executed after each test.

First, inside the describe() block and before the first it() test, create the appropriate hook to simplify this. For now, use an empty function.

5.

Now, copy fs.unlinkSync(path); into the callback for your new hook and delete it from the it() blocks.

6.

Confirm everything works by running the test suite!

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?