Search code examples
javascriptunit-testingsequelize.js

Sequelize createdAt/updatedAt timestamps (milliseconds) being rounded to 000?


I'm trying to unit test a createJob function.

I'm comparing the job object returned as part of the createJob response, to the object that's returned when querying the database for that newly created object.

The first object has milliseconds included in the timestamps, the second has them rounded to 000. createdAt and updatedAt are automatically created, and don't actually seem to store the milliseconds at all.

actual = result of createJob(...)

expected = result of getJob(5)

  showDiff: true,
  actual: {
    id: 5,
    companyId: 2,
    title: 'Future Markets Designer',
    wage: 80000,
    location: 'East Paolohaven',
    description: 'Dynamic',
    featured: false,
    updatedAt: 2021-08-03T15:53:44.335Z,
    createdAt: 2021-08-03T15:53:44.335Z
  },
  expected: {
    id: 5,
    title: 'Future Markets Designer',
    wage: 80000,
    location: 'East Paolohaven',
    description: 'Dynamic',
    featured: false,
    createdAt: 2021-08-03T15:53:44.000Z,
    updatedAt: 2021-08-03T15:53:44.000Z,
    companyId: 2
  },
  operator: 'deepStrictEqual'

Not sure the best way to fix this?

Thanks


Solution

  • I'll leave this here in case it helps someone else. Not sure if it's the best way to do it, but explicitly adding createdAt and updatedAt fixed the issue:

    createdAt: {
        type: Sequelize.DATE(3),
        allowNull: false,
    },
    updatedAt: {
        type: Sequelize.DATE(3),
        allowNull: false,
    }