Jest-Expo 在示例中崩溃(React.createElement:类型无效——需要一个字符串) [英] Jest-Expo crashes on example (React.createElement: type is invalid -- expected a string)

查看:37
本文介绍了Jest-Expo 在示例中崩溃(React.createElement:类型无效——需要一个字符串)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑为什么示例测试没有运行.我在这里使用带有打字稿的 Expo Web.我的前端使用 expo start --web 运行良好.

I'm very confused on why the example test isn't running. I'm using Expo Web here with typescript. My frontend runs fine using expo start --web.

// App.tsx
const App = () => {
  return (
    <View>
       <Text>Hello world</Text>
    </View>
  );
}

export default App;

我遵循了 expo-jest 文档

// App.test.js
import React from 'react';
import renderer from 'react-test-renderer';

import App from '../App'; // <-- only changed path to match my folder structure

describe('<App />', () => {
    it('has 1 child', () => {
        const tree = renderer.create(<App />).toJSON();
        expect(tree.children.length).toBe(1);
    });
});

但是当我运行 npm test 时,我得到

However when I run npm test, I get

const tree = renderer.create(<App />).toJSON();
                               ^
React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: object.

我知道大多数时候这是关于将默认导出与命名导出混合在一起,但我很清楚使用默认导出和导入.我做错了什么?

I know this is about mixing up default exports with named exports most of the time but I am cleary using a default export and import. What am I doing wrong?

推荐答案

您可能在包含 App.tsx 的目录中有 app.json 文件.Jest 尝试默认导入 app.json 而不是 App.tsx.

You may have app.json file in the directory which also contains App.tsx. Jest tries to import app.json rather than App.tsx by default.

配置 Jest 的 moduleFileExtensions 并放置 tsx在左边.

Configure Jest's moduleFileExtensions and place tsx in the left.

"moduleFileExtensions": ["tsx", ...]

这篇关于Jest-Expo 在示例中崩溃(React.createElement:类型无效——需要一个字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆