我为我正在构建的 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;
});
这只发生在我尝试测试时,常规应用程序工作正常我尝试过的事情:
解决方案,因为我需要测试数据库中的数据
希望你能帮我 :)
最佳答案
您应该更改 Jest 的默认测试环境 jsdom
至node
使用 jest --env=node
或通过设置 testEnvironment
node
的选项在您的 Jest 配置中。
关于node.js - 'FIRESTORE 内部断言失败 : Unexpected state' when unit testing with Jest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63319638/