I am trying to execute unit tests for Firestore. Below is the code snippet I am using:
import { getDoc, setDoc } from "@firebase/firestore";
import {
assertFails,
assertSucceeds,
initializeTestEnvironment,
RulesTestEnvironment,
} from "@firebase/rules-unit-testing"
describe('Creating a user', () => {
let alice;
let anon;
let testEnv;
beforeAll(async () => {
testEnv = await initializeTestEnvironment({
projectId: "PROJECT_ID"
})
})
beforeEach(() => {
alice = testEnv.authenticatedContext("alice", { user_id: "1" });
anon = testEnv.unauthenticatedContext();
});
test('alice can create a user', async () => {
await assertSucceeds(setDoc(alice.firestore(), '/users/alice'), { "name": "alice" });
})
});
The above code is based on the guidance provided in the official documentation, where they demonstrate a similar sample code:
// Assuming a Firestore app and the Firestore emulator for this example
import { setDoc } from "firebase/firestore";
const alice = testEnv.authenticatedContext("alice", { … });
// Use the Firestore instance associated with this context
await assertSucceeds(setDoc(alice.firestore(), '/users/alice'), { ... });
However, I encountered an error when running the code:
FirebaseError: Expected type 'DocumentReference', but it was: a custom Firestore object
This error occurred at the following line:
await assertSucceeds(setDoc(alice.firestore(), '/users/alice'), { "name": "alice" });
I am puzzled by why this is not functioning correctly even after reviewing the official documentation. Can anyone offer insight into what might be the issue here?
List of npm dependencies:
├── @babel/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="690a0500295e475b58d3c4c2d7cacbdcc0c9dae1cceeddeec597ccd51e89827a8ce89595b829507aca742848eeaa6fafa6eea9faedaafae689bbbaa8">[email protected]</a>
├── @babel/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e98a869969486978745c51577365606d7660767077">[email protected]</a>
├── @babel/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ddb7bfbeb196bdb494abbfb8adbab3aeb298fcbbeeeaa69cebcbdaaacb8bcade9a6e3fcedfced3fe">[email protected]</a>
├── @babel/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="02727067642d0001407b78697d76767b71696d00777901">[email protected]</a>
├── @firebase/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cdadacb5afac87bfb2badfbbbaffbacabcbaeffbdfd6cad4cecfc8caedd5dfaaa583cfd2dbdbeedbcaeefdcc91cd20cada"><span style="display: none;">[email protected]</span>[email protected]</a>
├── <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="62e6fdf0faf9f079fef2e0f681"
data-prefix=""
data-domain=""
data-suffix="">[email protected]</a>
└──
<a href="/cdn-cgi/l/email-protection" class="__cf_email__"
data-cfemail="4d34393d77723872617474817269757971757215">[email protected]</a>