[Firestore] Using Vitest setSystemTime() throws error
[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:
- Setup Admin NodeJS SDK
- Setup Vitest & integration tests
- Use vi.setSystemTime()
- Save data containing a Date to Firestore
- 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"}
I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
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.