YSNHatenaBlog

主にアプリやWebサービス開発について

Cloud Functionsのテスト導入中

CloudFunctionsテスト導入。

このエラーが出る。

% 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: 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...
✔  functions[nextApp]: http function initialized (http://localhost:5001/tennico-f93a4/us-central1/nextApp).
✔  functions[courtCreated]: firestore function initialized.
✔  functions[courtUpdated]: firestore function initialized.
i  Running script: jest
 FAIL  src/functions/__tests__/index.test.ts
  ● Test suite failed to run

    src/functions/__tests__/index.test.ts:2:8 - error TS1259: Module '"/Users/yosuke/Work/webapp/tennico/node_modules/firebase-functions-test/lib/index"' can only be default-imported using the 'esModuleInterop' flag

    2 import functionsTest from 'firebase-functions-test'
             ~~~~~~~~~~~~~

      node_modules/firebase-functions-test/lib/index.d.ts:4:1
        4 export = _default;
          ~~~~~~~~~~~~~~~~~~
        This module is declared with using 'export =', and can only be used with a default import when using the 'esModuleInterop' flag.

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        5.213 s

tsconfig見てない?試しに__test__直下にtsconfig置いて、compilerOptionsを親のものをコピペしてみる。

% 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: 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...
✔  functions[nextApp]: http function initialized (http://localhost:5001/tennico-f93a4/us-central1/nextApp).
✔  functions[courtCreated]: firestore function initialized.
✔  functions[courtUpdated]: firestore function initialized.
i  Running script: jest
 FAIL  src/functions/__tests__/index.test.ts
  ● Test suite failed to run

    Could not find a valid build in the '/Users/yosuke/Work/webapp/tennico/src/functions/next' directory! Try building your app with 'next build' before starting the server.

      4 | import * as url from 'url'
      5 | 
    > 6 | const app = next({
        |                 ^
      7 |   dev: false,
      8 |   dir: __dirname,
      9 |   conf: {

      at Server.readBuildId (node_modules/next/next-server/server/next-server.ts:1934:15)
      at new Server (node_modules/next/next-server/server/next-server.ts:192:25)
      at Function.createServer [as default] (node_modules/next/server/next.ts:41:10)
      at Object.<anonymous> (src/functions/index.ts:6:17)
      at Object.<anonymous> (src/functions/__tests__/index.test.ts:6:1)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        1.77 s
Ran all test suites.
(node:13284) ExperimentalWarning: The fs.promises API is experimental
⚠  Script exited unsuccessfully (code 1)
i  emulators: Shutting down emulators.
i  functions: Stopping Functions Emulator
i  hosting: Stopping Hosting Emulator
i  firestore: Stopping Firestore Emulator
i  hub: Stopping emulator hub

Error: Script "jest" exited with code 1
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

nextが呼ばれてしまうので、nextの設定がいろいろないとテストが実行されなそう。 functionsごとに別ファイルで切り出して、next以外の関数だけimportする。

更にハマる。またこれ。

FAIL  src/functions/__tests__/index.test.ts
  ● Test suite failed to run

    src/functions/__tests__/index.test.ts:2:8 - error TS1259: Module '"/Users/yosuke/Work/webapp/tennico/node_modules/firebase-functions-test/lib/index"' can only be default-imported using the 'esModuleInterop' flag

    2 import functionsTest from 'firebase-functions-test'
             ~~~~~~~~~~~~~

      node_modules/firebase-functions-test/lib/index.d.ts:4:1
        4 export = _default;
          ~~~~~~~~~~~~~~~~~~
        This module is declared with using 'export =', and can only be used with a default import when using the 'esModuleInterop' flag.

こうじゃなくて

import functionsTest from 'firebase-functions-test'

これでよかった

import * as functionsTest from 'firebase-functions-test'

前はlintエラーだったのに起きなくなった。

そしてこれ。え?まじか。

Cannot encode [object Object]to a Firestore Value. Local testing does not yet support Firestore geo points.

オブジェクトの構造だけ合わせる。

-          geo: new admin.firestore.GeoPoint(35.635557, 139.786987),
+          geo: {
+            _latitude: 35.635557,
+            _longitude: 139.786987,
+          },

あとはalgoliasearchをモックできれば動きそうな気配。