Categories
JavaScript Answers

How to reset or clear a spy in Jest?

Sometimes, we want to reset or clear a spy in Jest.

In this article, we’ll look at how to reset or clear a spy in Jest.

How to reset or clear a spy in Jest?

To reset or clear a spy in Jest, we call jest.clearAllMocks.

For instance, we write:

afterEach(() => {
  jest.clearAllMocks();
});

to call jest.clearAllMocks in the afterEach callback to clear all spies or mocks after each test is run.

Conclusion

To reset or clear a spy in Jest, we call jest.clearAllMocks.

Categories
JavaScript Answers

How to test axios in Jest?

Sometimes, we want to test axios in Jest.

In this article, we’ll look at how to test axios in Jest.

How to test axios in Jest?

To test axios in Jest, we can mock the axios dependency.

For instance, we write

import * as axios from "axios";

jest.mock("axios");

// ...

test("good response", () => {
  axios.get.mockImplementation(() => Promise.resolve({
    data: {
      //...
    }
  }));
  // ...
});

test("bad response", () => {
  axios.get.mockImplementation(() => Promise.reject({
    ...
  }));
  // ...
});

to call axios.get.mockImplementation to mock the implementation of the axios.get method with a function that returns a promise, which we passed in as the argument of mockImplementation.

We also called jest.mock("axios"); to mock the whole axios module.

Conclusion

To test axios in Jest, we can mock the axios dependency.

Categories
JavaScript Answers

How to mock console when it is used by a third-party library with Jest?

Sometimes, we want to mock console when it is used by a third-party library with Jest.

In this article, we’ll look at how to mock console when it is used by a third-party library with Jest.

How to mock console when it is used by a third-party library with Jest?

To mock console when it is used by a third-party library with Jest, we can set the global.console property to our own mock object.

For instance, we write:

global.console = {
  warn: jest.fn()
}
expect(console.warn).toBeCalled()

to set global.console to an object that mocks the console.warn method by setting warn to jest.fn.

Then we check that console.warn is called with expect(console.warn).toBeCalled().

We can also use jest.spyOn to mock console.warn with

jest.spyOn(global.console, 'warn')

since Jest 19.0.0.

Conclusion

To mock console when it is used by a third-party library with Jest, we can set the global.console property to our own mock object.

Categories
JavaScript Answers

How to check array equality ignoring element position in Jest?

Sometimes, we want to check array equality ignoring element position in Jest.

In this article, we’ll look at how to check array equality ignoring element position in Jest.

How to check array equality ignoring element position in Jest?

To check array equality ignoring element position in Jest, we can call array sort to sort the array before comparing them.

For instance,. we write:

expect(["wool", "down"].sort()).toEqual(["down", "wool"].sort());

to call sort on both arrays before comparing them with toEqual.

Conclusion

To check array equality ignoring element position in Jest, we can call array sort to sort the array before comparing them.

Categories
JavaScript Answers

How to skip one test in test file with Jest?

Sometimes, we want to skip one test in test file with Jest.

In this article, we’ll look at how to skip one test in test file with Jest.

How to skip one test in test file with Jest?

To skip one test in test file with Jest, we can call test.skip in our test code.

For instance, we write:

test('it is delicious', () => {
  expect(isDelicious()).toBeTruthy();
});

test.skip('it is not yellow', () => {
  expect(isYellow()).toBeFalsy();
});

to call test to create and run the first test.

And we call test.skip to skip the 2nd test.

Conclusion

To skip one test in test file with Jest, we can call test.skip in our test code.