Firebase functions test


Jest testing firebase functions with emulator-suite

Firebase プロジェクトについて理解する

Firebase プロジェクト、アプリ、プロダクトの関係

Firebase プロジェクトと Google Cloud Platform(GCP)の関係

Firebase プロジェクトとアプリ接続の設定

Firebase プロジェクトの管理

一般的なベスト プラクティス

ローカルでの関数の実行

https://firebase.google.com/docs/functions/local-emulator
Firebase CLI には、次のタイプの関数をエミュレートできる Cloud Functions エミュレータが含まれています。

  • HTTPS 関数(Cloud Functions エミュレータ)
  • 呼び出し可能関数(Cloud Functions エミュレータ)
  • Cloud Firestore 関数

関数を本番環境にデプロイする前に、ローカルで実行してテストできます。

Firebase CLI をインストールする

管理者の認証情報を設定する

エミュレータ スイートを実行する

firebase emulators:start

emulators:start コマンドは、ローカル プロジェクトで firebase init を使用して初期化したプロダクトに基づいて、Cloud Functions、Cloud Firestore、Realtime Database、Firebase Hosting のエミュレータを起動します。特定のエミュレータを起動する場合は --only フラグを使用します。

firebase emulators:start --only functions

エミュレータの起動後にテストスイートやテスト スクリプトを実行する場合は、emulators:exec コマンドを使用します。

firebase emulators:exec "./my-test.sh"

他のサービスとのインタラクション

  • Cloud Firestore
  • Firebase Hosting

次のステップ

Cloud Functions に TypeScript を使用する

TypeScript で新しい Cloud Functions プロジェクトを初期化する

firebase init 
#JavaScript と TypeScript のどちらを使ってプロジェクトをビルドするかを選択できます。
#ここでは TypeScript を選択します。

既存の TypeScript プロジェクトを使用する

既存の JavaScript プロジェクトを TypeScript に移行する

TypeScript 関数をエミュレートする

TypeScript プロジェクトの Functions ログ

firebase deploy の実行時に、プロジェクトの index.ts が index.js にトランスパイルされます。つまり、Cloud Functions ログは、デベロッパーが作成したコードではなく index.js ファイルから行番号を出力します。index.ts で対応するパスと行番号を見つけやすくするために、firebase deploy は functions/lib/index.js.map を作成します。このソースマップは、お好みの IDE またはノード モジュールで使用できます。

環境の構成

プロジェクトの環境構成を設定する

現在の環境構成を取得する

関数で環境構成にアクセスする

環境構成を使用してモジュールを初期化する

Cloud Functions の単体テスト

テストのセットアップ

関数フォルダで次のコマンドを実施して、firebase-functions-test とテスト フレームワークの Mocha の両方をインストールします。

npm install --save-dev firebase-functions-test
npm install --save-dev mocha

次に、関数フォルダ内に test フォルダを作成し、その内部にテストコード用の新しいファイルを作成して index.test.js のような名前を付けます。

最後に、functions/package.json を変更して次を追加します。

package.json
"scripts": {
  "test": "mocha --reporter spec"
}

テストを作成し終えたら、関数ディレクトリ内で npm test を実施することによりこれらのテストを行うことができます。

  • オフライン モードで SDK を初期化する

完全にオフラインのテストを作成する場合は、パラメータを指定せずに SDK を初期化できます。

index.test.js
// At the top of test/index.test.js
const test = require('firebase-functions-test')();
  • 構成値のモック

関数コードで functions.config() を使用すると、構成値をモックできます。

functions/index.js
const functions = require('firebase-functions');
const key = functions.config().stripe.key;
functions/index.test.js
// Mock functions config values
test.mockConfig({ stripe: { key: '23wr42ewr34' }});

  • 関数のインポート
functions/index.test.js
// after firebase-functions-test has been initialized
const myFunctions = require('../index.js'); // relative path to functions code

// If index.js calls admin.initializeApp at the top of the file,
// we need to stub it out before requiring index.js. This is because the
// functions will be executed as a part of the require process.
// Here we stub admin.initializeApp to be a dummy function that doesn't do anything.
adminInitStub = sinon.stub(admin, 'initializeApp');
// Now we can require index.js and save the exports inside a namespace called myFunctions.
myFunctions = require('../index');