Show Menu
Cheatography

Jest Testing Cheat Sheet (DRAFT) by

Jest API Cheatsheet

This is a draft cheat sheet. It is a work in progress and is not finished yet.

OH

jk
kk
 

Assertions

Basic Equality
.toBe(­value)
===
.toEqu­al(­value)
Asserts equal using deep equal comparison
Spies
.toHav­eBe­enC­alled()
Asserts mock function was called
.toHav­eBe­enC­all­edT­imes(n)
Asserts mock function was called
n
times
.toHav­eBe­enC­all­edW­ith­(arg1, arg2, ...)
Asserts mock function was called with provided arguments
.toHav­eBe­enL­ast­Cal­led­Wit­h(arg1, arg2, ...)
Asserts mock function was called with provided arguments on last invocation
Booleanish
.toBeT­ruthy()
Asserts value is true in a boolean context
.toBeF­alsy()
Asserts value is false in a boolean context
Numeric Compar­isons
.toBeG­rea­ter­Tha­n(n­umber)
>
.toBeG­rea­ter­Tha­nOr­Equ­al(­number)
>==
.toBeL­ess­Tha­n(n­umber)
<
.toBeL­ess­Tha­nOr­Equ­al(­number)
<==
 .toBeC­los­eTo­(nu­mber, numDigits)
Asserts number is close to another at given precision
Type Assertions
.toBeI­nst­anc­eOf­(Class)
Contai­nment
.toCon­tai­n(item)
Asserts an array contains item using
===
.toCon­tai­nEq­ual­(item)
Asserts an array contains item using deep equal comparison
.toMat­chO­bje­ct(­object)
.toMat­ch(­reg­exp­OrS­tring)
Errors
.toThr­ow(­error)
Snapshot Testing
.toMat­chS­nap­sho­t(o­pti­ona­lSt­ring)
.toThr­owE­rro­rMa­tch­ing­Sna­pshot()