Skip to content

Latest commit

 

History

History
38 lines (31 loc) · 1.31 KB

README.md

File metadata and controls

38 lines (31 loc) · 1.31 KB

A Jest snapshot serializer that removes headaches when working with JavaScript Date objects

NPM version Build Status

When encountering a JavaScript Date object within a Jest snapshot, this serializer determines if the date corresponds to the current date (new Date()) or not. It then serializes them as follows:

Current Date:

- "yourDate": 2018-07-13T22:42:46.531Z
+ "yourDate": "Current Date"

Non-current Date:

- "yourDate": 2018-07-13T22:42:46.531Z
+ "yourDate": "07-13-2018"

This effectively avoids errors where snapshots fail because

  1. Something like new Date() was used, and the snapshot test was re-run on a different date
  2. The snapshot test is run in an environment with a different timezone than that of the developer who wrote the test, causing date serialization mis-match

Install

Install the package as a dev-dependency:

# npm
npm install --save-dev jest-serializer-date

# yarn
yarn add --dev jest-serializer-date

Tell jest to use the package in package.json:

"jest": {
    "snapshotSerializers": ["jest-serializer-date"]
}