45 minutes
Here are links to lessons that should be completed before this lesson:
Learn to use another testing tool for flexibility.
Participants will be able to:
Both Mocha and Jasmine are popular JavaScript testing frameworks for writing BDD (Behavior-Driven Development) tests. So, why might you choose one over the other?
Jasmine: Jasmine attempts to provide everything that a developer would need in a testing framework. It does not need the browser, the DOM, or any JavaScript framework to work, so it’s simple to get up and running.
Mocha: Mocha does not claim to be a complete testing framework. It is intended to be extended with other frameworks, such as Chai. People like it for its flexibility. It runs on Node and in the browser.
At the end of the day, both Jasmine and Mocha are popular testing frameworks that many companies use. There is no wrong answer: the testing framework that you choose for projects is up to you.
Let’s get started by setting up a new project with Mocha.
Create a new project
mkdir mocha-practice
- create a folder for your new projectcd mocha-practice
- change directories to that foldernpm init --yes
- make the package.json
fileInstall Mocha
npm install --save-dev mocha
Create Test Files
mkdir test
- your tests should live in this folder, so that mocha knows where to find themtouch test/myTest.js
- add a test file to the test
directoryStart Test
"test": "mocha"
npm test
! Since you have no tests written yet, you should see something like this:> mocha
0 passing (1ms)
Syntax
describe
and it
functions work like Jasmine’s similarly named functions:const assert = require('assert');
describe('Mocha String Test', function() {
it('should return the exact number of characters in a string', function() {
assert.equal('Hello'.length, 4); // this line will fail
});
it('should return first character of the string', function() {
assert.equal('Hello'.charAt(0), 'H'); // this line will pass
});
});
test.js
file, and run the tests again. You should now see one passing test, and one failing test.Mocha String Test
1) should return the exact number of characters in a string
✓ should return first character of the string
1 passing (8ms)
1 failing
1) Mocha String Test
should return the exact number of characters in a string:
AssertionError [ERR_ASSERTION]: 5 == 4
+ expected - actual
-5
+4
at Context.<anonymous> (test/myTest.js:5:16)
assert.equal(a, b)
mocha expects the values to be equivalent, or else the test will fail.done
) to it
function and invoke the callback function after your test is complete.var assert = require('assert');
function asyncFunction(stringToTest, callback) {
setTimeout(function() {
callback(stringToTest.charAt(0) === 'H');
}, 1);
}
// Note: setTimeout has been used to simulate the async behavior
// Warn: Using setTimeout is not the best practice since it's execution time is unpredictable or unreliable
describe('Mocha String Test', function() {
it('should return first character of the string', function(done) {
asyncFunction('Hello', function(isValid) {
assert.equal(isValid, true);
done();
});
});
});
To learn more about testing asynchronous code with Mocha, see the docs.
Let’s install Chai, an assertion library commonly used with Mocha!
install Chai
npm install --save-dev chai
setting up Chai
assert
: These statements typically take two arguments, and “assert” that something is truthful.const assert = require('chai').assert;
const foo = 'foo';
// Passing
assert.typeOf(foo, 'string');
// Failing - note that the third argument is an optional error message
assert.equal(foo, 'bar', 'foo equal `bar`');
expect
: “Expect” statements chain together assertions that feel like natural languageconst expect = require('chai').expect;
const foo = 'foo';
// Passing
expect(foo).to.be.a('string');
// Failing
expect(foo).to.equal('bar');
should
: “Should” statements are similar to “expect”, but start with the start each chain with a should
const should = require('chai').should();
const foo = 'foo';
// Passing
foo.should.be.a('string');
// Failing
foo.should.equal('bar');
Note: If you like this style, you can use the should library with Mocha, as that is a common reason to use it over Jasmine.
FizzBuzz (again!) Within your Mocha project, re-write the FizzBuzz test from the Jasmine Testing exercises, using assert
syntax. Refer to the docs as needed to find the matchers you need. Ensure that the test passes.
const fizzBuzz = require('../src/fizzBuzz');
const assert = require('chai').assert;
describe("fizzBuzz", function() {
it("should be defined", function() {
assert.exists(fizzBuzz);
});
it("should return 'fizz' when given a multiple of 3", function() {
assert(fizzBuzz(3), "fizz");
assert(fizzBuzz(6), "fizz");
});
it("should return 'buzz' when given a multiple of 5", function() {
assert(fizzBuzz(5), "buzz");
assert(fizzBuzz(10), "buzz");
});
it("should return 'fizzbuzz' when given a multiple of 3 and 5", function() {
assert(fizzBuzz(15), "fizzbuzz");
assert(fizzBuzz(30), "fizzbuzz");
});
});
expect
style assertions, referring to the docs as needed. Add assertions that:
metersToInches
are floatsmetersToInches
have the correct results:input | value |
---|---|
metersToInches(0) | 0 |
metersToInches(1) | 39.3701 |
metersToInches(1.5) | 59.05515 |
metersToInches(15.6) | 614.17356 |
['Fido', 'Fluffy', 'Roxy']
(Hint: if the equal
assertion fails, take a look at this article)Do the Mocha and Chai projects have their own internal tests? Read them and see how they are used.
Chai and Mocha blog post, link
Question: What are Mocha and Chai, and how do they work in your project? How do they differ from Jasmine?
Exercise: Every student should pick a Chai assertion, like .to.have.lengthOf
. Describe that matcher to the class and how it should be used.