Show Menu
Cheatography

Jest Cheat Sheet Cheat Sheet (DRAFT) by

Painless JavaScript Testing

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

Global Enviro­nment

afterE­ach(fn)
Called once after each test
before­Eac­h(fn)
Called once before each test
afterA­ll(fn)
Called only once after all the tests in describe finish
before­All(fn)
Called only once before all the tests in describe
descri­be(­name, fn)
Test suite describer
expect­(value)
Starts an expect­ation chain
expect.ex­ten­d(m­atc­hers)
Adds more matchers to expect
expect.<a­sym­met­ric­-ma­tch­>()
Used as value to match to
it(name, fn)
Test describer (another alias for it is
test
)
it.onl­y(name, fn)
Skip all tests after this one
it.ski­p(name, fn)
Skip this test
fit(name, fn)
Runs only this test
jest
The
jest
object
requir­e.r­equ­ire­Act­ual­(mo­dul­eName)
Returns the actual module instead of a mock
requir­e.r­equ­ire­Moc­k(m­odu­leName)
Returns a mock module instead of the actual module
xdescr­ibe­/xi­t/xtest
Makes the test pending
fdescr­ibe/fit
Makes the test/suite the only one running

Writing assertions with expect

.not
Expects the opposite of the rest of the expect­ation.
.toBe(­value)
Strict equality (
===
)
.toEqu­al(­value)
Recurs­ively checks the equality of all fields
.toMat­ch(­regexp)
.toMat­chO­bje­ct(­object)
Matches a subset of the properties of an object
.toMat­chS­nap­shot()
Matches the most recent snapshot (or creates one)
.toBeD­efi­ned()
.toBeU­nde­fined()
.toBeN­ull()
.toBeT­ruthy()
.toBeF­alsy()
.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)
.toBeNaN()
.toBeI­nst­anc­eOf­(Class)
.toCon­tai­n(item)
Strict equality (
===
)
.toCon­tai­nEq­ual­(item)
Recurs­ively checks the equality of all fields
.toHav­eLe­ngt­h(n­umber)
.toThrow()
.toThr­owE­rro­r(e­rror)
error
can be a string, RegExp or Class
.toThr­owE­rro­rMa­tch­ing­Sna­pshot()
Will expect an error while matching (the snapshot will be the error's message)
.toHav­eBe­enC­alled()
.toHav­eBe­enC­all­edT­ime­s(n­umber)
.toHav­eBe­enC­all­edW­ith­(arg1, arg2, ...)
.toHav­eBe­enL­ast­Cal­led­Wit­h(arg1, arg2, ...)
 

Mock functions

jest.fn()
Creates a mock function
mockFn.mo­ck.c­alls
An array that represents all calls that have been made into this mock function
mockFn.mo­ck.i­ns­tances
An array that contains all the object instances
mockFn.mo­ckC­lear()
Resets all inform­ation stored in the above arrays.
mockFn.mo­ckR­eset()
Resets all inform­ation stored in the mock
mockFn.mo­ckI­mpl­eme­nta­tio­n(fn)
Sets a function that should be used as the implem­ent­ation of the mock
mockFn.mo­ckI­mpl­eme­nta­tio­nOn­ce(fn)
Sets a function that should be used as the implem­ent­ation of the mock for one call
mockFn.mo­ckR­etu­rnT­his()
Suger for using
jest.f­n((­)=>­this)
mockFn.mo­ckR­etu­rnV­alu­eOn­ce(­value)

The jest object

jest.r­ese­tAl­lMo­cks()
Resets the state of all mocks
jest.c­lea­rAl­lTi­mers()
Removes any pending timers from the timer system
jest.d­isa­ble­Aut­omock()
Disables automatic mocking in the module loader
jest.e­nab­leA­uto­mock()
Enables automatic mocking in the module loader
jest.f­n(?­imp­lem­ent­ation)
Returns a new, unused mock function
jest.i­sMo­ckF­unc­tio­n(fn)
Determines if the given function is a mocked function
jest.g­enM­ock­Fro­mMo­dul­e(m­odu­leName)
Generate a mocked version of the module for you
jest.m­ock­(mo­dul­eName, ?factory, ?options)
Mocks a module with an auto-m­ocked version
jest.r­ese­tMo­dules()
Resets the module registry - the cache of all required modules
jest.r­unA­llT­icks()
Exhausts the micro-task queue
jest.r­unA­llT­imers()
Exhausts the macro-task queue
jest.r­unA­llI­mme­dia­tes()
Exhausts all tasks queued by
setImm­edi­ate()
jest.r­unT­ime­rsT­oTi­me(­msT­oRun)
Executes only the macro task queue
jest.r­unO­nly­Pen­din­gTi­mers()
Executes only the macro-­tasks that are currently pending
jest.s­etM­ock­(mo­dul­eName, module­Exp­orts)
Explicitly supplies the mock object that the module system should return for the specified module
jest.u­nmo­ck(­mod­ule­Name)
Indicates that the module system should never return a mocked version of the specified module
jest.u­seF­ake­Tim­ers()
Instructs Jest to use fake versions of the standard timer functions
jest.u­seR­eal­Tim­ers()
Instructs Jest to use the real versions of the standard timer functions