0
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) Cannot read property 'fn' of undefined となってしまう

Posted at
 TypeError: Cannot read property 'fn' of undefined

      14 |             mockStore[key] = {};
      15 |             Object.keys(store[key]).map(actionKey=>{
    > 16 |                 mockStore[key][actionKey] = jest.fn();

直ったのですが、要因が下記のいずれかだったようです。

考えられる要因1:jestのimportの記述

The Jest Object
jest オブジェクトは、すべてのテストファイル内で自動的にスコープされます。 jest オブジェクトのメソッドはモックの作成に役立ち、Jestの全体的な動作を制御できます。 import {jest} from '@jest/globals' を介して明示的にインポートすることもできます。


import {jest} from '@jest/globals'
// import jest from 'jest' //ではない

考えられる要因2:jest.config.js にresetMocks: trueを追加

jest.config.js
module.exports = {
    resetMocks: true,
}

以上で jest.fn() が呼べるようになりました。
以上です。

0
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
0
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?