Categories
JavaScript Answers

How to clear the Jest cache?

Sometimes, we want to clear the Jest cache.

In this article, we’ll look at how to clear the Jest cache.

How to clear the Jest cache?

To clear the Jest cache, we can run jest with the --clearCache option.

We run

jest --clearCache

to clear the Jest cache.

Or we can put it in a script in package.json with

{
  //  ...
  "scripts:" {
    "clearJest": "jest --clearCache"
  }
  // ...
}

and run npm run clearJest

Conclusion

To clear the Jest cache, we can run jest with the --clearCache option.

Categories
JavaScript Answers

How to get the code coverage report using Jest?

Sometimes, we want to get the code coverage report using Jest.

In this article, we’ll look at how to get the code coverage report using Jest.

How to get the code coverage report using Jest?

To get the code coverage report using Jest, we run jest with the coverage option.

For instance, we run

npx jest --coverage

to run Jests tests and generate a code coverage report after that.

Conclusion

To get the code coverage report using Jest, we run jest with the coverage option.

Categories
JavaScript Answers

How to test process.env with Jest?

Sometimes, we want to test process.env with Jest.

In this article, we’ll look at how to test process.env with Jest.

How to test process.env with Jest?

To test process.env with Jest, we can set the process.env properties before out tests.

And then we can run the test code after that.

For instance, we write

describe('environmental variables', () => {
  const OLD_ENV = process.env;

  beforeEach(() => {
    jest.resetModules()
    process.env = {
      ...OLD_ENV
    };
  });

  afterAll(() => {
    process.env = OLD_ENV;
  });

  test('will receive process.env variables', () => {
    process.env.NODE_ENV = 'dev';
    process.env.PROXY_PREFIX = '/new-prefix/';
    process.env.API_URL = 'https://new-api.com/';
    process.env.APP_PORT = '7080';
    process.env.USE_PROXY = 'false';

    // tests
  });
});

to reset process.env to the old values in the beforeEach callback.

And after each test, we set process.env to OLD_ENV which has the original process.env values.

Next, we set the process.env values we want to run our test with within the test callback.

And then we run our test code after they’re set.

Conclusion

To test process.env with Jest, we can set the process.env properties before out tests.

And then we can run the test code after that.

Categories
JavaScript Answers

How to test for the non-existence of an element using Jest and react-testing-library?

Sometimes, we want to test for the non-existence of an element using Jest and react-testing-library.

In this article, we’ll look at how to test for the non-existence of an element using Jest and react-testing-library.

How to test for the non-existence of an element using Jest and react-testing-library?

To test for the non-existence of an element using Jest and react-testing-library, we can use the the not.toBeInTheDocument method.

For instance, we write:

import '@testing-library/jest-dom/extend-expect'
// ...
const submitButton = screen.queryByText('submit')
expect(submitButton).not.toBeInTheDocument()

to call screen.queryByText to select the element by text.

Then we call expect(submitButton).not.toBeInTheDocument to check that the element with text 'submit' isn’t in the document object.

Conclusion

To test for the non-existence of an element using Jest and react-testing-library, we can use the the not.toBeInTheDocument method.

Categories
JavaScript Answers

How to set a mock date in Jest?

To set a mock date in Jest, we can use the useFakeTimers and setSysttemTime methods.

For instance, we write:

jest
  .useFakeTimers()
  .setSystemTime(new Date('2022-01-01').getTime());

to call useFakeTimers to let us mock timers.

Then we call setSystemTime to set the time of the mock timer by calling it with a timestamp in milliseconds.