I'm writing some tests in Jest for an API that's returning results from Postgres via the pg library. I post some data (via a faker'ed set of values, template1), and then test that what I get back is the same, allowing for an id value, and modified fields. The template1 data includes an approved property (SQL defn: approved timestamp with time zone), which is generated like so:
{
approved: faker.date.past(),
description: faker.lorem.paragraph(),
groups: faker.lorem.paragraph(),
}
This test is something like this:
expect(response.body.rows).toStrictEqual([
{
...template1,
id: 1,
modified: null,
},
])
The issue is that the test is failing because the returned value of approved appears to be a string:
expect(received).toStrictEqual(expected) // deep equality
- Expected
+ Received
@@ -1,8 +1,8 @@
Array [
Object {
- "approved": 2019-12-19T03:48:20.613Z,
+ "approved": "2019-12-19T03:48:20.613Z",
"approved_by": "[email protected]",
I've tried casting the template1.approved value just prior to the comparison to both a date, and to a string. Both approaches fail. What am I doing wrong, and how do I fix the issue?
I didn't try hard enough - the answer was to convert the timestamp to JSON:
expect(response.body.rows).toStrictEqual([
{
...template1,
id: 1,
modified: null,
approved: new Date(template1.approved).toJSON(), // here
},
])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With