runFunctionsTest function

Future<FunctionsTestClient> runFunctionsTest(
  1. FunctionsRunner runner, {
  2. required FirebaseApp adminApp,
})

Sets up a Firebase instance for testing without starting an HTTP server.

The runner callback registers functions exactly as in production (firebase.https.onCall, firebase.https.onRequest, etc.). The returned FunctionsTestClient lets tests invoke those handlers in-process, preserving the full request pipeline (token verification, error handling, streaming) without network overhead or a running emulator.

Pass the adminApp you have already initialised for your test — typically one whose Auth service is wired to a mock certificate HTTP client (see Auth.internal and FirebaseTokenVerifier).

Example:

final client = await runFunctionsTest(
  adminApp: app,
  (firebase) {
    firebase.https.onCall(name: 'echo', (req, _) async {
      return CallableResult({'uid': req.auth?.uid});
    });
  },
);

final response = await client.call('echo', data: {}, idToken: token);
expect(response.statusCode, 200);

See also:

  • FunctionsTestClient for invoking registered functions.
  • EmulatorHelper (in test/e2e/helpers/emulator.dart) for full E2E testing with the Firebase emulator.

Implementation

Future<FunctionsTestClient> runFunctionsTest(
  FunctionsRunner runner, {
  required FirebaseApp adminApp,
}) async {
  final projectId =
      adminApp.options.projectId ??
      (throw ArgumentError(
        'adminApp must have a projectId set in its AppOptions.',
      ));

  FirebaseEnv.mockEnvironment = {'FIREBASE_PROJECT': projectId};
  try {
    final firebase = createTestFirebaseInternal(adminApp);
    await runner(firebase);
    return FunctionsTestClient._(firebase);
  } finally {
    FirebaseEnv.mockEnvironment = null;
  }
}