Expect는 깔끔한 문법과 내장 Spy 기능을 제공하는 자바스크립트 단언(assertion) 라이브러리입니다.
사용법 및 설치 (Usage)
npm install --save-dev expect
// ES6 모듈 사용 방식
import expect, { createSpy, spyOn, isSpy } from 'expect'
// CommonJS 모듈 사용 방식
var expect = require('expect')
var createSpy = expect.createSpy
var spyOn = expect.spyOn
var isSpy = expect.isSpy
단언 메서드 목록 (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)
체인 단언 (Chained Assertions)
단언 메서드를 자연스러운 체인 형태로 연결하여 사용할 수 있습니다:
expect(3.14)
.toExist()
.toBeLessThan(4)
.toBeGreaterThan(3)
스파이 기능 (Spies)
const video = {
play: function () { /* ... */ }
}
spy = expect.spyOn(video, 'play')
spy = expect.spyOn(video, 'play')
.andCallThrough() // 원본 함수 구현을 그대로 호출
.andCall(fn)
.andThrow(exception)
.andReturn(value)
스파이 호출 검증 (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')