用于测试安全规则的 Firestore 模拟器 - 运行 测试
Firestore emulator for testing security rules - running the tests
我已经按照 enter link description here 中的说明安装了模拟器,我可以启动它,到目前为止一切顺利。
在这里和那里挑选一些代码后,我编写了我的第一个测试,这里是:
import * as firebasetesting from '@firebase/testing';
import * as firebase from 'firebase';
import * as fs from 'fs';
const projectId = 'my-firebase-project';
const rules = fs.readFileSync('firestore.rules', 'utf8');
beforeAll(async () => {
// Make your test app load your firestore rules
await firebasetesting.loadFirestoreRules({ projectId, rules });
});
beforeEach(async () => {
// Reset our data from our test database
await firebasetesting.clearFirestoreData({ projectId });
});
after(async () => {
// Shut down all testing Firestore applications after testing is done.
await Promise.all(firebasetesting.apps().map(app => app.delete()));
});
describe("TRACKERS AND ALLIES", () => {
it('TRACKER UP', async () => {
let user = {username: "Bob", uid: 'bobuid'}
let target = { username: "Alice", uid: 'aliceuid'}
const auth = { uid: bob.uid, token: {isadmin: false} };
const app = firebasetesting.initializeTestApp({ projectId, auth }).firestore();
const ref = app.doc('users/'+ user.uid + '/contact/' + target.uid);
await firebasetesting.assertSucceeds(ref.update({ up: true, username: target.uid, timestamp: firebase.firestore.FieldValue.serverTimestamp() }));
});
})
而我的问题很简单:我该如何运行呢?
编辑:我可能只是补充说我是 Firestore 的新手,一般来说 Javascript...上面的 link 只是说明
After running a suite of tests, you can access test coverage reports that show how each of your security rules was evaluated.
所以我想它一定很简单,但是我在任何地方都找不到 "run" 命令...
如果你有一个 nodejs 脚本,运行 它与 node your-script.js
。您必须安装节点。
如果您想 运行 将脚本与模拟器一起运行,并在脚本完成后关闭模拟器,您链接到的页面显示:
In many cases you want to start the emulator, run a test suite, and
then shut down the emulator after the tests run. You can do this
easily using the emulators:exec command:
firebase emulators:exec --only firestore "./my-test-script.sh"
如果您发现文档令人困惑或不完整,您应该使用页面右上角的 "send feedback" 按钮。
我已经按照 enter link description here 中的说明安装了模拟器,我可以启动它,到目前为止一切顺利。
在这里和那里挑选一些代码后,我编写了我的第一个测试,这里是:
import * as firebasetesting from '@firebase/testing';
import * as firebase from 'firebase';
import * as fs from 'fs';
const projectId = 'my-firebase-project';
const rules = fs.readFileSync('firestore.rules', 'utf8');
beforeAll(async () => {
// Make your test app load your firestore rules
await firebasetesting.loadFirestoreRules({ projectId, rules });
});
beforeEach(async () => {
// Reset our data from our test database
await firebasetesting.clearFirestoreData({ projectId });
});
after(async () => {
// Shut down all testing Firestore applications after testing is done.
await Promise.all(firebasetesting.apps().map(app => app.delete()));
});
describe("TRACKERS AND ALLIES", () => {
it('TRACKER UP', async () => {
let user = {username: "Bob", uid: 'bobuid'}
let target = { username: "Alice", uid: 'aliceuid'}
const auth = { uid: bob.uid, token: {isadmin: false} };
const app = firebasetesting.initializeTestApp({ projectId, auth }).firestore();
const ref = app.doc('users/'+ user.uid + '/contact/' + target.uid);
await firebasetesting.assertSucceeds(ref.update({ up: true, username: target.uid, timestamp: firebase.firestore.FieldValue.serverTimestamp() }));
});
})
而我的问题很简单:我该如何运行呢?
编辑:我可能只是补充说我是 Firestore 的新手,一般来说 Javascript...上面的 link 只是说明
After running a suite of tests, you can access test coverage reports that show how each of your security rules was evaluated.
所以我想它一定很简单,但是我在任何地方都找不到 "run" 命令...
如果你有一个 nodejs 脚本,运行 它与 node your-script.js
。您必须安装节点。
如果您想 运行 将脚本与模拟器一起运行,并在脚本完成后关闭模拟器,您链接到的页面显示:
In many cases you want to start the emulator, run a test suite, and then shut down the emulator after the tests run. You can do this easily using the emulators:exec command:
firebase emulators:exec --only firestore "./my-test-script.sh"
如果您发现文档令人困惑或不完整,您应该使用页面右上角的 "send feedback" 按钮。