Javascript AssertionError for database objects with generated timestamps

29 Views Asked by At

I'm asserting that the data fetched from my database contain the below objects but my assertionError seems to show that the objects aren't included. My query is being logged to the console, showing the only difference is from the timestamp. This is my test code:

const chai = require("chai");
const request = require("supertest");
const { server } = require("../../app");

const { expect } = chai; 

const shouldNotRestockProduct = {
    id: 1,
    name: "Salmon",
    price: 10.0,
    stock: 5,
    min_stock: 2,
    created_at: '2023-10-30TT00:00:00.000Z',
    updated_at: '2023-10-30TT00:00:00.000Z',
    shouldRestock: 'no',
  };
const shouldRestockProduct = {
    id: 2,
    name: "Chicken",
    price: 6.5,
    stock: 5,
    min_stock: 10,
    created_at: "2023-10-30TT00:00:00.000Z",
    updated_at: "2023-10-30TT00:00:00.000Z",
    shouldRestock: 'yes',
};
const shouldShortlyStockProduct = {
    id: 3,
    name: "Turkey",
    price: 5.5,
    stock: 5,
    min_stock: 5,
    created_at: "2023-10-30TT00:00:00.000Z",
    updated_at: "2023-10-30TT00:00:00.000Z",
    shouldRestock: 'shortly',
};


describe("Fetch products test", async() => {
    it("shows all stock states", async () => {
        const { body, status } = await request(server).get("/products");
        const { data } = body;
        expect(status).to.equal(200);
        expect(data).to.deep.include(shouldNotRestockProduct);
        expect(data).to.deep.include(shouldRestockProduct);
        expect(data).to.deep.include(shouldShortlyStockProduct);
    });
});

Here is the code for setting up my test database which I run the test against:

'use strict';

/** @type {import('sequelize-cli').Migration} */
module.exports = {
  async up (queryInterface, Sequelize) {
   return queryInterface.sequelize.transaction(async (t) => {
    await queryInterface.bulkInsert("Products", [
      {
        name: "Salmon",
        price: 10.0,
        stock: 5,
        min_stock: 2,
        created_at: new Date(),
        updated_at: new Date(),
      },
      {
        name: "Chicken",
        price: 6.5,
        stock: 5,
        min_stock: 10,
        created_at: new Date(),
        updated_at: new Date(),
      },
      {
        name: "Turkey",
        price: 5.5,
        stock: 5,
        min_stock: 5,
        created_at: new Date(),
        updated_at: new Date(),
      },
     ])
   })

  },

  async down (queryInterface, Sequelize) {
    return queryInterface.sequelize.transaction(async (t) => {
      await queryInterface.bulkDelete("Products", null, {})
    })
  }
};

When I run the test against the test_database, I get this error:

  1. Fetch products test shows all stock states: AssertionError: expected [ …(3) ] to deep include { id: 1, name: 'Salmon', …(6) }

I expect the assertion to pass, as the objects are the same, but could the issue be from the timestamps being different? I'm testing out Sequelize and had followed this tutorial which seemed to work on their end: ExpressJS and Sequelize application tested with mocha, chai, supertest, migrations and seeds

0

There are 0 best solutions below