So far, we have no guarantees that our application is working as expected. Installing Mocha is the first step toward solving this problem.
Mocha, is developed by TJ Holowaychuk, who is also the creator of Express. We'll also install Expect.js, an assertion library and we'll write our first test. So why do we need to do this? Our code might work now but what about now as time? How about a month's time, or maybe a year's time? By writing tests to prove that the code is doing what we expect, we can be sure that our application will continue to work so long as we make sure that the code always passes our tests.
This is especially important when we start to collaborate with other people, as it allows them to make changes, knowing that they can just run the tests and make sure that they have not broken anything else. First, we need to install Mocha. We'll install it globally like the docs tell us to, with npm install -g mocha. Be aware that you might have to install with pseudo, or as an administrator if the privileges of your Node.js install aren't correct.
In addition to Mocha we'll need an assertion library to use. In this case we'll use Expect.js, which we'll install into our project. Now that we have it installed, let's make sure that Mocha works. To do this, we'll create a test that does not depend on our application code, and run it. This is the test taken from the Mocha documentation.
So let's save our file and just run Mocha. As we can see the tests pass. Next, lets update our assertions. Whilst, we could use the Assert library, personally I find Expect.js much easier to read. With Asserts, we assert equals minus one. An index of five in one, two, and three. With Expect.js we except one, two, and three index of five to equal minus one.
It reads a lot more like the English language, which is why I find it easier. If you want to go on using Assert, feel free, but I'll be using Expect for the remaining of the tests. As we've changed our tests we need to run them again to make sure that everything is still passing, which it is. Great! Our tests are running. All of our tests passed first time through. So let's add a test that is intended to fail to make sure that things are working correctly.
We'll make it really simple. Let's just say that the number one is expected to equal the number seven. Obviously this isn't true, so when we run the test it will fail. Once the test has failed we can change the number seven to one and run it again to make sure that the tests pass. As all our tests pass let's delete our two example tests, so that we're ready to start writing tests for our application next time.
Mocha allows us to define expected behavior for our code coupled with Expect.js, which allows us to assert that the expected behavior is actually what happens. Our tests help us to prove that things work as expected, both now and in the future. In the next video, we'll be performing a little re-factoring on our application so that we can test things more easily.
To start, author Michael Heap creates a new Express application, showing how to configure it and increase application visibility with logs. Explore Express along with various libraries that will help improve your development experience. Then take a look at technologies such as SSL and nginx, and work through deploying your application to production in a secure and scalable way. Michael also introduces some existing open-source Express projects and reviews how they are structured, to help you organize your own applications in a systematic way. By the end of the course, you'll be familiar with a wide range of new Express tools and libraries, all of which will help you deliver the best value to your customers.
- Consuming an API
- Showing results on a webpage
- Caching requests in memory
- Refactoring for testing
- Mocking to remove dependencies
- Spying with Sinon.JS
- Sending and receiving data in real time
- Mounting subapplications
- Serving content conditionally for AJAX
- Securing your app
- Improving performance
- Examining large-scale Express apps: Ghost.org and Balloons.IO