oldoc63 / learningFS

Web development back and front
0 stars 0 forks source link

Teardown #1056

Open oldoc63 opened 2 years ago

oldoc63 commented 2 years ago

So far, we've been writing just one test using a single it() block. However, in most situations, we will need to write many tests for a particular feature that get executed in succession.

Running multiple test can introduce issues if the tests make changes to the testing environment: changes to the environment in one test might affect the next test. Some common changes to an environment include:

To address this issue, we often add a teardown step to the end of our tests. The teardown phase makes our tests isolated by resetting the environment before the next test runs. This provides two key benefits:

oldoc63 commented 2 years ago

This exercise uses Node's filesystem library fs in addition to assert. It's okay if you're not familiar with fs -we will briefly explain the key methods we are using:

oldoc63 commented 2 years ago

Note the new file message.txt created in your directory. It should have the text Hello Node.js. In both tests, we are modifying the enviroment by creating a new message.txt file. However, the current tests do not have a teardown phase. The test fails because the second test appends an empty string to the file created from the first test instead of creating a new file with just an empty string. Furthermore, if you were to run the test file again, the first test would now fail. Run the test again and see for yourself - the message.txt file will now have the text Hello Node.jsHelloNode.js.

oldoc63 commented 2 years ago

You got different output because the test was not isolated.

oldoc63 commented 2 years ago

To ensure that each test is isolated from each other, and that each run of the test file isolated, we will add a teardown step to the tests. In this case, we want to make sure that after each test, the file found at path is deleted.

oldoc63 commented 2 years ago

This keeps the tests isolated.