Jest Note
Was with React, but separated now.
Basic#
- @2021-09-30
Jest
was for React andMocha
for Node.js- All
console.log
will be shown at the end of file, together.console.log
will increase ~60ms execution time. - Mock Functions called in
beforeEach()
in Setup and Teardown will addFUN.mock.calls.length
of eachtest()
, not accumulative(the accumulative one isFUN.mock.invocationCallOrder
. but bybeforeAll()
will not(no effect). Thejest.fn()
called outsidetest()
will haveundefined
asmock.result[0].value
. - This video is not clipped, worse than the official doc.
Expected errors#
- @2021-10-04
- Jest:
toThrow()
method acceptregex|string|Error|<error class>
. When passing astring
, it will only check if "error message includes the substring". To make it a exact match, useError(<string>)
. More detailed on Official Doc - Jest: suppressing
console.warn
jest.spyOn(console, 'warn').mockImplementation(() => {});
(from answer onSO
), official doc - Jest: My jest didn't throw error because in
catch(error){}
I threwError
, noterror
. To avoid this "invisible" typo error, always prefer usingerr
. (90min)
Get process.env
#
- @2022-04-16
- imported modules and variables defined in the modules won't have
process.env
set. (all fields are undefined) - imported functions call accessing
process.env
will load theprocess.env
correctly.