最近firebaseを使ってアプリを作ることが多くあります。
ちょくちょくテストコードを書いていたので、せっかくなら自動テストしたいと思いgithub actions上でci作った忘備録です。
前提
PR出したときにfirebase emulatorでテストを走らせる。
ついでに静的解析もやる。
まずpackage.jsonにlinterとテストコードのscriptsを作る。
今回はfunctionsとfirestoreを動かしながらjestでテストする。
--projectはfirebaseのprojectを指定
package.json
{
"scripts": {
"format": "prettier -c './**/*.{○○,△△}'",
"lint": "eslint --ext .○○,△△ .",
"test": "firebase emulators:exec --project=○○ 'jest' --only functions,firestore"
}
}
workflow
firebase操作できるツールあるかなと思ったけどなかったのでglobalにfirebase-toolsを直でインストールして動かす。(deploy系はあった。)
ルートディレクトリで実行しない場合はworking-directoryを指定する必要がある。
cd functions && ...
でも可
pull_request.yml
name: static analysis and test
'on':
pull_request:
paths: #functions/が変わった時のみ動かす
- 'functions/**'
jobs:
test:
if: '${{ github.event.pull_request.head.repo.full_name == github.repository }}'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v2
with:
node-version: '16'
- name: install firebase-tools
run: npm install -g firebase-tools
- name: install node_modules
run: npm ci
- name: linter
run: |
npm run format
npm run lint
working-directory: ./functions
- name: test
run: npm run test
working-directory: ./functions