Categories
JavaScript Answers

What’s the Jest equivalent to RSpec lazy evaluated variables (let)?

In this article, we’ll look at what’s the Jest equivalent to RSpec lazy evaluated variables (let).

What’s the Jest equivalent to RSpec lazy evaluated variables (let)?

The Jest equivalent to RSpec lazy evaluated variables (let) is defining a variable that’s shared between different tests.

For instance, we write

beforeEach(() => {
  let input = 'foo';
  beforeEach(() => {
    setupSomething(input);
  });

  describe('when input is bar', () => {
    beforeAll(() => {
      input = 'bar';
    });

    it('does something different', () => {

    });
  });

  describe('when input is baz', () => {
    beforeAll(() => {
      input = 'baz';
    });

    it('does something different', () => {

    });
  });
});

to define the input variable that’s available for all tests.

We assign values to input with different tests and use it in the setupSomething function for setting up each test.

Conclusion

The Jest equivalent to RSpec lazy evaluated variables (let) is defining a variable that’s shared between different tests.

Categories
JavaScript Answers

How to fix the ‘jest.fn() value must be a mock function or spy’ error with Jest?

Sometimes, we want to fix the ‘jest.fn() value must be a mock function or spy’ error with Jest.

In this article, we’ll look at how to fix the ‘jest.fn() value must be a mock function or spy’ error with Jest.

How to fix the ‘jest.fn() value must be a mock function or spy’ error with Jest?

To fix the ‘jest.fn() value must be a mock function or spy’ error with Jest, we should add a spy to the function that we’re testing.

For instance, we write

const video = require('./video');

test('plays video', () => {
  const spy = jest.spyOn(video, 'play');
  const isPlaying = video.play();

  expect(spy).toHaveBeenCalled();
  expect(isPlaying).toBe(true);

  spy.mockRestore();
});

to call jest.spyOn to spy on the play function in the video module.

Then we call play to test the play method.

And then we check the behavior of the spy with toHaveBeenCalled and check the returned result with toBe.

Finally, we call mockRestore to reset the spy.

Conclusion

To fix the ‘jest.fn() value must be a mock function or spy’ error with Jest, we should add a spy to the function that we’re testing.

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.