Link to blog: https://medium.com/swlh/intro-to-javascript-testing-with-mocha-1366dad9b973
For the next (and last) couple weeks of class, we are required to create our own in-class activity as if other students were going to use it. We were tasked with choosing something different than we have done already, so my group decided on testing JavaScript programs with the Mocha framework. I personally have no experience with Mocha, but I do have a tiny bit of experience with JavaScript, so I was interested to learn more about how different testing looked compared to what I was currently experiencing. This is how I found Valerie Foster’s blog post Into to JavaScript Testing with Mocha. Here’s what I found.
Foster does give a few instructions on how to install the Mocha framework to your project, but I’ll mainly focus on the pros and cons of testing within the framework. They include a codeblock of a simple test, however it was not 100% clear to me at first what each line meant. Here are a few things included in the codeblock, and what they do:
- describe(string, function)
- According to Codeacademy.com, the describe function groups tests together and uses the first parameter, the string, to explain that group. The second parameter, the function, is a callback function which has ‘it’ tests (I’ll get to that in a moment)
- it(string, function)
- The it function is similar to the describe function, as its parameters are the same. However, the string it accepts is instead used to explain what the test itself is checking for. So, for example it(‘should //do something when //condition’, function(){})
Foster then discusses some customization options for Mocha, but then specifies their favorite assertion library in Mocha, called chai. One of the key benefits of this library is that it might make the test functions easier to read (e.g., going from assert.equal(a, b) to expect(a).to.equal(b), according to Foster).
I selected this article because I wanted to have more of a background on Mocha and testing with Javascript since it is going to be our project. Also, Foster seems capable and comfortable with Mocha and JS, and makes it easy to understand what is going on in tests with a framework I’ve never seen before. A good beginner’s guide is always helpful to a fresh start.
I liked this blog because it felt informative and informal; like I was being taught something new but not in a super serious tone (Foster shared their favorite terminal customization was a Nyan Cat reporter). I learned a bit more about how testing is done in JavaScript, including describe and it functions. This affected me by boosting my confidence in my ability to assist my group with our assignment and it encouraged me to learn more JS and Mocha as a whole. I expect to apply such learned knowledge to future web applications and perhaps even past projects.
From the blog CS@Worcester – Josh's Coding Journey by joshuafife and used with permission of the author. All other rights reserved by the author.