Referință rapidă

Ghid rapid Expect.js Assertions

Ghid rapid de referinta pentru biblioteca de asertiuni Expect.js: instalare, asertiuni de baza, inlantuire de asertiuni si spies cu verificare.

· Publicat

Expect este o biblioteca de asertiuni pentru JavaScript cu o sintaxa clara si suport incorporat pentru spies.

Utilizare si instalare (Usage)

npm install --save-dev expect
// Utilizare module ES6
import expect, { createSpy, spyOn, isSpy } from 'expect'
// Utilizare module CommonJS
var expect = require('expect')
var createSpy = expect.createSpy
var spyOn = expect.spyOn
var isSpy = expect.isSpy

Lista asertiunilor (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)

Asertiuni inlantuite (Chained Assertions)

Asertiunile pot fi inlantuite fluent:

expect(3.14)
  .toExist()
  .toBeLessThan(4)
  .toBeGreaterThan(3)

Spioni (Spies)

const video = {
  play: function () { /* ... */ }
}
spy = expect.spyOn(video, 'play')
spy = expect.spyOn(video, 'play')
  .andCallThrough()      // Transmite apelul functiei originale
  .andCall(fn)
  .andThrow(exception)
  .andReturn(value)

Asertiuni pe 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')

Referinte conexe

Subiecte conexe