lundi 30 décembre 2019

Comparing timestamp results from Postgres in Jest

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": "Percy_Hills@yahoo.com",

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?

Aucun commentaire:

Enregistrer un commentaire