Out of the box Jest provides various tools that you will find useful.
Expect is a powerful assertion library with various built-in matchers:
expect(1 + 1).toBe(2); expect(foo).toEqual(bar); expect(mock).toHaveBeenCalledTimes(1); expect(mock).toHaveBeenCalledWith(arg1, arg2, ...);
For simulating the behavior of a real object:
const mockCounter = jest.fn(); mockCounter .mockReturnValueOnce(1) .mockReturnValueOnce(2) .mockReturnValueOnce(3); console.log(mockCounter()); // 1 console.log(mockCounter()); // 2 console.log(mockCounter()); // 3
On test failure it will generate useful error messages:
Storing the previous output of a function for future comparison.
Inbuilt code coverage by supplying
--coverage as an argument:
Jest comes with an interactive command line which can help you control which tests you wish to test.
There are various testing libraries in existence already. Why would a developer choose Jest, over something else - such as Mocha, or Jasmine?
On an average day, the key distinctions between Jest and $otherFramework are:
expect(...).toBe(...)creates human readable diffs
As your codebase matures, and your codebase and tests increase in size, some benefits between Jest and $otherFramework are: