Jest is a testing framework for JavaScript that includes both a test-runner and assertion functions in one package.
it()
functionEvery Jest test begins with the it()
function, which accepts two required arguments and one optional argument:
Each it()
function call will produce a separate line in the testing report. In order for a given test to pass, the test callback must run without throwing errors or failed expect()
assertions.
The it()
function is an alias for test()
.
it('test description', () => {// testing logic and assertions go here...}, timeout)
Jest will automatically pass a test that it perceives to have no expect()
assertions or errors. As a result, false positives are likely to occur when naively testing code with asynchronous functionality.
To ensure that Jest waits for asynchronous assertions to be made before marking a test as complete, there are two asynchronous patterns that Jest supports, each with its own syntax for testing:
done()
parameter function.async
/await
keywords.When testing asynchronous code that uses a callback to deliver a response, the it()
function argument should accept the done()
callback function as a parameter. Jest will wait for done()
to be called before marking a test as complete.
You should execute done()
immediately after expect()
assertions have been made within a try
block and then again within a catch
block to display any thrown error messages in the output log.
it('tests async code: callbacks', (done)=>{//actasyncFunc(input, response => {//assertionstry {expect(response).toBeDefined();done();} catch (error) {done(error);}});}
When testing asynchronous code that returns a Promise, you must await
the Promise and the callback passed to it()
function must be marked as async
.
it('tests promises', async () => {//arrangeconst expectedValue = 'data';//actconst actualValue = await asyncFunc(input);//assertionsexpect(actualValue).toBe(expectedValue);});
jest.fn()
The Jest library provides the jest.fn()
function for creating a “mock” function.
jest.fn()
to define the mock function’s behavior and return value. .mockReturnValueOnce()
.expect()
API.const mockFunction = jest.fn(() => {return 'hello';});expect(mockFunction()).toBe('hello');mockFunction.mockReturnValueOnce('goodbye');expect(mockFunction()).toBe('goodbye');expect(mockFunction()).toBe('hello');expect(mockFunction).toHaveBeenCalledTimes(3);
jest.mock()
When mocking entire modules, mock implementations of the module should be created in a __mocks__/ folder adjacent to the file being mocked.
In the test files, the jest.mock()
method may be used. It accepts a path to the file where the module to be mocked is defined and replaces the actual module with the version defined in the __mocks__/ folder.
The file to be mocked must be imported before it can be mocked with jest.mock()
.
// ../utils/utilities.jsexport const someUtil = () => 'hello';// ../utils/__mocks__/utilities.jsexport const someUtil = jest.fn(() => 'goodbye');// myTest.test.jsimport { someUtil } from './utils/utilities';jest.mock('./utils/utilities');it('uses a mock function', () => {expect(someUtil()).toBe('goodbye');});