Skip to content

Instantly share code, notes, and snippets.

@r3dm1ke
Last active August 16, 2024 01:17
Show Gist options
  • Save r3dm1ke/0542bb2b1279476da87df1deee69ba49 to your computer and use it in GitHub Desktop.
Save r3dm1ke/0542bb2b1279476da87df1deee69ba49 to your computer and use it in GitHub Desktop.
toBeISODate Jest matcher
expect.extend({
toBeISODate(received) {
// This regexp checks for formatting
if (
!/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/.test(received)
) {
return {
pass: false,
message: `Expected ${received} to be a valid ISO date string`,
};
}
// This checks if JS can correctly parse it
const d = new Date(received);
return d.toISOString() === received
? {
pass: true,
message: 'Expected ${received} not to be a valid ISO date string',
}
: {
pass: false,
message: `Expected ${received} to be a valid ISO date string`,
};
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment