Skip to main content
InterviewHubPRO
Section Overview
Q99

Question 99 of 407

Part 4: React Testing & Jest (95 - 100)

24%
React Core & Hooks (407 Qs)•Part 4: React Testing & Jest (95 - 100)

What are the advantages of Jest over Jasmine?

junior LevelCode ExampleTesting

There are couple of advantages compared to Jasmine:

  • Automatically finds tests to execute in your source code.
  • Automatically mocks dependencies when running your tests.
  • Allows you to test asynchronous code synchronously.
  • Runs your tests with a fake DOM implementation (via jsdom) so that your tests can be run on the command line.
  • Runs tests in parallel processes so that they finish sooner.

Practical Code Example

javascript
// Advantages of Jest: Zero config, parallelized workers, built-in mocking & assertions
describe('UserService', () => {
  it('mocks asynchronous network requests natively', async () => {
    const mockFetch = jest.fn().mockResolvedValue({ id: 101, name: 'Alice' });
    const user = await mockFetch();
    expect(user.name).toBe('Alice');
    expect(mockFetch).toHaveBeenCalledTimes(1);
  });
});