JavaScript Testing with Mocha JS and Chai JS Part -3 : The usage of ChaiJS assert(...)

#dc comics#dc#dc fanart#batman#bruce wayne#tim drake#batfam#dick grayson#batfamily




seen from Russia

seen from United Kingdom

seen from Italy
seen from Malaysia

seen from United Kingdom
seen from United Kingdom

seen from Greece
seen from Malaysia
seen from United Kingdom

seen from Malaysia
seen from Canada

seen from United Arab Emirates
seen from United States

seen from United States

seen from United Kingdom
seen from United States

seen from Malaysia
seen from South Korea
seen from Canada
seen from Netherlands
JavaScript Testing with Mocha JS and Chai JS Part -3 : The usage of ChaiJS assert(...)
Working with Chai JS, The JavaScript Assertion Library
ChaiJS is one of the most used JavaScript assertion library used in conjunction with the testing frameworks like MochaJs
Its has so much that it can also be used as a TDD/BDD based software development.
For the people who want to make use of Chai to its full potential, here is a playlist in youtube which depicts how to efficiently use the ChaiJS for testing
The usage of should(..) based assertions in Chai JS : The #JavaScript Assertion Library #mochajs #chaijs
Test your Node.js modules using Mocha and expect with ES2015
Continuing where we left off in "Creating Node.js modules with ES2015", wouldn't it be nice if we could write our test code in ES2015 as well.
We can and we're going to do it using Mocha and expect.
Let's install them.
Note: I recommend using nvm to manage your Node.js versions. I explain how to set it up here.
$ cd path/to/hello-world $ nvm use node $ npm i -D mocha expect $ mkdir test $ touch test/greet.spec.js
In test/greet.spec.js add the following ES2015 code:
import expect from 'expect' import greet from '../src' describe('greet', function() { context('with no arguments', function() { it('returns "Hello, world!"', function() { expect(greet()).toEqual('Hello, world!') }) }) context('with 1 argument, for e.g. Keisha', function() { it('returns "Hello, Keisha!"', function() { expect(greet('Keisha')).toEqual('Hello, Keisha!') }) }) })
Note: I didn't pass arrow functions to the describe and it blocks since Mocha discourages its use. It says "... Their lexical binding of the this value makes them unable to access the Mocha context, and statements like this.timeout(1000); will not work inside an arrow function.".
Before we can run the tests we also need to install babel-register. It allows us to use Babel through the require hook. Learn more here.
$ npm i -D babel-register
Now, to run the tests we type:
$ ./node_modules/.bin/mocha --compilers js:babel-register
which gives us
greet with no arguments ✓ returns "Hello, world!" with 1 argument, for e.g. Keisha ✓ returns "Hello, Keisha!" 2 passing (9ms)
All good. Finally let's make this easier to run by adding it to our scripts property in our package.json.
{ ... "scripts": { ... "test": "mocha --compilers js:babel-register" } ... }
Simply type npm run test or npm test to run the tests as before.
Pass any extra command-line options to mocha as follows npm test -- [extra-options]. For e.g. to watch files for changes and rerun the tests type npm test -- --watch. Learn about all the options here (though I've found that ./node_modules/.bin/mocha --help is better to use since it has the relevant options for your version of the installation).
Happy hacking :).