Categories
JavaScript Answers

How to mock a dependency’s constructor with Jest?

To mock a dependency’s constructor with Jest, we can call jest.mock.

For instance, we write

import * as AWS from 'aws-sdk';

jest.mock('aws-sdk', () => {
  return {
    CloudWatch: jest.fn().mockImplementation(() => {
      return {}
    })
  }
});

test('AWS.CloudWatch is called', () => {
  new AWS.CloudWatch();
  expect(AWS.CloudWatch).toHaveBeenCalledTimes(1);
});

to mock the CloudWatch constructor with a function that returns an empty object.

We call jest.fn to return a mocked function and we mock the return value of the function with mockImplementation.

Then we can use the AWS.CloudWatch constructor to run the mocked version of the constructor.

We still have to import the real dependency with

import * as AWS from 'aws-sdk';

before we can mock AWS.CloudWatch.

Categories
JavaScript Answers

How to run Jest tests only for current folder?

Sometimes, we want to run Jest tests only for current folder.

In this article, we’ll look at how to run Jest tests only for current folder.

How to run Jest tests only for current folder?

To run Jest tests only for current folder, we can run jest with the path of the folder with the tests we want to run.

For instance, we run

npm test dev/app

to run the tests in the dev/app folder given that the test script runs jest.

We can also set the testPathPattern option to the folder with the tests we want to run.

For instance, we run

jest --watchAll --testPathPattern=dev/app

to run the tests in dev/app.

Conclusion

To run Jest tests only for current folder, we can run jest with the path of the folder with the tests we want to run.

Categories
JavaScript Answers

How to mock platform detection in Jest and React Native?

Sometimes, we want to mock platform detection in Jest and React Native.

In this article, we’ll look at how to mock platform detection in Jest and React Native.

How to mock platform detection in Jest and React Native?

To mock platform detection in Jest and React Native, we can call jest.mock to mock the Platform module.

For instance, we write:

jest.mock('Platform', () => {
  const Platform = require.requireActual('Platform');
  Platform.OS = 'android';
  return Platform;
});

to call jest.mock with 'Platform and a function that return the Platform object.

Before we return it, we set the Platform.OS property to 'android' so Platform.OS would be 'android' in our tests.

Conclusion

To mock platform detection in Jest and React Native, we can call jest.mock to mock the Platform module.

Categories
JavaScript Answers

How to mock React custom hook returned value with Jest?

To mock React custom hook returned value with Jest, we can call jest.mock.

For instance, we write:

jest.mock('module_name', () => ({
  useRect: () => [300, 200, jest.fn()]
}));

to call jest.mock with the module name and the function to mock the useRect hook with a function that returns the mocked values of the hook.

If we want to mock the hook once for a test, then use jest.spyOn with

import * as hooks from 'module_name';

it('a test', () => {
  jest.spyOn(hooks, 'useRect').mockImplementation(() => ([100, 200, jest.fn()]));
  // ...
});

to mock the useRect hook and return the mocked return values with mockImplementation.

We can just mock the return values since hooks are pure functions so they don’t commit side effects.

Categories
JavaScript Answers TypeScript Answers

How to set the data type of Axios mock using Jest TypeScript?

Sometimes, we want to set the data type of Axios mock using Jest TypeScript.

In this article, we’ll look at how to set the data type of Axios mock using Jest TypeScript.

How to set the data type of Axios mock using Jest TypeScript?

To set the data type of Axios mock using Jest TypeScript, we can use the jest.Mocked<typeof axios> type.

For instance, we write

import axios from 'axios';
jest.mock('axios');
const mockedAxios = axios as jest.Mocked<typeof axios>;

//...

mockedAxios.get.mockRejectedValue('error');
mockedAxios.get.mockResolvedValue({ data: {} });

to set the type of mockedAxios to jest.Mocked<typeof axios> with as.

We mock Axios with jest.mock('axios').

And then we can call mockedAxios.get.mockRejectedValue or mockedAxios.get.mockResolvedValue to set the mocked resolve or reject value of the promise returned by axios.get.

Conclusion

To set the data type of Axios mock using Jest TypeScript, we can use the jest.Mocked<typeof axios> type.