2
votes

I am writing unit tests for react native app using jest and enzyme. When I try to run the test cases I get the following error:

● Test suite failed to run

TypeError: Cannot read property 'SHORT' of undefined

at Object. (node_modules/react-native-simple-toast/index.js:7:23) at Object. (src/client/components/Catalogue/events.js:10:29) at Object. (tests/events-test.js:5:13) at Generator.next () at new Promise () at Generator.next () at

The error is due to importing Toast from react-native-simple-toast. Without this import the test runs fine, but I need react-native-simple-toast in my component and hence cannot remove it.I somehow need the test to run with react-native-simple-toast in my component:

import React, { Component } from 'react'
import { Platform, ActivityIndicator, Linking, Dimensions, View, Text, 
TouchableOpacity, TextInput, StyleSheet, Keyboard, Image, 
KeyboardAvoidingView } from 'react-native'
import { Container, Header, Card, CardItem, Content, Left, Right, Body, 
Title, Icon, Button } from 'native-base';
import IconIOSback from 'react-native-vector-icons/Ionicons';
import IconMenu from 'react-native-vector-icons/MaterialIcons';
import axios from 'axios';
import SERVER_URL from '../../config';
import { Col, Row, Grid } from 'react-native-easy-grid';
import Toast from 'react-native-simple-toast';   //this causes the error

Kindly, help me resolve this issue.

3
did you look at events.js?azium
Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a minimal reproducible example. Use the edit link to improve your question - do not add more information via comments. Please add the corresponding source code?!GhostCat

3 Answers

4
votes

to get a way around this is to mock the property SHORT. Create a test setup file eg setup.js and include the following mock

jest.mock('react-native-simple-toast', () => ({
  SHORT: jest.fn(),
}));

Ensure to add this file in your package.json under jest configuration ie

"jest": 
.....
"setupFiles": [
      "/setup.js" // location of your setup.js file
    ]
0
votes

Install

npm install react-native-simple-toast --save

After installation .

react-native link

Then check this module is added or not , insidenode_modules folder

Restart Project and run again

Here is the Discussion

0
votes

Make sure you have run the cd ios && pod install after the package installation.

It perhaps happening due to the existing cache.

  • Close the app in simulator
  • run again with npm run ios