简体   繁体   中英

Can't write firestore tests because of: FIRESTORE (9.6.1) INTERNAL ASSERTION FAILED: Unexpected state

Is testing possible with firebase firestore? I have been following testing tutorial and test that work for others are not working for me.

My component code Works in the client, so I'm very confused why I'm getting this error.

I have only found information regarding testing security rules in firebase, but nothing about firestore, does that mean that its the only thing that we can test?

this is the error that I get:

    FIRESTORE (9.6.1) INTERNAL ASSERTION FAILED: Unexpected state

      at fail (node_modules/@firebase/firestore/src/util/assert.ts:40:9)
      at hardAssert (node_modules/@firebase/firestore/src/util/assert.ts:54:5)
      at fromBytes (node_modules/@firebase/firestore/src/remote/serializer.ts:252:5)
      at fromWatchChange (node_modules/@firebase/firestore/src/remote/serializer.ts:475:25)
      at PersistentListenStream.onMessage (node_modules/@firebase/firestore/src/remote/persistent_stream.ts:642:25)
      at node_modules/@firebase/firestore/src/remote/persistent_stream.ts:517:21
      at node_modules/@firebase/firestore/src/remote/persistent_stream.ts:570:18
      at node_modules/@firebase/firestore/src/util/async_queue_impl.ts:135:7
      at node_modules/@firebase/firestore/src/util/async_queue_impl.ts:186:14

● if the user is fetched, the name is shown

    Unable to find an element with the text: /James/i. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.

My component code:

function Hellopage() {
  const [userData, setUserData] = useState();

  const getData = async () => {
    const docRef = doc(firestore, "users", "pW5CizOJOpXezr5lGGshDmKdVzZ2");
    const docSnap = await getDoc(docRef);
    if (docSnap.exists()) {
      setUserData(docSnap.data());
    } else {
      // doc.data() will be undefined in this case
      console.log("No such document!");
    }
  };

  useEffect(() => {
    getData();
  }, []);

  return (
    <div>
      <div>{userData?.name}</div>
    </div>
  );
}

export default Hellopage;

My Test:

test("if the user is fetched, the name is shown", async () => {
  const result = render(<HelloPage />);

  expect(await result.findByText(/James/i)).toBeInTheDocument();
});

This is an issue with the jsdom test environment. The environment should be switched back to the default node when using firestore. If you're relying on jsdom for other tests, the simplest way to do this may be with a docblock at the top of your file:

/**
 * @jest-environment node
 */

just rerun the development server and the problem will go away

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM