Firebaseのプロジェクトにテストを導入してみる(Jest入れるまで)
firebaseプロジェクトにテストを導入してみる。
馴染みがあるjestを使う。やり方は2種類。
Babelで使う
ts-jestを使う
直接Babelは使ってないのでts-jestで。型チェックも効く。
yarn add -D jest @types/jest ts-jest firebase-functions-test @firebase/rules-unit-testing
適当にテストを書くとLintエラー。import/exportがないモジュールは駄目。
index.test.ts cannot be compiled under '--isolatedModules' because it is considered a global script file. Add an import, export, or an empty 'export {}' statement to make it a module.
こんな構成にしつつ、
src/functions ├── __tests__ │ ├── index.test.ts │ └── tsconfig.json ├── index.ts └── tsconfig.json
tests下のtsconfigはこうするとLintエラーが直る。
{ "extends": "../tsconfig.json", "compilerOptions": { "isolatedModules": false }, }
jest.config.js
module.exports = { roots: ['<rootDir>/src'], testMatch: [ '**/__tests__/**/*.+(ts|tsx|js)', '**/?(*.)+(spec|test).+(ts|tsx|js)', ], transform: { '^.+\\.(ts|tsx)$': 'ts-jest', }, }
package.jsonのscriptsに追加
+ "test": "jest"
動いた
% yarn test yarn run v1.16.0 $ jest (node:9850) ExperimentalWarning: The fs.promises API is experimental PASS src/functions/__tests__/index.test.ts ✓ basic (3 ms) Test Suites: 1 passed, 1 total Tests: 1 passed, 1 total Snapshots: 0 total Time: 1.58 s, estimated 6 s Ran all test suites. ✨ Done in 3.13s.
そのうちpathの指定やりたい。
emulatorの起動入れてみる。 package.json修正
"test": "firebase emulators:exec 'jest'"
% yarn test yarn run v1.16.0 $ firebase emulators:exec 'jest' i emulators: Starting emulators: functions, firestore, hosting ⚠ functions: The following emulators are not running, calls to these services from the Functions emulator will affect production: auth, database, pubsub ✔ functions: Using node@10 from host. i firestore: downloading cloud-firestore-emulator-v1.11.11.jar... Progress: ======================================================> (100% of 64MB i firestore: Removing outdated emulator files: cloud-firestore-emulator-v1.8.4.jar i firestore: Firestore Emulator logging to firestore-debug.log i hosting: Serving hosting files from: dist/public ✔ hosting: Local server: http://localhost:5000 i functions: Watching "/Users/yosuke/Work/webapp/tennico/dist/functions" for Cloud Functions... ⚠ It looks like you're trying to access functions.config().algolia but there is no value there. You can learn more about setting up config here: https://firebase.google.com/docs/functions/local-emulator ⚠ TypeError: Cannot read property 'app_id' of undefined at Object.<anonymous> (/Users/yosuke/Work/webapp/tennico/dist/functions/index.js:69:66) at Module._compile (internal/modules/cjs/loader.js:776:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10) at Module.load (internal/modules/cjs/loader.js:653:32) at tryModuleLoad (internal/modules/cjs/loader.js:593:12) at Function.Module._load (internal/modules/cjs/loader.js:585:3) at Module.require (internal/modules/cjs/loader.js:690:17) at require (internal/modules/cjs/helpers.js:25:18) at initializeRuntime (/Users/yosuke/Work/webapp/tennico/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:676:29) at process._tickCallback (internal/process/next_tick.js:68:7) ⚠ We were unable to load your functions code. (see above)
構成変数が読めてないのでこれを実行。
firebase functions:config:get > .runtimeconfig.json
動いた。
参考
セキュリティルールのテスト firebase.google.com
Cloud Functionsのテスト firebase.google.com