Tests

  • Unit tests

  • Integration tests

  • System tests

  • test

  • test suite

  • test plan

Describe()
 it()

'use strict';

const constante = true;

describe('Dumb test', () => {
  it('Should return true', () => {
    // test code here
  });
});

Assertions

'use strict';

const constante = true;

function assert(cond, msg) {
  if (!cond) {
    throw new Error(msg);
  }
}


describe('Dumb test', () => {
  it('Should return true', () => {
    assert(constante === false, 'Should be true');
  });
});

Set test pending

'use strict';

const constante = true;


describe('Dumb test', () => {
  xit('Should return true', () => {
    assert(constante === false, 'Should be true');
  });
});

Run only one test

'use strict';

const constante = true;

describe('Dumb test', () => {
  it('Should not run', () => {
    assert(constante === false, 'Should be false');
  });

  it.only('Should run', () => {
     assert(constante === true, 'Should be true');
  });
});
  • Before / after

  • beforeEach / afterEach

Async: done()

'use strict';

const constante = true;

function assert(cond, msg) {
  if (!cond) {
    throw new Error(msg);
  }
}

describe('Dumb test', () => {
  it('Should return true', (done) => {
    setTimeout(() => {
      assert(constante === true);
      done();
    }, 300);
  });
});
'use strict'

const constante = true

function assert(cond, msg) {
  if (!cond) {
    throw new Error(msg)
  }
}

describe('Dumb test', () => {
  it('Should return true', (done) => {
    new Promise((resolve, reject) => {
      setTimeout(() => {
        try {
          assert(constante === true)
          resolve()
        } catch (err) {
          reject(err)
        }
      }, 300)
    })
    .then(() => done(), () => done())
  })
})

Async: Promise

'use strict'

const constante = true

function assert(cond, msg) {
  if (!cond) {
    throw new Error(msg)
  }
}

describe('Dumb test', () => {
  it('Should return true', () => {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        try {
          assert(constante === true)
          resolve()
        } catch (err) {
          reject(err)
        }
      }, 300)
    })
  })
})

Assertions (2)

'use strict'

const constante = true

const should = require('should')

describe('Dumb test', () => {
  it('Should return true', () => {
    constante.should.be.true
  })
})
'use strict'

const constante = true
const fn = function () { return 42;}

const should = require('should')

describe('Dumb test', () => {
  it('Should return true', () => {
    constante.should.be.true
    fn.should.be.a('function')
  })
})
'use strict'

const constante = true
const fn = function () { return 42;}

const should = require('should')

describe('Dumb test', () => {
  it('Should return true', () => {
    constante.should.be.true
    fn.should.not.be.an('undefined').and.should.be.a('function')
  })
})

Mock: spy / stub

sinonjs

spy

called

calledOnce

calledTwice

calledThrice

restore

stub

  • idem spy +
  • onCall(int)
  • withArgs(whatever)
  • returns(whatever)

Tests

By Stanislas Ormières