Skip to content

Jest Note

Was with React, but separated now.

Basic#

  • @2021-09-30
  • Jest was for React and Mocha 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 add FUN.mock.calls.length of each test(), not accumulative(the accumulative one is FUN.mock.invocationCallOrder. but by beforeAll() will not(no effect). The jest.fn() called outside test() will have undefined as mock.result[0].value.
  • This video is not clipped, worse than the official doc.

Expected errors#

  • @2021-10-04
  • Jest: toThrow() method accept regex|string|Error|<error class>. When passing a string, it will only check if "error message includes the substring". To make it a exact match, use Error(<string>). More detailed on Official Doc
  • Jest: suppressing console.warn jest.spyOn(console, 'warn').mockImplementation(() => {}); (from answer on SO), official doc
  • Jest: My jest didn't throw error because in catch(error){} I threw Error, not error. To avoid this "invisible" typo error, always prefer using err. (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 the process.env correctly.

Timeout#