Last active
August 16, 2024 01:17
-
-
Save r3dm1ke/0542bb2b1279476da87df1deee69ba49 to your computer and use it in GitHub Desktop.
toBeISODate Jest matcher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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