Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'FIRESTORE INTERNAL ASSERTION FAILED: Unexpected state' when unit testing with Jest

I'ḿ setting up a jest test suite for a Node.js and Express REST API i'm building, i'm using @firebase/testing module to initialize a testing app, however when i try to perform any sort of operation to the database this error comes out:

FIRESTORE (7.17.2) INTERNAL ASSERTION FAILED: Unexpected state
      at fail (/home/cardonapablo/Documentos/Proyectos/Optica (Ilicit)../../../../../node_modules/@firebase/testing/node_modules/@firebase/firestore/src/util/assert.ts:39:9)
      at hardAssert (/home/cardonapablo/Documentos/Proyectos/Optica (Ilicit)../../../../../node_modules/@firebase/testing/node_modules/@firebase/firestore/src/util/assert.ts:53:5)
      at fromBytes (/home/cardonapablo/Documentos/Proyectos/Optica (Ilicit)../../../../../node_modules/@firebase/testing/node_modules/@firebase/firestore/src/remote/serializer.ts:270:5)
      at fromWatchChange (/home/cardonapablo/Documentos/Proyectos/Optica (Ilicit)../../../../../node_modules/@firebase/testing/node_modules/@firebase/firestore/src/remote/serializer.ts:486:25)
      at PersistentListenStream.onMessage (/home/cardonapablo/Documentos/Proyectos/Optica (Ilicit)../../../../../node_modules/@firebase/testing/node_modules/@firebase/firestore/src/remote/persistent_stream.ts:576:25)
      at /home/cardonapablo/Documentos/Proyectos/Optica (Ilicit)../../../../../node_modules/@firebase/testing/node_modules/@firebase/firestore/src/remote/persistent_stream.ts:456:21
      at /home/cardonapablo/Documentos/Proyectos/Optica (Ilicit)../../../../../node_modules/@firebase/testing/node_modules/@firebase/firestore/src/remote/persistent_stream.ts:509:18
      at /home/cardonapablo/Documentos/Proyectos/Optica (Ilicit)../../../../../node_modules/@firebase/testing/node_modules/@firebase/firestore/src/util/async_queue.ts:369:14

I also tried connecting to my regular firestore database with the credentials i have been using to develop the endpoints and same error pops out even tho it's the app i use daily

Weird thing is, data is being written to the database, but error still stops testing

Here is firebase setup:

(src/db/functions.js)

let app  = initializeTestApp({ 
    projectId: "illicit"
})
db = app.firestore()
module.exports = { db }

Function throwing the error

(tests/fixtures/db.js)

const { db } = require('../../src/db/functions')
const bcrypt = require('bcrypt');

const createAdmin = async function() {

    // Encrypt password
    let encPass = await bcrypt.hash("admin", 8)

    let admin = {
        name: "Admin Test User",
        email: "[email protected]",
        password: encPass,
        tokens: []
    }

    // Add to db
    let docRef = await db.collection('admins').add(admin) // <- This line throws the error
    return;
}

module.exports = {
    createAdmin
}

And finally testing file

(tests/glasses.test.js)
const supertest = require('supertest');
const app = require('../src/app')
const functions = require('./fixtures/db')

let adminToken;
let glassesId;

//Executes before any test, here is where error occurs, before any tests
beforeAll( async () => {
    await functions.createAdmin()
    return
})

test('Should log in an admin', async () => {
    let response = await supertest(app)
    .post('/admins/login')
    .send({
        email: '[email protected]',
        password: 'admin'
    })
    .expect(200);
    expect(response.body.token).toEqual(expect.any(String))
    adminToken = response.token;
});

This only happens only when i try to test, regular app works just fine

Things i've tried:

  • Firestore rules are read and write true, so it's not a rules error
  • Mocked Firestore with firebase-mock and Jest seems to work fine, however this is not a solution, since i need to test data inside the database

Hope you can help me :)

like image 978
CardonaPablo Avatar asked Aug 08 '20 19:08

CardonaPablo


People also ask

Do you test the internals of FireStore?

We don't particularly care about testing the internals of Firestore, but there is value in testing our homebrewed logic that runs on the retrieved data. Granted, even though all we're doing above is extrapolating the product count, in a real-world scenario this API function might be doing quite a bit of heavy lifting.

Why am I getting a unexpected state error in FireStore?

It looks like the "Unexpected state" error was thrown when our app called disableNetwork () on multiple Firestore instances, waited a few seconds and then called enableNetwork () on those same instances. This was likely happening while Firestore was initialising.

Are FireStore rules read and write true or false?

Firestore rules are read and write true, so it's not a rules error Mocked Firestore with firebase-mock and Jest seems to work fine, however this is not a solution, since i need to test data inside the database

What is Supertest in FireStore?

This library aims to simulate all of the Firestore functions with an in-memory database that you can define on every test, letting you set up mock data for your unit tests with ease. If you're not already familiar with supertest, it's a library for ease-of-testing with Express endpoints.


3 Answers

You should change Jest's test environment from the default jsdom to node using jest --env=node or by setting the testEnvironment option to node in your Jest config.

like image 102
Korneel Avatar answered Oct 25 '22 02:10

Korneel


This is a open issue on GitHub. I'm pasting my comment from that issue here to hopefully help some other people:

I experienced the same error message on 9.6.6 with NextJS. I believe this error message could be presented due to a range of errors - as I see 100+ Stackoverflow questions with this error message.

After lots of debugging I realized I accidently used SQL capitalization:

.orderBy('time', 'ASC') = "INTERNAL ASSERTION FAILED: Unexpected state" .orderBy('time', 'asc') = No Errors!

This was a pain to debug, and my mistake was so small. Maybe better error reporting is needed in cases like this? When you get then Google this error message it easily leads you down a path of debugging things completely irrelevant to the real error.

So pretty much - a tiny syntax error can cause the error message and lead you down a road of debugging the wrong things. To solve this you have to find exactly where it is happening and narrow in your debuging.

like image 27
August Kimo Avatar answered Oct 25 '22 03:10

August Kimo


Solved the problem myself, i was using the Firebase web client, I switched to the Admin SDK made specifically for servers, i guess it was some sort of auth problem, because the admin sdk automatically authenticates you in the db

like image 38
CardonaPablo Avatar answered Oct 25 '22 03:10

CardonaPablo