简体   繁体   English

使用 Jest 进行单元测试时出现“FIRESTORE 内部断言失败:意外状态”

[英]'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:我为我正在构建的 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

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我还尝试使用我一直用来开发端点的凭据连接到我的常规 firestore 数据库,即使它是我每天使用的应用程序也会弹出相同的错误

Weird thing is, data is being written to the database, but error still stops testing奇怪的是,正在向数据库写入数据,但仍然报错停止测试

Here is firebase setup:这是 firebase 设置:

(src/db/functions.js)

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

Function throwing the error Function 抛出错误

(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
}

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: 'admin@test.com',
        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 Firestore 规则读写为真,所以不是规则错误
  • 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使用 firebase-mock 和 Jest 模拟 Firestore 似乎工作正常,但这不是解决方案,因为我需要测试数据库中的数据

Hope you can help me:)希望你能帮我:)

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.您应该使用jest --env=node或通过在 Jest 配置中将testEnvironment选项设置为node将 Jest 的测试环境从默认的jsdomnode

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 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

This is a open issue on GitHub. I'm pasting my comment from that issue here to hopefully help some other people:这是 GitHub 上的一个未决问题。我将我对该问题的评论粘贴到此处,希望能帮助其他人:

I experienced the same error message on 9.6.6 with NextJS.我在使用 NextJS 的 9.6.6 上遇到了相同的错误消息。 I believe this error message could be presented due to a range of errors - as I see 100+ Stackoverflow questions with this error message.我相信由于一系列错误可能会出现此错误消息- 因为我看到 100 多个 Stackoverflow 问题带有此错误消息。

After lots of debugging I realized I accidently used SQL capitalization:经过大量调试后,我意识到我不小心使用了 SQL 大写:

.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.要解决此问题,您必须准确找到它发生的位置并缩小调试范围。

execute this command with what is indicated a little above yarn test jest --env=node after this the error disappears使用上面显示的内容执行此命令yarn test jest --env=node在此之后错误消失

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法编写 firestore 测试,因为:FIRESTORE (9.6.1) INTERNAL ASSERTION FAILED: Unexpected state - Can't write firestore tests because of: FIRESTORE (9.6.1) INTERNAL ASSERTION FAILED: Unexpected state Firebase - 使用 Jest 进行测试会抛出错误“内部断言失败:需要一个 class 定义” - Firebase - Testing with Jest throws error "INTERNAL ASSERTION FAILED: Expected a class definition" 如何用玩笑重置我的 Cloud Functions 的 Firestore 单元测试 - How to reset firestore unit testing my Cloud Functions with jest “FIRESTORE 内部断言失败:文档引用无效。文档引用必须有偶数个段,但 NewGame 有 1 个” - "FIRESTORE INTERNAL ASSERTION FAILED: Invalid document reference. Document references must have an even number of segments, but NewGame has 1" Flutter 错误 - 断言失败:第 213 行 pos 15:'data:= null':在从 firestore 获取数据时不正确 - Flutter error - failed assertion: line 213 pos 15: 'data != null': is not true at the time of fetching data from firestore 使用 Jest 和 AWS-SDK-Mock 进行单元测试,模拟 DynamoDB 扫描 - Unit Testing Using Jest and AWS-SDK-Mock, Mock DynamoDB Scan Firestore 事务在云功能中使用时表现出意外行为 - Firestore transactions showing unexpected behaviour when used in cloud functions 在 firebase/auth 中使用 createUserWithEmailAndPassword 时,Jest 遇到了 Next.js 和 TypeScript 的意外令牌 - Jest encountered an unexpected token with Next.js and TypeScript when using createUserWithEmailAndPassword in firebase/auth 在本地测试 Cloud Functions 时 Cloud Firestore 模拟器未运行 - Cloud Firestore emulator not running when testing Cloud Functions locally 如何用 Jest 模拟 Firestore v9 getDocs() - How to mock Firestore v9 getDocs() with Jest
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM