Jestjs与Expo-cli。永久违反:元素类型无效

时间:2019-07-28 09:04:04

标签: javascript reactjs react-native jestjs expo

Jest似乎无法使用我导入的组件。

这通常是导出/导入设置方式的问题,但是我的控制台日志显示该组件已导入文件,但没有使其进入describe函数(请参见下面的内联注释)

import time
import random

def print_pause(message_to_print):
    print(message_to_print)
    time.sleep(2)

correct = []
incorrect = [] 




def intro():

    print_pause("Welocme to Maurice favorite Homemade pizza                                                 creation.""\n")
    print_pause("if you can guess 5 of my favorite toppings correct"' '
                "You have won the game.""\n")
    print_pause("So lets begin the the game.""\n")

def play_game(correct,incorrect):
    print_pause("Please enter in Maurice Favorite Meat topping")
    meat = input("1. Pepperoni\n"
                 "2. Sausage\n"
                 "3. Bacon\n")
    if meat == '2':

                 print_pause ('Correct!' ' ' + meat + ' ' 'is one of Maurice Favorite'' '                                 'toppings')                                    
                 correct.append(meat)



    elif meat == '1' or meat =='3':
                 print_pause ('Incorrect!' ' '+ meat + ' ' 'is not one of Maurice Favorite'                               ' ' 'toppings')
                 incorrect.append(meat) 

    else:
                 print_pause("Sorry, Please enter in a valid choice.")
                 play_game(correct,incorrect)              

    print_pause("Please enter in Maurice Favorite style crust")
    crust = input("1. Thin\n"
                  "2. Chesses\n"
                  "3. Thick\n")
    if crust == '3':

                 print_pause ('Correct!' ' ' + crust + ' ' 'is one of Maurice Favorite'' '                                 'toppings') 
                 correct.append(crust)

    elif crust == '1' or crust =='2':
                 print_pause ('Incorrect!' ' '+ crust + ' ' 'is not one of Maurice Favorite'                               ' ' 'toppings')
                 incorrect.append(crust) 
    else:
                 print_pause("Sorry, Please enter in a valid choice.")
                 play_game(correct,incorrect)

    print_pause("Please enter in Maurice Favorite cheese topping")
    cheese = input("1. Mozzarella\n"
                   "2. Provolone\n"
                   "3. Chedder\n")
    if cheese == '3':

                 print_pause ('Correct!' ' ' + cheese + ' ' 'is one of Maurice Favorite'                                    ' ' 'toppings')  
                 correct.append(cheese)
    elif cheese == '1' or cheese =='2':
                 print_pause ('Incorrect!' ' '+ cheese + ' ''is not one of Maurice Favorite'                               ' ' 'toppings')
                 incorrect.append(cheese)
    else:
                 print_pause("Sorry, Please enter in a valid choice.")
                 play_game(correct,incorrect)

    print_pause("Please enter in Maurice Favorite onion topping")
    onion = input("1. Red Onion\n"
                 "2. White Onion\n"
                 "3. Yellow Onion\n")
    if onion == '2':

                 print_pause ('Correct!' ' ' + onion + ' ' 'is one of Maurice Favorite'                                   ' ''toppings')  
                 correct.append(onion)
    elif onion == '1' or onion =='3':
                 print_pause ('Incorrect!' ' '+ onion + ' ' 'is not one of Maurice Favorite'                               ' ''toppings')
                 incorrect.append(onion)
    else:
                 print_pause("Sorry, Please enter in a valid choice.")
                 play_game(correct,incorrect) 

def play_again():
    while True:
     a = input("enter yes/no to continue\n")
     if a =="yes":
        play_game(correct,incorrect)
     else: a =="no"
     exit()

intro()
play_game(correct,incorrect)               
play_again()   

组件:

import ValidInput from '../components/ui/ValidInput';
console.log(typeof ValidInput) // 'function'

describe('ValidInput', () => {
    it('Renders succesfully', () => {
        console.log(typeof ValidInput) // 'undefined'
        const validInputInst = renderer.create(
            <View>
                <ValidInput 
                    inputType='email'
                    validationRules={{
                        isEmail: true,
                    }}
                />
            </View>
        )
        const ValidInput = validInputInst.root.findByType(ValidInput)
    })
})

错误:一直违反:元素类型无效:预期为字符串(对于内置组件)或类/函数(对于复合组件),但得到:未定义。您可能忘记了从定义文件中导出组件,或者可能混淆了默认导入和命名导入。

预计将能够渲染组件

1 个答案:

答案 0 :(得分:0)

事实证明问题出在这一行:

const ValidInput = validInputInst.root.findByType(ValidInput)

和另一个在这里: inputType='email'

由于某些原因,尽管在describe()范围内未定义ValidInput,但是如果您尝试分配一个名为ValidInput(const ValidInput)的变量,则在导入与上面相同的名称之后,将抛出Invariant Violation错误好像您要尝试调用您尚未导入的电话一样。