1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

jestでfirebase-adminのauth().verifyIdToken()をモックする

Last updated at Posted at 2021-07-26

環境
TypeScript, Node(Hapi)

###コード

import admin from "firebase-admin";

const decoded = adimn.auth().verifyIdToken(token);

このverifyIdToken(token)で返すデータをJestでモックしたかった。

###テスト

test.ts
let spy: jest.SpyInstance;

afterEach(() => {
  spy.mockRestore();
});

test("テスト", () => {
  const admin = require("firebase-admin");
  const mockVerifyIdToken = jest.fn().mockResolvedValue({ uid: "uid" });
  const mockAuth = jest.fn().mockReturnValue({ verifyIdToken: mockVerifyIdToken });
  spy = jest.spyOn(admin, "app").mockReturnValue({ auth: mockAuth });
})

複数のテストで違う値を返したかったのでspyOnを使いました。

これで

const decoded = adimn.auth().verifyIdToken(token);

の部分は{ uid: "uid" }を返しました。


ちゃんとは読んでないけど参考

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?