Back

createDocument not completing when using Jest fake time

  • 0
  • Self Hosted
  • Databases
WhiteWalker72
3 Apr, 2024, 06:25

I've updated to appwrite 1.5 and and node-appwrite 12.0.1. Whenever I try to run a test which fakes the system time my request to create a new document keeps processing and does not complete at all. This is the code:

TypeScript
    it('Should return work when an active work period is active', async () => {
      // Arrange
      const startDate = new Date(2024, 2, 2, 12).toISOString();
      jest.useFakeTimers().setSystemTime(new Date(2024, 2, 2, 13));

      await presencePeriodRepo.createDocument({
        userId: testUserId,
        startDate,
        type: PresencePeriodType.Work,
        expectedEndDate: new Date(2024, 2, 2, 17).toISOString(),
      });

      // Act
      const result = await controller.getPresenceStatus(client);

      // Assert
      expect(result.status).toBe(PresenceStatus.Work);
    })

Whenever I remove the usefakeTimers line the createDocument call completes just fine. This is the implementation of the createDocument function:

TypeScript
  public async createDocument(data: Schema, permissions?: string[]): Promise<Schema & Models.Document> {
    return this.databases.createDocument(this.databaseId, this.collectionId, ID.unique(), data, permissions);
  }

Note this code was working fine in Appwrite 1.4.

TL;DR
Issue: When using Jest's fake time feature, the createDocument request does not complete at all, causing the test to hang indefinitely. Solution: The Jest fake timers may be interfering with the asynchronous nature of the `createDocument` function. Consider using a different method to handle time-related testing scenarios.
Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more