chai-iso8601
Chai assertion to check dates in the ISO-8601 format.
Developed at the Media Engineering Institute (HEIG-VD).
Installation
$> npm install --save-dev chai-iso8601
Usage
Basic usage allows you to check that a string is a valid ISO-8601 date and represents the expected time.
const chai = ; // Note the extra call (chai-iso8601 returns a factory function).chai; // Simple usageconst expectedDateString = '2001-01-01T00:00:00Z';chaitobe; // With a date objectconst expectedDate = 2001 0 1 0 0 0 0;chaitobe; // With a moment objectconst expectedMoment = '2001-01-01T00:00:00Z';chaitobe;
Operators
chai-iso8601 includes operators to perform more complex assertions on dates. All the following assertions will pass:
// Check that a string is an ISO-8601 date that is equal to another date (this is the default operator used above).chaitobe; // Check that a string is an ISO-8601 date that is after another date (and not the same).chaitobe; // Check that a string is an ISO-8601 date that is after or the same as another date.chaitobe;chaitobe; // Check that a string is an ISO-8601 date that is before another date (and not the same).chaitobe; // Check that a string is an ISO-8601 date that is before or the same as another date.chaitobe;chaitobe;
Margin of error
You may pass an additional number representing a margin of error that the date must be within. This can be useful in tests if you know the approximate value of a date but not its exact value.
Using a margin changes the behavior of the operators. All the following assertions will pass:
// Check that a string is an ISO-8601 date that is equal to another date with a margin of one second.chaitobe;chaitobe;chaitobe; // Check that a string is an ISO-8601 date that is after another date but no more than one second.chaitobe;chaitobe; // Check that a string is an ISO-8601 date that is after or the same as another date but no more than one second.chaitobe;chaitobe; // Check that a string is an ISO-8601 date that is before another date but no more than one second.chaitobe;chaitobe; // Check that a string is an ISO-8601 date that is before or the same as another date but no more than one second.chaitobe;chaitobe;
Note that the following assertions WILL NOT PASS although they would without a margin of error:
// The date is after the specified one, but it exceeds the margin of error of 500 milliseconds.chaitobe;chaitobe; // The date is before the specified one, but it exceeds the margin of error of 500 milliseconds.chaitobe;chaitobe;
If you are using a margin of error in your tests, you might want to make it mandatory so that it throws an error if you forget to specify one.
const chai = ; // Note the extra call (chai-iso8601 returns a factory function).chai; // Error thrown! (margin is missing)chaitobe; // No error thrownchaitobe; // Use a margin of zero if you don't need a margin for that particular test.chaitobe;