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.

Categories
JavaScript Answers

What is the difference between ‘toBe’ and ‘toEqual’ in Jest?

In this article, we’ll look at what is the difference between toBe and toEqual in Jest.

What is the difference between ‘toBe’ and ‘toEqual’ in Jest?

The difference between toBe and toEqual in Jest is that toBe is used for comparing primitive values like numbers, strings, and booleans.

It uses === to do the comparison.

Objects are considered equal with toBe if they reference the same object.

toEqual is used for comparing everything else.

We can use toEqual compare objects directly for equality since their content is checked.

Conclusion

The difference between toBe and toEqual in Jest is that toBe is used for comparing primitive values like numbers, strings, and booleans.

Categories
JavaScript Answers

How to mock window.location.href with Jest and Vuejs?

Sometimes, we want to mock window.location.href with Jest and Vuejs.

In this article, we’ll look at how to mock window.location.href with Jest and Vuejs.

How to mock window.location.href with Jest and Vuejs?

To mock window.location.href with Jest and Vuejs, we can set the propert window.location property to a value.

For instance, we write

global.window = Object.create(window);
const url = "http://example.com";
Object.defineProperty(window, 'location', {
  value: {
    href: url
  }
});
expect(window.location.href).toEqual(url);

in a test function to set global.window to a copy of the existing window object with Object.create.

Then we add a value to globa.window.location property and set it to an object with the href property set to url with Object.defineProperty.

As a result, the check for window.location.href equal to url in the last line should pass.

Conclusion

To mock window.location.href with Jest and Vuejs, we can set the propert window.location property to a value.