Skip to content
This repository was archived by the owner on Mar 16, 2026. It is now read-only.
This repository was archived by the owner on Mar 16, 2026. It is now read-only.

Hooks #1063

@oldoc63

Description

@oldoc63

We learned about the four main phases of a test: setup, execute, verify and teardown. In the last exercise, you may have notice 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 test.
These functions (also referred 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 test.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions