Expect is een assertiebibliotheek voor JavaScript met een heldere syntaxis en ingebouwde spy-functionaliteit.
Gebruik en installatie (Usage)
npm install --save-dev expect
// Gebruik met ES6-modules
import expect, { createSpy, spyOn, isSpy } from 'expect'
// Gebruik met CommonJS-modules
var expect = require('expect')
var createSpy = expect.createSpy
var spyOn = expect.spyOn
var isSpy = expect.isSpy
Beschikbare asserties (Assertions)
expect(x).toBe(y)
.toBe(val)
.toEqual(val)
.toThrow(err)
.toExist() // aka: toBeTruthy()
.toNotExist() // aka: toBeFalsy()
.toBeA(constructor)
.toBeA('string')
.toMatch(/expr/)
.toBeLessThan(n)
.toBeGreaterThan(n)
.toBeLessThanOrEqualTo(n)
.toBeGreaterThanOrEqualTo(n)
.toInclude(val) // aka: toContain(val)
.toExclude(val)
.toIncludeKey(key)
.toExcludeKey(key)
Gekoppelde asserties (Chained Assertions)
Asserties kunnen vloeiend aan elkaar worden gekoppeld:
expect(3.14)
.toExist()
.toBeLessThan(4)
.toBeGreaterThan(3)
Spies (Spies)
const video = {
play: function () { /* ... */ }
}
spy = expect.spyOn(video, 'play')
spy = expect.spyOn(video, 'play')
.andCallThrough() // Geeft de aanroep door aan het origineel
.andCall(fn)
.andThrow(exception)
.andReturn(value)
Asserties op Spies (Assertions on Spies)
expect(spy.calls.length).toEqual(1)
expect(spy.calls[0].context).toBe(video)
expect(spy.calls[0].arguments).toEqual([ 'some', 'args' ])
expect(spy).toHaveBeenCalled()
expect(spy).toHaveBeenCalledWith('some', 'args')