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
- Something like
new Date()
was used, and the snapshot test was re-run on a different date - 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 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"]
}