Test the Default Cloud Function Locally

Testing the default cloud function locally

Let’s first integrate a simple Cloud Function with the Firebase Local Emulator Suite to make sure everything works as expected.

Click Run and open services/web/firebase/functions/src/index.ts and comment out the three last lines. This is a very basic HTTPS function that returns a string when it’s called.

We also have to make a small configuration change to services/web/firebase/functions/tsconfig.json. In particular, add the following to the compilerOptions object.

{
  "compilerOptions": {
    ...
    "typeRoots": ["./node_modules/@types"]
  },
  ...
}

Restricting TypeScript compiler

By default, the TypeScript compiler (tsc) looks for type definitions from the directory it is invoked in, all the way up to the root of the file system. Since we are in a monorepo and have node_modules directories at various levels in the hierarchy, we want to restrict tsc to the types stored in the closest node_modules/@typings directory only.

Since our Cloud Functions are written in TypeScript, we need to compile them to JavaScript before we can run them locally. Update the dev script in services/web/firebase/package.json to match the following:

"dev": "npm run build --prefix functions && firebase emulators:start",

Click Run to save the changes.

Get hands-on with 1400+ tech skills courses.