Categories
JavaScript Answers

How to clean up after all tests have run with Jest?

Sometimes, we want to clean up after all tests have run with Jest.

In this article, we’ll look at how to clean up after all tests have run with Jest.

How to clean up after all tests have run with Jest?

To clean up after all tests have run with Jest, we can add the setupFilesAfterEnv config property in our Jest config.

For instance, in package.json, we add

{
  // ...
  "jest": {
    "setupFilesAfterEnv": ["<rootDir>/setupTests.js"]
  }
}

to run setupTests.js before each test.

Then in setupTests.js, we add

global.beforeEach(() => {
  //...
});

global.afterEach(() => {
  //...
});

to run the beforeEach hook before each test and afterEach hook after each test.

Conclusion

To clean up after all tests have run with Jest, we can add the setupFilesAfterEnv config property in our Jest config.

Categories
JavaScript Answers

How to run global test setup before each test in Jest?

Sometimes, we want to run global test setup before each test in Jest.

In this article, we’ll look at how to run global test setup before each test in Jest.

How to run global test setup before each test in Jest?

To run global test setup before each test in Jest, we can add the setupFilesAfterEnv config property in our Jest config.

For instance, in package.json, we add

{
  // ...
  "jest": {
    "setupFilesAfterEnv": ["<rootDir>/setupTests.js"]
  }
}

to run setupTests.js before each test.

Then in setupTests.js, we add

global.beforeEach(() => {
  //...
});

global.afterEach(() => {
  //...
});

to run the beforeEach hook before each test and afterEach hook after each test.

Conclusion

To run global test setup before each test in Jest, we can add the setupFilesAfterEnv config property in our Jest config.

Categories
JavaScript Answers

How to fix “ReferenceError: document is not defined” error when trying to test a create-react-app project with Jest?

Sometimes, we want to fix "ReferenceError: document is not defined" error when trying to test a create-react-app project with Jest.

In this article, we’ll look at how to fix "ReferenceError: document is not defined" error when trying to test a create-react-app project with Jest.

How to fix "ReferenceError: document is not defined" error when trying to test a create-react-app project with Jest?

To fix "ReferenceError: document is not defined" error when trying to test a create-react-app project with Jest, we can call the shallow function to shallow mount our component for testing.

For instance, we write

import {
  shallow
} from 'enzyme';

it('renders without crashing', () => {
  shallow(< App / >);
});

to shallow mount the App component with shallow with Enzyme.

Conclusion

To fix "ReferenceError: document is not defined" error when trying to test a create-react-app project with Jest, we can call the shallow function to shallow mount our component for testing.

Categories
JavaScript Answers

How to test components using Date objects produce different snapshots in different timezones with Jest?

Sometimes, we want to test components using Date objects produce different snapshots in different timezones with Jest.

In this article, we’ll look at how to test components using Date objects produce different snapshots in different timezones with Jest.

How to test components using Date objects produce different snapshots in different timezones with Jest?

To test components using Date objects produce different snapshots in different timezones with Jest, we can use the timezone-mock package.

To install it, we run npm i timezone-mock.

Then we write

import timezoneMock from 'timezone-mock';

describe('when in Pacific timezone', () => {
  beforeAll(() => {
    timezoneMock.register('US/Pacific');
  });

  afterAll(() => {
    timezoneMock.unregister();
  });

  // ...
})

to call timezoneMock.register with the time zone we want to run our tests in.

And then we call timezoneMock.unregister to restore the test environment to its original time zone.

Conclusion

To test components using Date objects produce different snapshots in different timezones with Jest, we can use the timezone-mock package.

Categories
JavaScript Answers

How to mock the same function twice with different arguments with Jest?

Sometimes, we want to mock the same function twice with different arguments with Jest.

In this article, we’ll look at how to mock the same function twice with different arguments with Jest.

How to mock the same function twice with different arguments with Jest?

To mock the same function twice with different arguments with Jest, we can call mockReturnValueOnce.

For instance, we write:

myMock
  .mockReturnValueOnce(100)
  .mockReturnValueOnce('x')
  .mockReturnValue(true);

to call mockReturnValueOnce on the mocked function myMock to return the values we want.

If we just want to check the argument that a mocked function is called with, we use

expect(mock).toHaveBeenNthCalledWith(1, 'foo');

to check the argument at position 1 is called with 'foo with toHaveBeenNthCalledWith.

Conclusion

To mock the same function twice with different arguments with Jest, we can call mockReturnValueOnce.