Mocha is a test framework for NodeJS. Installation npm install --save-dev mocha Example In test folder add file.
const assert = require('assert')
describe('Array', () => {
it('should return -1 when the value is not present', () => {
assert.equal([1, 2, 3].indexOf(4), -1)
})
})
Add script in package.json
"scripts": {
"mocha": "mocha"
},
Start test with npm run mocha . Output
Array
✔ should return -1 when the value is not present
In this example mocha uses built-in node assertion , but we can use any, like chai or expect Hooks
describe('hooks', function () {
before('some description', function () {
console.log('runs once before the first test in this block')
})
after('some description', function () {
console.log('runs once after the last test in this block')
})
beforeEach('some description', function () {
console.log('runs before each test in this block')
})
afterEach('some description', function () {
console.log('runs after each test in this block')
})
it('test hooks1', () => {
assert.equal(1, 1)
assert.equal(2, 2)
})
it('test hooks2', () => {
assert.equal(1, 1)
assert.equal(2, 2)
})
})
Output
hooks
runs once before the first test in this block
runs before each test in this block
✔ test hooks1
runs after each test in this block
runs before each test in this block
✔ test hooks2
runs after each test in this block
runs once after the last test in this block
Only Only 2nd test will run.
describe('hooks', function () {
it('test1', () => {
assert.equal(1, 1)
})
it.only('test2', () => {
assert.equal(1, 1)
})
})
describe.only('set 1', function () {
it('test1', () => {
assert.equal(1, 1)
})
it('test2', () => {
assert.equal(1, 1)
})
})
describe('set 2', function () {
it('test1', () => {
assert.equal(1, 1)
})
it('test2', () => {
assert.equal(1, 1)
})
})
Skip
describe('skip', () => {
it('test1', () => {
assert.equal(1, 1)
})
it.skip('test2', () => {
assert.equal(1, 1)
})
})
Result
skip
✔ test1
- test2
1 passing (3ms)
1 pending
Retry Make several retries until success.
describe('retry', function () {
this.retries(2)
it('test', function () {
const num = Math.random()
console.log(num)
assert.equal(num > 0.9, true)
})
})
or
describe('retry', function () {
it('test', function () {
this.retries(2)
const num = Math.random()
console.log(num)
assert.equal(num > 0.9, true)
})
})
or pass it via terminal npm run mocha -- --retries 5 Output
retry
0.14763852688168244
0.21161851333340276
0.9364510602907048
✔ test
1 passing (4ms)
Dynamic tests
const assert = require('assert')
const add = args => args.reduce((prev, curr) => prev + curr, 0)
describe.only('add()', function () {
const tests = [
{ args: [1, 2], expected: 3 },
{ args: [1, 2, 3], expected: 6 },
{ args: [1, 2, 3, 4], expected: 10 }
]
tests.forEach(({ args, expected }) => {
it(`correctly adds ${args.length} args`, function () {
const res = add(args)
assert.strictEqual(res, expected)
})
})
})
Output
add()
✔ correctly adds 2 args
✔ correctly adds 3 args
✔ correctly adds 4 args
3 passing (3ms)
Test specific files npm run mocha "./test/myTest.test.js" or npm run mocha --file "./test/myTest.test.js" Watch npm run mocha -- --watch Not clear why we need to put additional '--' May be it somehow separates npm flags from internal mocha flags, don't know. Several flags npm run mocha --file "./test/myTest.test.js" -- --retries 5 --watch Reporter Changes test output representation npm run mocha -- --reporter nyan Output
9 -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-__,------,
53 -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-__,------,
0 -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-__| /_/
0 -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_~|_( ^ .^)
-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_ "" ""
53 passing (34ms)