Reputation: 59
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.
Upvotes: 3
Views: 3374
Reputation: 1759
Make sure you have run the cd ios && pod install
after the package installation.
It perhaps happening due to the existing cache.
npm run ios
Upvotes: 0
Reputation: 71
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
]
Upvotes: 7
Reputation: 2299
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
Upvotes: -1