Testing With javascript
Pre Requisite
1. Node installed
Setting up project
- Creating project directory
- run command
npm init
will generate package.json file with specified configuration - Installing mocha
npm install mocha
npm install chai
- To run mocha via npm need change test script in package.json to
"test": "mocha --recursive"
Framework / Libraries used:
- Mocha – the core framework: it provides common testing functions including describe and it and the main function that runs tests.
- Chai – the library with many assertions. It allows to use a lot of different assertions, for now we need only assert.equal.
- Sinon – a library to spy over functions, emulate built-in functions and more, we’ll need it much later.
Notes:
-
Mocha : Test spec has three main building blocks
describe("title", function() { ... })
it works like a suit or tests container. using [Mocha] for itit("use case description", function() { ... })
perticular use case or A test.assert
to check if works as expected. using [Chai] for it- In Async js testing -> this context in not accessible in arrow functions in ES6 hence mocha suggest we should not use arrow function.
-
Running tests
- run command
mocha
will find automatically all tests under test directory but not nested - run
mocha --recursive
to run nested directories mocha "test/dirName/*.spec.js"
to specify custom path
- run command
nvm - node version manager to work with multiple version of node