Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
When we set something up before our tests, we need to be sure we tear it down afterward too. In this video, weβll use Mocha functions called 'after' and 'afterEach'.
Resources
Video review
- Mocha provides a "teardown" phase to remove unwanted variables
- If your tests change your development environment, like creating a pretend database, or start up a local server, you can use the teardown block to set your system back to where it started
- Mocha's
after()
andafterEach()
hooks work exactly likebefore()
andbeforeEach()
, expect that they happen after - If you find yourself depending heavily on the teardown phase, you should double-check that youβre testing the right kind of function
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Once your test suite runs you
won't need the set of variables or
0:00
objects any longer.
0:03
In fact, if you leave those
around they might interfere with
0:05
other tests that run later.
0:07
That's why mocha provides a tear down
phase, to remove unwanted variables.
0:09
In addition, if your test changed
your development environment,
0:15
like creating a pretend database.
0:18
Or start up a local server,
0:20
you can use the tear down block to set
your system back to where it started.
0:22
You'll need tear downs more often when
your tests need a pretend database, or
0:28
they interact with the DOM somehow.
0:32
For example, you might be testing
a function that sets up a new remote
0:34
database, creates a local file or
starts a server instance.
0:38
When these functions are doing their job,
they end up polluting your database or
0:42
host service with a bunch of
junk testing tables and files.
0:47
In that case, you'll want to take
apart any changes your test make so
0:51
that the test further runs
to start with a clean slate.
0:54
And your external systems don't
get crowded with test results,
0:57
sometimes that really
is what you need to do.
1:02
But if you find yourself depending
heavily on the tear down phase,
1:04
you should double check that you're
testing the right kind of function.
1:08
For example, developers often rely on
an external library like jQuery or
1:11
React to deal with the DOM.
1:16
We shouldn't be writing tests to make sure
the addClass function works correctly.
1:18
That function has already been
thoroughly tested for us.
1:23
We should also definitely try to avoid
passing state between our test suites.
1:26
That adds unnecessary complexity and
room for error remember,
1:30
our tests should not be clever.
1:35
They don't need to do anything fancier
than report our basic expectations.
1:37
Our battleship engine doesn't
rely on an external database or
1:43
write files to our machine,
it only needs a few objects in memory.
1:46
We saw in the last video that JavaScript
objects can just be overwritten
1:51
in the before and before each blocks.
1:56
So instead of destroying our object,
I'll show you how to use the after and
2:01
after each hooks to print
a message in our test output.
2:06
Mocha's after and after each hooks
were exactly like before and
2:10
before each,
except that they only happen after.
2:15
So in the fire suite,
right below the before each block,
2:19
I'll add an after block by typing actor.
2:24
Now after takes one function that will
run at the very end of the test suite,
2:29
when every spec has finished.
2:34
Inside the function,
2:36
I'll add a console.log that says
entire test suite completed.
2:38
So now right below
the after block I'll create
2:47
an after each block by typing
afted just like after.
2:51
Actor each takes one function that runs
at the very end of each test spec.
2:57
So inside this function i'll console dot
log that says one unit test completed.
3:05
All right, so I'll save my file.
3:16
And if I run my tests in
the console by typing npm test,
3:18
you can see the message, one unit test
completed after each individual test.
3:26
And then we see,
3:32
entire test suite completed at
the very end of the block, perfect.
3:33
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up