mochaccino 1.2.0
United js test tools
Mocha is great but having asserts, assert plugins and spies spread out as separate projects is a bit frustrating, especially when you have to jump around all of the different documentations or setup test environment.
Mochaccino goals:
- documentation for expectations/spies/tests in ONE place
- providing all of the mocha important components together already setup
- shallow learning curve, quick to start and work with
Quick start
npm install --save-dev mochaccino mocha babel-core babel-register babel-preset-es2015
add the following to your package.json
:
{
...
"scripts": {
"test": "mocha --compilers js:babel-register"
}
"babel": {
"presets": ["es2015"]
}
...
}
create a directory test
with a file index.js
:
; ;
and then just run in the command line:
npm test
Expectations
- expect(a).toBe(b)
- expect(a).toEqual(b)
- expect(a).toBeTruthy()
- expect(a).toBeFalsy()
- expect(a).toBeDefined()
- expect(a).toBeUndefined()
- expect(a).toBeNull()
- expect(a).toBeLessThan(b)
- expect(a).toBeGreaterThan(b)
- expect([1,2]).toContain(1)
- expect(f).toThrow()
- expect(f).toThrowError(ErrorType)
- expect(s).toMatch(regexp)
for spies:
- expect(s).toHaveBeenCalled()
- expect(s).toHaveBeenCalledWith(a1, a2)
- expect(s).toHaveBeenCalledTimes(n)
each expectation can be combined with not
flag:
not
Spies
;
spy(obj, 'funcName'); - spy on objects
;obj;;
spy(); - create standalone spy
let s = ;;;
spy(obj, 'funcName').and.callFake(func); - spy on object and call given function instead original one
and;;;
spy(obj, 'funcName').restore(); - restore original behaviour by removing the spy
;objfuncName;
spy(obj, 'funcName').and.callThrough(); - spy on object but call original method
spy(obj, 'funcName').and.returnValue(5); - spy will return a value when called
DOM testing
Mochaccino dom helper lets you run a dom implementation (jsdom) inside the node enviroment. No browser required.
; ;
Unfortunately approach above is not recommended for testing React
component as React's source code makes several assumptions about
the environment it is running in, and one of them is that the
document
that is found at "require time" is going to be
the one and only document it ever needs to worry about.
As a result, this type of "reloading" ends up causing more pain
than it prevents.
In that case document
needs to be created only once before
requiring React. This can be done by:
mocha --require mochaccino/dom-setup --compilers js:babel-register
and then "clearing" existing document between the tests to make sure that components do not leak:
; ;