firebase-admin-node icon indicating copy to clipboard operation
firebase-admin-node copied to clipboard

[Firestore] Using Vitest setSystemTime() throws error

Open risalfajar opened this issue 2 months ago • 2 comments

[READ] Step 1: Are you in the right place?

Yes

[REQUIRED] Step 2: Describe your environment

  • Operating System version: MacOS Sequioa 15.7.2
  • Firebase SDK version: 12.5.0
  • Firebase Product: Firestore
  • Node.js version: 22.19.0
  • NPM version: 10.9.3

[REQUIRED] Step 3: Describe the problem

When using vi.setSystemTime() on integration tests, saving a Date to Firestore throws error.

Steps to reproduce:

  1. Setup Admin NodeJS SDK
  2. Setup Vitest & integration tests
  3. Use vi.setSystemTime()
  4. Save data containing a Date to Firestore
  5. Error
Error: Value for argument "data" is not a valid Firestore document. Input is not a plain JavaScript object (found in field "date").

Relevant Code:

it("date test", async () => {
	vi.setSystemTime(new Date())
	admin.firestore.collection("tests").doc("1").set({ date: new Date() }) // throws error
	vi.useRealTimers()
})

If I were to log the new Date() it will return this: {"severity":"INFO","message":"MockDate 2025-11-27T14:51:53.132Z"}

risalfajar avatar Nov 27 '25 14:11 risalfajar

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

google-oss-bot avatar Nov 27 '25 14:11 google-oss-bot

Would you mind creating a github repo that I can clone to reproduce the error for myself?

I'm not sure we'll be able to do much in the Firestore SDK to support this because Firestore needs to know how to serialize a Date and it doesn't support serializing complex objects, such as "mock date" objects. But I can see if there is any low-friction way to support this.

dconeybe avatar Dec 01 '25 19:12 dconeybe