The chai testing framework works hand in hand with mocha. It is configured by default with following plugins:
- chai-as-promised → adds support for asserting promises and async code.
- chai-asserttype → adds support for asserting types.
- chai-equal-jsx → adds
equalJSXandincludeJSXassetions. - chai-spies → adds spy ability.
- chai-string → help with common string comparison assertions.
- dirty-chai → provides a function-call form for chai property getter assertions.
mai-chai also includes configuration files which will be written into
the ./test folder of the including project:
mocha.opts→ registers Babel as the compiler and requires the./test/test-helper.jsfile in every test.test-helper.js→ configures the globaldocument,windowandnavigatorobjects.
And for Wallaby.js users, mai-chai comes with a default configuration
file.
mai-chai follows these conventions:
./src→ source code under test../src.test→ source code for the tests../test→ mocha configuration files.
First install mai-chai:
npm install mai-chai --save-devThis will also install mocha, chai, chai-as-promised, chai-asserttype,
chai-equal-jsx, chai-spies, chai-string and dirty-chai.
Import expect from mai-chai, as you would from chai:
import {expect} from 'mai-chai';and then write your BDD assertions with methods rather than properties, such as:
expect (true).to.be.true ();
foo.should.be.ok ();or use the various assertions added by chai-string and spy on methods with chai-spies.
mai-chai provides a default Wallaby.js configuration file. If you
stick to the conventions of mai-chai no other setup or configuration
is required. Just point Wallaby.js to wallaby.conf.js.
Stringassertions are provided bychai-string.ReactElementcomparisons are provided bychai-equal-jsx.
See also Algolia's
react-element-to-jsx-stringwhich is used bychai-equal-jsxand provides the logic needed to convert aReactElementto the corresponding JSX string.
clock()→ perf, starts a high resolution timer which can be used to measure performance; it returns an opaque object.clock(perf)→ ms, returns the number of milliseconds elapsed.