【问题标题】:Jest + Typescript lint error: TS2339: Property 'store' does not exist on type 'IntrinsicAttributesJest + Typescript lint 错误:TS2339:“IntrinsicAttributes”类型上不存在属性“存储”
【发布时间】:2018-01-14 19:16:48
【问题描述】:

我的 Jest 测试在 react native 上运行,但是当我检查 Typescript linter 时出现此错误:

error TS2339: Property 'store' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<Component<{}, ComponentState>> & Readonly<{ childr...'.

顺便说一句,我正在研究 react-native。

这是抛出错误的笑话: const home = shallow(<Home store={store}/>)

【问题讨论】:

    标签: reactjs typescript react-native jestjs


    【解决方案1】:

    您的Home 很可能是connect() 返回的包装组件。 在这种情况下,有几个选项可以在测试中处理这个问题:

    1) 如果您不需要连接的道具和操作进行测试,那么只需使用未包装的Home 组件,如此处所述https://github.com/reactjs/redux/blob/master/docs/recipes/WritingTests.md#connected-components

    2) 用Provider 包裹你的组件,例如; wrapper = mount(<Provider store={store}><Home /></Provider>。缺点是您的快照也将包含有关 Provider 的信息,您不能仅使用浅层测试 Home 组件。

    3) 在一些.d.ts 文件中扩展IntrinsicAttributes,如下所示:

    declare namespace JSX {
      interface IntrinsicAttributes {
        store: any;
      }
    }
    

    并将其包含在测试中。

    【讨论】:

    • 不,浅点是不要使用 mount 所以我拒绝这个答案。使用 mount 会渲染整个树,人们想要使用 shallow 是有原因的,因此将测试更改为使用 mount 并不是没有副作用的更改,您是在建议改变他们实际完全测试的方式
    • 我如何将它包含在测试中?在哪里?怎么样?
    猜你喜欢
    • 2018-01-25
    • 2016-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-09
    • 2018-01-03
    • 2018-03-25
    相关资源
    最近更新 更多