'FIRESTORE INTERNAL ASSERTION FAILED: Unexpected state' 使用 Jest 进行单元测试时

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

我'ḿ 为我正在构建的 Node.js 和 Express REST API 设置一个笑话测试套件,我正在使用 @firebase/testing 模块来初始化测试应用程序,但是当我尝试对数据库执行任何类型的操作时出现此错误:

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

我还尝试使用我一直用来开发端点的凭据连接到我的常规 firestore 数据库,即使是我每天使用的应用程序也会弹出同样的错误

奇怪的是,正在向数据库写入数据,但仍然报错停止测试

这里是 Firebase 设置:

(src/db/functions.js)

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

函数抛出错误

(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: "admin@test.com",
        password: encPass,
        tokens: []
    }

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

module.exports = {
    createAdmin
}

最后测试文件

(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: 'admin@test.com',
        password: 'admin'
    })
    .expect(200);
    expect(response.body.token).toEqual(expect.any(String))
    adminToken = response.token;
});

只有在我尝试测试时才会出现这种情况,常规应用程序工作正常

我尝试过的事情:

希望你能帮助我:)

我自己解决了这个问题,我使用的是 Firebase 网络客户端,我切换到专门为服务器制作的 Admin SDK,我猜这是某种身份验证问题,因为 admin sdk 会自动在数据库中对您进行身份验证

您应该使用 jest --env=node 将 Jest 的测试环境从默认的 jsdom 更改为 node,或者在您的 Jest 中将 testEnvironment 选项设置为 node配置

根据上面的指示执行此命令yarn test jest --env=node 此后错误消失

这是 GitHub 上的 open issue。我在此处粘贴我对该问题的评论,希望对其他人有所帮助:

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+ Whosebug 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.

差不多 - 一个微小的语法错误可能会导致错误消息并导致您走上调试错误之路。要解决此问题,您必须准确找到它发生的位置并缩小调试范围。