Post

Unit Test: Cannot set properties of undefined (settings 'call')

React Navigation, Testing with Jest - Mocking native modules에서 react-native-reanimated/mock를 Mock 할때 나타나는 오류로 Unit Test 시 node_modules이 변환되는데 때때로 module 내부의 모든 파일이 변환되지 않아 Jest가 module의 코드를 이해하지 못해 발생하는 오류이다. 문제를 해결하려면 transformIgnorePatterns를 사용하여 module을 변환시키지 않고 사용하면 된다.

Error Message

 FAIL  src/__tests__/App-test.js
 ● Test suite failed to run

   TypeError: Cannot set properties of undefined (setting 'call')

      8 |   // The mock for `call` immediately calls the callback which is incorrect
      9 |   // So we override it with a no-op
   > 10 |   Reanimated.default.call = () => {};
        |                          ^
     11 |
     12 |   return Reanimated;
     13 | });

     at setupTests.js:10:26
     at Object.<anonymous> (node_modules/@react-navigation/drawer/lib/src/DrawerView.tsx:22:1)
     at Object.<anonymous> (node_modules/@react-navigation/drawer/lib/src/createDrawerNavigator.tsx:20:1)

Solution

  1. jest.config.js 안에 transformIgnorePatterns 를 설정한다.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    
    module.exports = {
      transformIgnorePatterns: [
        "node_modules/(?!" +
          [
            "(jest-)?react-native",
            "react-clone-referenced-element",
            "@react-native-community",
            "rollbar-react-native",
            "@fortawesome",
            "@react-native",
            "@react-navigation",
            "react-native-vector-icons",
          ].join("|") +
          ")",
      ],
    };
    

Reference

This post is licensed under CC BY 4.0 by the author.