DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Edward Ezekiel
Edward Ezekiel

Posted on • Originally published at Medium on

Javascript Testing With Mocha

Javascript Testing

One of my favorite parts about going to a coding bootcamp was all the automated tests. It was incredibly helpful to see whether my code contained errors, and to get hints about what might be causing those errors.

Now that I’ve graduated it’s up to me to write those tests for my own projects!

I had written some Ruby tests using Capybara, so now I wanted to try my hand at writing Javascript tests.

Mocha

In honor of β€œMay the 4th Be With You” my alternative title for this post is β€œMay the Mocha Be With You.” I found that Mocha was easy to get up and running and a pleasure to use.

Mocha is a Javascript testing framework. It works for testing JS in the browser, as well as JS that is executed in a node JS environment.

My First Mocha Test

I decided to write a Mocha test that would be executed in the node JS environment.

Here is the code:

// Require the built in 'assertion' library
 var assert = require('assert');

describe('Compare', function() {
 describe('Star Wars Quotes', function() {
 it('should test whether two data types are equivalent ', function() {
 assert.equal(true, typeof "May the Fourth Be With You" === typeof "Help me Obi Wan Kenobi, you're my only hope")
 })
 })
 })

This test passes, because both operands are strings, so function will return the expected output of true.

Lessons Learned

The Getting Started guide on mochajs.org has very basic instructions on how to get your first Mocha test running.

However, I kept getting an error message about my package.json file whenever I tried to run β€œnpm test.” The error message claimed β€œno such file or directory” existed. Eventually I ran the command β€œnpm init” from within the project directory. That created a package.json file within my project directory and that fixed the problem.

Resources

There are a lot of great resources out there to learn Mocha if you are interested. Of course, there is the official documentation at mochajs.org.

There is also a more in-depth tutorial on medium by codeburst

Top comments (0)

🌚 Browsing with dark mode makes you a better developer by a factor of exactly 40.

It's a scientific fact.