node.js - 'FIRESTORE 内部断言失败 : Unexpected state' when unit testing with Jest

标签 node.js firebase unit-testing jestjs firebase-test-lab

我为我正在构建的 Node.js 和 Express REST API 设置了一个 Jest 测试套件,我正在使用 @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 数据库,即使它是我每天使用的应用程序,也会弹出相同的错误
奇怪的是,数据正在写入数据库,但错误仍然停止测试
这是火力基地设置:
(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;
});
这只发生在我尝试测试时,常规应用程序工作正常
我尝试过的事情:
  • Firestore 规则读写为真,所以不是规则错误
  • 使用 firebase-mock 和 Jest 模拟 Firestore 似乎工作正常,但这不是
    解决方案,因为我需要测试数据库中的数据

  • 希望你能帮我 :)

    最佳答案

    您应该更改 Jest 的默认测试环境 jsdomnode使用 jest --env=node或通过设置 testEnvironment node 的选项在您的 Jest 配置中。

    关于node.js - 'FIRESTORE 内部断言失败 : Unexpected state' when unit testing with Jest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63319638/

    相关文章:

    swift - Firebase Cloud Firestore - 从引用访问集合

    c# - 如何测试抽象类的 protected 抽象方法?

    node.js - 如何使用 node.js 设置 ELK

    node.js - JSONAPI实现

    node.js - 使用 Node 请求 OAuth 凭据

    python - 我需要测试功能的哪些方面?

    unit-testing - 如何使用 Jest 测试 img.onerror

    node.js - nwjs Reactjs,对我的上下文感到困惑。文档未定义

    android - 为什么我的 Firebase 字符串不会显示在 myRecyclerView 上

    javascript - 如何停止执行 Firebase 触发的 Firebase 函数?