Hone logo
Hone
Problems

Configuring Jest with setupFiles for Global Configuration

Jest's setupFiles configuration option allows you to specify files that will be run before each test suite. This is incredibly useful for setting up global variables, mocking modules, or configuring libraries that your tests rely on. This challenge will guide you through implementing setupFiles to configure a global utility function for your tests.

Problem Description

You are building a testing suite for a JavaScript library that uses a utility function called formatDate. This function takes a date object and returns a formatted string. You want to mock the formatDate function in your tests to control its behavior and avoid dependencies on external date formatting libraries. You need to configure Jest using setupFiles to automatically mock the formatDate function before each test suite runs. The mock should replace the original formatDate function with a function that always returns "Mocked Date".

Key Requirements:

  • Create a setupFiles array in your Jest configuration.
  • Create a setup.ts file (or .js if you prefer) that will be executed before each test.
  • Inside setup.ts, mock the formatDate function.
  • Ensure that all tests in your project use the mocked formatDate function.

Expected Behavior:

When a test runs, the original formatDate function should be replaced with a mock function that returns "Mocked Date". Tests should not fail due to the absence or incorrect behavior of the original formatDate function.

Edge Cases to Consider:

  • The formatDate function might be imported from a different module in your project.
  • The setup file needs to be correctly referenced in the Jest configuration.
  • Ensure the mock is applied globally, affecting all test files.

Examples

Example 1:

// Original code (in a module being tested)
export function formatDate(date: Date): string {
  // Complex date formatting logic here
  return date.toLocaleDateString();
}

// Test file
import { formatDate } from './dateUtils';

test('should format a date', () => {
  const date = new Date();
  expect(formatDate(date)).toBe('Mocked Date'); // This should pass
});

Example 2:

// setup.ts
jest.mock('./dateUtils', () => ({
  formatDate: jest.fn(() => 'Mocked Date'),
}));

// Jest configuration (jest.config.js or package.json)
module.exports = {
  setupFiles: ['<rootDir>/setup.ts'],
  // ... other configurations
};

Constraints

  • The solution must be written in TypeScript.
  • The formatDate function is assumed to be defined in a separate module named dateUtils.ts (or .js).
  • The solution must use Jest's mocking capabilities.
  • The setup file must be correctly configured in the Jest configuration.
  • The solution should be compatible with standard Jest configurations.

Notes

  • Consider using jest.mock() to replace the module containing formatDate with a mock implementation.
  • The setupFiles array in your Jest configuration should point to the correct path of your setup file.
  • Ensure that the mock function returns the expected value ("Mocked Date").
  • You can use jest.fn() to create a mock function.
  • The setup file will be executed before each test suite, so it's a good place to set up global mocks or configurations.
Loading editor...
typescript