drizzle-orm icon indicating copy to clipboard operation
drizzle-orm copied to clipboard

[BUG]: Unable to insert when using jest fake timers

Open JesusTheHun opened this issue 1 year ago • 1 comments

What version of drizzle-orm are you using?

0.25.4

What version of drizzle-kit are you using?

No response

Describe the Bug

If you try to insert data while fake timers are in use, the data is never inserted and the test timeout.

import {beforeAll, afterAll, jest} from "@jest/globals";
import {datetime, int, mysqlTable, text} from "drizzle-orm/mysql-core";
import {drizzle, MySql2Client, MySql2Database} from "drizzle-orm/mysql2";
import {getDrizzleClient} from "src/test-utils/db-utils";

export const booksTable = mysqlTable('books', {
  id: int('id').autoincrement().primaryKey(),
  title: text('title').notNull(),
  createAt: datetime('createdAt', { mode: 'date' }).notNull(), // the presence of a datetime field is unrelated, I've tested 
});

describe('Demo', () => {
  let drizzleClient: MySql2Client;
  let db: MySql2Database;

  beforeAll(async () => {
    drizzleClient = await getDrizzleClient();
    db = drizzle(drizzleClient);
    jest.useFakeTimers(); // Comment this line, and it works !
  });

  afterAll(async () => {
    await drizzleClient.end();
  });

  it('should insert data', async () => {
    await db.insert(booksTable).values({
      title: "Never happening",
      createAt: new Date(),
    });
  });
});

Expected behavior

No response

Environment & setup

No response

JesusTheHun avatar May 05 '23 13:05 JesusTheHun

jest mocks too much by default in its timers. try mocking out just the parts you really need, that should help (jest timer mocking supports granular configuration). had same issue with fastify init breaking, this helped.

kibertoad avatar May 21 '23 21:05 kibertoad

This issue is not related to drizzle-orm

AndriiSherman avatar Jul 21 '23 17:07 AndriiSherman