-
Notifications
You must be signed in to change notification settings - Fork 934
Can't use Timestamp in Firestore rules unit tests #6077
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@ebeloded Can you run |
This is what gets listed: @firebase/rules-unit-testing 2.0.2
firebase 9.6.8
firebase-admin 10.0.2 I've noticed that Timestamp returned by BTW, the error only happens in tests. When doing the same in the app (against Emulator or not), everything works and the time checking rule is respected. |
Yes, the Timestamp object are unfortunately not compatible. Are you able to use the Timestamp from |
No, Timestamp from the admin library doesn't work either. I've tried Furthermore, I tried another FieldValue ( |
I'm having the same issue. |
Having the same issue. |
@ebeloded I had this issue a long time ago. With Jest, I am mocking The TypeScript code below should do that, but I’m using my own client compatibility layer of firestore-admin mocks instead of the import type { Timestamp } from '@firebase/firestore-types'
// ...
jest.unstable_mockModule('@firebase/firestore', () => {
return {
...<any>jest.requireActual('@firebase/firestore'),
serverTimestamp: jest.fn(() => <Timestamp><unknown>new Date())
}
}) |
Thanks @laurentpayot for pointing out the possible workaround. The problem is that this doesn't work if the Firestore rules are checking timestamp integrity. Like this:
This rule won't work unless you use proper |
I had the same problem, and was able to fix it by not using A very simple example: import * as firebaseTest from "@firebase/rules-unit-testing"
import firebase from "firebase/compat"
describe ('some test') {
test ('that we can use serverTimestamp') {
const env = await firebaseTest.initializeTestEnvironment({ projectId: 'your-project-id' })
const context = env.authenticatedContext('some-user-id')
const firestore = context.firestore()
await firestore.collection('collectionId').doc('docId').set({
createdAt: firebase.firestore.FieldValue.serverTimestamp()
})
}
} In that case, even with the below security rule your test will pass.
I think it's the same for Timestamp and other firebase types. Take it all from |
Hello all. I just want to acknowledge that we are still investigating this issue internally. I'll have a more concrete response by the end of the week. |
Hi all, Firebaser here. @juyaki is correct in #6077 (comment), the underlying issue is that rules-unit-testing is depending entirely on the compat bindings. We will work on finding some time to migrate this library to the modular v9 bindings in the next major version change, in the meantime if we are happy to accept PRs. |
I tried |
UPDATE: I’m now using Vitest instead of Jest so I could not use Jest mocks anymore. import { Timestamp } from 'firebase-admin/firestore'
function getServerTimestamp() {
const datesMs = new Date().getTime()
return new Timestamp(Math.floor(datesMs / 1000), (datesMs % 1000) * 1000)
} |
Importing the type from the firestore client library does the trick :
Out of curiosity, how come admin and client Timestamp types are not compatible ? Is this on purpose ? |
Turns out we have the same issue, not just in tests but when writing documents to Firestore. This works without the converter, but if we use a converter we get the same error as the author :
I found that nt is the minified name of Timestamp from our installed dependency. If I instead use a Date, then the file is written correctly with no error. Additionally, if i use serverTimestamp() it works too. Therefore, I believe that this is an issue where you cannot use Timestamps in return value from Current workaround for me : Just use Date object instead, we don't need nanosecond precision. It does however make it confusing to read our Converters : we send Date objects and receive Timestamp objects... |
I think I am facing the same issue ... I have an app that uses @firebase/firestore and stores records using Timestamp fields. In a Cloud Function for Firebase I am using the admin SDK @firebase-admin/firestore to do some db cleansing tasks based on those timestamps. While working with the Timestamps in the app works as expected, in my Function I only receive an empty object for every Timestamp field in my record. Any thoughts and/or help on this? |
Some additional information I ran into while validating an object as a timestamp instance: _ is an oldschool way to point out properties like that, but the type should match the implementation either way. |
I came across same problem to test security rules for timestamps. This is what worked for me:
Simple as that! |
[REQUIRED] Describe your environment
[REQUIRED] Describe the problem
Steps to reproduce:
While writing unit tests for Firestore rules, I am trying to set a timestamp on a document, but this results in the following error:
serverTimestamp()
:Timestamp.now()
and foradminFirebase.firestore.Timestamp.now()
:Relevant Code:
Update
It turns out that I can provide
new Date()
as timestamp, which doesn't throw an error. However, the timestamp generated in this case doesn't pass the security rule that verifies that the time:The text was updated successfully, but these errors were encountered: