Some tests require a fourth phase called teardown. This step makes your test isolated.
- Teardown - reset any conditions that were changed during the test
A test, like the example in this exercise, can make changes to its environment that could affect other tests. The teardown phase is used to reset the environment before the next test runs.
Some common changes to an environment include
- altering files and directory structure
- changing read and write permissions on a file
- editing records in a database
In some cases — including the previous exercise — the teardown phase isn’t needed because there are no conditions to reset.
This exercise uses Node’s filesystem library
fs in addition to
assert. It’s okay if you’re not familiar with
fs: each function is described in comments.
The call to
fs.appendFileSync(path, str) will do one of two things:
- If a file at
pathdoes not exist, the method creates the file and adds the string
strto that file.
- If the file does exist, the method adds the string
strto the end of the file.
The current test does not have a teardown phase. Run it using the terminal.
Run the test suite again. Compare this output to the output of the previous test run.
Note the new file
message.txt created in your directory.
project |_ node_modules |_ test |___ index_test.js |_ message.txt |_ package.json
You got different output because the test was not isolated.
- In the first run the file didn’t exist, so
message.txtfile containing the string.
- In the second run, the method added the string to the end of
message.txtbecause it already existed.
In the terminal enter
rm message.txt to delete the file.
Add this code below
// Teardown. This method will delete the file at
path before the test finishes.
Verify that you fixed the issue: run the test suite twice and compare the outputs.