我正在为我的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数据库,即使是我每天使用的应用程序,仍会出现相同的错误。
奇怪的是,数据正在写入数据库,但错误仍然阻止测试。
这里是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;
});
只有在测试时才会出现这种情况,普通应用程序运行正常
我尝试过的事情:
- Firestore规则是读取和写入为true,因此不是规则错误
- 使用firebase-mock模拟Firestore并使用Jest似乎工作正常,但这不是解决方案,因为我需要测试数据库中的数据
希望你能帮助我 :)