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.

Categories
JavaScript Answers

How to run Jest tests sequentially?

Sometimes, we want to run Jest tests sequentially.

In this article, we’ll look at how to run Jest tests sequentially.

How to run Jest tests sequentially?

To run Jest tests sequentially, we can use the --runInBand option.

We can run all tests sequentially with

jest --runInBand

Conclusion

To run Jest tests sequentially, we can use the --runInBand option.

Categories
JavaScript Answers

How to write async tests that expect toThrow with Jest?

Sometimes, we want to write async tests that expect toThrow with Jest.

In this article, we’ll look at how to write async tests that expect toThrow with Jest.

How to write async tests that expect toThrow with Jest?

To write async tests that expect toThrow with Jest, we can put await before expect and call toThrow.

For instance, we write

it('should test async errors', async () => {
  await expect(failingAsyncTest())
    .rejects
    .toThrow('I should fail');
});

to call failingAsyncTest in the test function.

We use await and rejects to get the rejected value if any and call thThrow to check what error message is given with the promise rejection.

Conclusion

To write async tests that expect toThrow with Jest, we can put await before expect and call toThrow.