jest-date is a utility library providing custom Jest matchers specifically designed for comparing and asserting dates. It simplifies date-related tests by offering a declarative API, such as `toBeBefore`, `toBeSameDayAs`, and `toBeMonday`, eliminating the need for repetitive date manipulation logic in test assertions. The current stable version is 1.1.6, and while no explicit release cadence is stated, its versioning suggests incremental updates. A key differentiator is the helpful, descriptive failure messages it provides, indicating precise differences (e.g., '1 second after' or '3 months after') rather than generic assertion failures, making debugging significantly easier for developers. It leverages underlying date utility libraries, abstracting their complexity for focused testing scenarios.
npm install jest-dateVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to install `jest-date`, configure it in a Jest setup file, and use various custom matchers like `toBeBefore`, `toBeSameDayAs`, and `toBeMonday` to assert date comparisons in tests, highlighting their readability and expressive failure messages.
For full type support, use the global import `import 'jest-date';` in a `.ts` Jest setup file (e.g., `jest.setup.ts`). If you must use selective imports, you will need to manually augment Jest's `Expect` interface in your project's `d.ts` files.
Ensure `import 'jest-date';` is present in a file configured in Jest's `setupFilesAfterEnv` option. For example, add `"setupFilesAfterEnv": ["<rootDir>/jest.setup.ts"]` to your `jest.config.js` and create `jest.setup.ts` with the import.
Ensure you have `import 'jest-date';` in your Jest setup file (e.g., `jest.setup.ts`) and that this file is correctly referenced in your `jest.config.js` under `setupFilesAfterEnv`.
Verify that `import 'jest-date';` is correctly placed and executed in your Jest test environment's setup, typically within a file specified by Jest's `setupFilesAfterEnv` configuration option.
No dependency data recorded yet.