3
2

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 1 year has passed since last update.

テストでモックを使用したら"TypeError: () is not a function"のエラーが出た

Posted at

はじめに

現在学習記録アプリのテストを書いているのですが、モックを使用した際に出たエラーの解決方法をまとめます。

問題

getAllRecordsdeleteRecords2つの関数をモック化したのですが、 "Failed to fetch records: TypeError: (0 , supabaseFunctions_1.getAllRecords) is not a function"のエラーが出ました。
これは 「getAllRecordsが関数ではない」と言われています。

tsx
//モック化
const mockGetAllRecords = jest
.fn()
.mockResolvedValue([ new Record("1", "テスト", 3, "2024-03-16 09:30:48.200885")]  )
jest.mock("../utils/supabaseFunctions", ()=>{
    return{
        getAllRecords:()=>mockGetAllRecords(),
    }})

const mockDeleteRecords = jest
.fn()
.mockResolvedValue([]);
jest.mock("../utils/supabaseFunctions", () => {
    return{
        deleteRecords: () => mockDeleteRecords(),
    }});

解決方法

jest.mock~の部分がダブっていました。
こちらは複数モックを作成する場合も、1回のみ使用するものです。 

tsx
//モック化
const mockGetAllRecords = jest
.fn()
.mockResolvedValue([ new Record("1", "テスト", 3, "2024-03-16 09:30:48.200885")]  )

const mockDeleteRecords = jest
.fn()
.mockResolvedValue([]);

//1つにまとめる
jest.mock("../utils/supabaseFunctions", () => {
    return{
        getAllRecords:()=>mockGetAllRecords(),
        deleteRecords: () => mockDeleteRecords(),
    }});
3
2
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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?