Javascript 本地测试firestore触发器
我正在写一个测试firebase触发器的测试。然而,问题是我不能让它工作。 我想使用本地firestore emulator和Jest来模拟firestore中的更改,并查看触发器是否执行了它需要执行的操作 我需要在测试中使用云功能,并初始化我的应用程序 Setup.js:Javascript 本地测试firestore触发器,javascript,google-cloud-firestore,jestjs,google-cloud-functions,firebase-cli,Javascript,Google Cloud Firestore,Jestjs,Google Cloud Functions,Firebase Cli,我正在写一个测试firebase触发器的测试。然而,问题是我不能让它工作。 我想使用本地firestore emulator和Jest来模拟firestore中的更改,并查看触发器是否执行了它需要执行的操作 我需要在测试中使用云功能,并初始化我的应用程序 Setup.js: const firebase = require('@firebase/testing'); const PROJECT_ID = 'project'; let admin; let db; const setupAdm
const firebase = require('@firebase/testing');
const PROJECT_ID = 'project';
let admin;
let db;
const setupAdmin = async () => {
admin = firebase.initializeAdminApp({
projectId: PROJECT_ID
});
db = admin.firestore();
};
const getAdmin = () => {
return admin;
};
const getDb = () => {
return db;
};
module.exports.setupAdmin = setupAdmin;
module.exports.getAdmin = getAdmin;
module.exports.getDb = getDb;
Test.js
describe('Billing', () => {
let dbRef;
beforeAll(async () => {
const {db, admin} = require('../../../functions/helpers/setup');
dbRef = db;
});
afterAll(async () => {
await Promise.all(firebase.apps().map(app => app.delete()));
console.log(`View rule coverage information at ${COVERAGE_URL}\n`);
});
it('test', async () => {
const mockData = {
'Users/user1': {
uid: 'user1'
},
['Users/user1/Taxes/' + new Date().getFullYear().toString()]: {
totalExpenseEuro: 0
}
};
for (const key in mockData) {
const ref = dbRef.doc(key);
await ref.set(mockData[key]);
}
// Create mockup data
await dbRef.collection('Users').doc('user1').collection('Expenses').doc('expense1').set({
amountEuroInclVAT: 100
});
// Make snapshot for state of database beforehand
const beforeSnap = test.firestore.makeDocumentSnapshot({amountEuroInclVAT: 0}, 'Users/user1/Expenses/expense1');
// Make snapshot for state of database after the change
const afterSnap = test.firestore.makeDocumentSnapshot(
{amountEuroInclVAT: 100},
'Users/user1/Expenses/expense1'
);
const change = test.makeChange(beforeSnap, afterSnap);
// Call wrapped function with the Change object
const wrapped = test.wrap(calculateTaxesOnExpenseUpdate);
wrapped(change, {
params: {
uid: 'test1'
}
});
});
});
现在,当我试图在触发器中访问这个db对象时,主要的问题出现了
const calculateTaxesOnExpenseUpdate = functions.firestore
.document('Users/{uid}/Expenses/{expenseId}')
.onWrite(async (change, context) => {
const {getDb} = require('../helpers/setup'); // This setup is the same as above
let db = getDb();
...
由于某种原因,当我执行像(wait db.collection('Users').get()).get('totalExpenseEuro')
这样的操作时,Jest停止执行我的代码。当我在该行之后设置调试器时,它永远不会被打印出来。那段代码崩溃了,我不知道为什么。我认为DB实例如果没有在我的云触发函数中正确配置的话
问:在测试和云触发函数之间共享DB实例(admin.firestore())的好方法是什么?如果没有任何堆栈跟踪,很难确定错误在哪里。出于这个原因,我发现了这个非常好的教程和其他类似的案例,其中介绍了如何使用Jest对Firestore和云函数进行测试。请检查差异并验证是否可以按照这些链接上的指示进行调试?如果没有任何堆栈跟踪,很难确定错误在哪里。出于这个原因,我发现了这个非常好的教程和其他类似的案例,其中介绍了如何使用Jest对Firestore和云函数进行测试。请检查差异并验证是否可以按照这些链接上的指示进行调试,好吗?