If before starting Mocha you want to first run some boot code in a file that uses the syntax of the ECMAScript 2015 module (i.e. import instead of require )
- Create the following files:
./setupBabel.js (for bootstrap Babel transpiler)
require('babel-polyfill'); require('babel-register');
./setupDependencies.js (for bootstrap Chai and Sinon using ES2015 syntax)
import chai from 'chai'; import sinon from 'sinon'; global.expect = chai.expect; global.sinon = sinon;
./test/codeSpec.js (Unit Test example using the syntaxes of Chai, Sinon Stubs, Spies and ES2015, such as arrow functions, let and const )
describe('test suite', () => { it('does not modify immutable constant value', () => { const myStub = sinon.stub(); const mySpy = sinon.spy(myStub); myStub.returns({}); let val = 1; expect(val).to.not.equal(2); sinon.assert.notCalled(mySpy); }); });
- Run the following terminals to install the appropriate NPM packages:
npm install babel-polyfill babel-register chai sinon mocha
- Run the test suite using the following terminal command and flags:
mocha --require ./setupBabel.js --require ./setupDependencies.js ./test/codeSpec.js
Luke schoen
source share