Jest Note
Was with React, but separated now.
Basic#
- @2021-09-30
Jestwas for React andMochafor Node.js- All
console.logwill be shown at the end of file, together.console.logwill increase ~60ms execution time. - Mock Functions called in
beforeEach()in Setup and Teardown will addFUN.mock.calls.lengthof eachtest(), not accumulative(the accumulative one isFUN.mock.invocationCallOrder. but bybeforeAll()will not(no effect). Thejest.fn()called outsidetest()will haveundefinedasmock.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.warnjest.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.envset. (all fields are undefined) - imported functions call accessing
process.envwill load theprocess.envcorrectly.