Issue with unit testing in Firestore: Anticipated data type was supposed to be 'DocumentReference', however, what was received was a unique Firestore object

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>

Answer №1

I am unable to locate the specific example you referred to in the provided link.

As per the official documentation, they recommend using this sample code:

setDoc requires a DocumentReference as the first argument, which aligns with the error message you received.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

During the rendering process, the property "instance" was attempted to be accessed but is not defined

I am having trouble creating a Contact Us page using v-model. My code keeps throwing these errors: Property "inputted_name" was accessed during render but is not defined on instance Property "inputted_email" was accessed during render but is not defined o ...

Having trouble with Node JS res.redirect() not functioning as desired?

I'm attempting to use res.redirect() to redirect to an external URL, but the host name isn't being replaced. For example: My current URL: https://example.com Desired redirect URL: When I use res.redirect("https://example-2.com/pqr?par=123 ...

Having trouble getting an Angular directive to bind a click event to an external element?

I've been working on creating a unique custom event for toggling with Angular. The directive I'm using is called toggleable. It may sound simple at first, but the tricky part is that I want to be able to use any button or link on the page for to ...

What distinguishes node from node js?

Following the instructions on https://nodejs.org/en/download/package-manager/, I installed Node.js. Upon checking the current version using: :~/Downloads$ nodejs -v I discovered that I was running v4.2.6, which is an older version. Deciding to update, I ...

Import a JSON file into Parse by reading and parsing it to store in the database

I am currently facing a challenge with parsing JSON data in my Parse Cloud function. After fetching the JSON file, I need to parse the data and populate one of my classes with the results. However, I'm having trouble figuring out how to properly parse ...

Having trouble with Lerna bootstrap? You might be running into the dreaded npm error code E401

Every time I run Lerna bootstrap on Jenkins, it fails with an error, but it works fine on my local machine. npm ERR! code E401 npm ERR! Unable to authenticate, need: BASIC realm="Sonatype Nexus Repository Manager" Package.json in the main folder ...

Clicking on a card will cause it to expand

Exploring Material-UI-React for the first time to create expanding cards, I've encountered an issue where all cards expand when one is clicked. How can I modify my code so that only the clicked card expands without affecting the others? const useSt ...

Failed attempt to authenticate with Facebook via Firebase does not redirect and instead leaves the user stranded on a blank white screen

Currently working on an app that integrates Facebook login using the authWithOPopup feature in the Firebase API. Despite extensive troubleshooting and researching solutions, I have not been able to resolve the issue. Running tests on Ionic View. Operation ...

What is the best way to retrieve the value of a specific textfield using the map function with a collection of

These posts have identical text fields for comments, using the same useState hook. I am trying to extract the value of a specific text field without affecting others. It's similar to Facebook posts, but I'm unsure how to achieve this. const PostT ...

Error injecting Angular components

Here is the structure of my HTML file: <html> <head> <title>PLD Interaction pattern</title> <link href="css/bootstrap.min.css" rel="stylesheet" type="text/css"/> </head> <body ng-app="myT ...

Choosing custom element children once they're connected to the DOM: A guide

My goal is to retrieve the value of transaction-name__inputbox when the user clicks on the transaction-add__button. The function transactionAddHandler is triggered upon clicking the button. However, my attempt to select this element using document.querySe ...

Ways to incorporate a dynamic HTML form that allows for input elements to be added both horizontally and vertically while maintaining connected lines

Looking to design a form that showcases HTML elements in both vertical and horizontal positions, with lines connecting them as seen in this example: https://i.sstatic.net/jB12f.png. Can anyone offer guidance on how to achieve this? Thanks! ...

Grabbing nested JSON Array data using Node.js

As a beginner in Node.js, I’m attempting to extract data from the JSON below: var data1 = { "_id":"R1::table::A1::order::167::comanda::2", "_rev":"1-ed6df32d3b4df9cc8019e38d655a86f5", "comanda":[ [ { ...

Utilize the .not() filter function in JavaScript

I am trying to apply a filter of not(.pseudo-element) using JavaScript, but I am unsure how to add it. So far, I have been able to extract #app from the DOM with the following code: const app = document.getElementById('app') app.style.filter ...

Utilizing Vue.js to dynamically update input fields based on other field values

I'm a beginner with Vue.js and Vuetify.js, and I am encountering some difficulties with the following: <v-select v-model="car.type" :items="types" label="Type" ></v-select> <div v-if="car.type === 'fiat'"> < ...

What is the best way to replicate the Ctrl+A action on an element using jQuery or JavaScript?

Is there a way to trigger the Ctrl+A key combination in a textarea element when a specific event occurs, without resorting to caret positioning or selecting all text separately? I'm looking for a method that simulates hitting Ctrl+A using a cross-brow ...

Avoid refreshing AJAX on browser back navigation

Describing my current situation: I have a scenario with 2 pages: - On page 1, it sends a request using $.ajax to receive JSON data for display. Then there is a button that when clicked, takes me to page 2. - After clicking the button and transitionin ...

Asynchronous requests in Node.js within an Array.forEach loop not finishing execution prior to writing a JSON file

I have developed a web scraping Node.js application that extracts job description text from multiple URLs. Currently, I am working with an array of job objects called jobObj. The code iterates through each URL, sends a request for HTML content, uses the Ch ...

Angular JS Sorting Wordpress Plugin allows users to easily organize and sort content

Seeking some assistance here, any help would be greatly appreciated. Currently using a Wordpress Angular JS plugin that is causing some unusual alphabetical sorting. This snippet of code showcases the taxonomy: <!-- for taxonomy --> <div ng-if ...

JSON has encountered an unconventional string at position 41 that was not anticipated

Attempting to learn Javascript as a beginner, I am facing an error that is puzzling me: SyntaxError: /home/runner/myrepl/config.json: Unexpected string in JSON at position 41 Upon checking my index.js file, it seems correct at position 41 (at least I beli ...