找不到酶-适配器-反应16的声明文件? [英] Could not find declaration file for enzyme-adapter-react-16?

查看:86
本文介绍了找不到酶-适配器-反应16的声明文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间以来,我一直在使用酶来测试React应用程序中的组件。在几周内首次更新软件包后,我开始从测试中得到错误。

I've been using Enzyme to test components in my React application for some time. After updating my packages for the first time in a few weeks I've started getting an error from my tests.

 FAIL  src/__tests__/title.test.ts
  ● Testing title component › renders
Enzyme Internal Error: Enzyme expects an adapter to be configured, but found none. [...] 
To find out more about this, see http://airbnb.io/enzyme/docs/installation/index.html

我继续按照链接中的说明安装'enzyme-adapter-react-16'并将以下行添加到我的测试文件:

I proceed to install 'enzyme-adapter-react-16' as described in the link and add the following lines to my test file:

import * as enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
enzyme.configure({ adapter: new Adapter() });

但是,由于我的应用程序是用TypeScript编写的,所以我现在遇到了两个新问题。

However, as my application is written in TypeScript, I now run into two new problems.


图像第一个错误TS7016是 enzyme-adapter-react-16 没有任何类型,第二个错误TS2339说没有属性 configure

To clarify the images the first error, TS7016, is that there aren't any types for enzyme-adapter-react-16, and the second error, TS2339, says that enzyme does not have the property configure.

我是相对较新的到TypeScript,所以我需要一些帮助。我尝试为 enzyme-adapter-react-16 安装类型,但这些似乎不存在。

I'm relatively new to TypeScript so I need some help. I've tried to install types for enzyme-adapter-react-16 but those do not appear to exist.

我应该尝试自己添加它们,还是可以通过某种方式避免这个问题?

也很好奇是什么导致了此错误。我以前不需要适配器,为什么要现在?

Also curious what made this error appear. I didn't need an Adapter before, why now?

推荐答案


我应该尝试添加它们吗?我自己,还是有某种方法可以完全避免这个问题?

Should I attempt to add them myself, or is there some way I can avoid this problem all together?

我相信您是正确的,目前没有用于 enzyme-adapter-react-16 -这是很新的东西,因此似乎还没有人创建。

I believe you are correct that there are currently no types for enzyme-adapter-react-16 - it's pretty new, so it would seem that no one has created any yet.

您可以自己创建它们,如果您认为它们定义良好,则可以将它们贡献给绝对输入供其他人使用。为了避免该问题,您可以让TypeScript忽略它没有类型信息的事实。

You can create them yourself, and if you thought they were well defined, you could potentially contribute them to DefinitelyTyped for others to use. To "avoid" the issue, you can get TypeScript to ignore the fact that it has no type information.

忽略类型错误:


  • 对于第一个错误,错误消息尝试帮助添加新的声明(.d.ts)文件...。因此,您可以创建一个文件(我通常将其放在名为 / types的文件夹中),该文件的名称类似于 enzymeAdapter.d.ts ,并将内容设置为声明模块'enzyme-adapter-react-16'; (来自错误消息)。这基本上告诉打字稿只将导入的对象视为任何类型(即,不进行类型检查)。

  • For the first error, the error message tries to help with the "add a new declaration (.d.ts) file ...". So, you could create a file (I usually put it in a folder called "/types") called something like enzymeAdapter.d.ts, and make the contents declare module 'enzyme-adapter-react-16'; (from the error message). This basically tells typescript to just treat the imported object as any type (ie. no type checking).

对于第二个错误,您可以将导入转换为 any ,然后调用配置。例如:(任何酶均可).configure({adapter:new Adapter()}); 。如果tslint抱怨使用 any ,则可以使用 // tslint:disable-next-line:no-any忽略该行。 以上评论。

For the second error, you can cast the enzyme import to any, then call configure. For example: (enzyme as any).configure({ adapter: new Adapter() });. If tslint complains about using any, you can get it to ignore just that line with a // tslint:disable-next-line:no-any comment above.

完整代码:

import * as enzyme from 'enzyme';
import * as Adapter from 'enzyme-adapter-react-16';

// tslint:disable-next-line:no-any
(enzyme as any).configure({ adapter: new Adapter() });




也很好奇是什么导致了此错误。我以前不需要适配器,为什么呢?

Also curious what made this error appear. I didn't need an Adapter before, why now?

这是酶的新设计选择版本3的项目-您可以阅读有关版本2.x的主要更改的详细信息此处。我认为适配器方法是使处理支持不同版本的React的不同需求更容易,更一致。

This is a new design choice from the enzyme project for version 3 - you can read details about the major changes from version 2.x here. I think the adapter approach is to make handling the different requirements for supporting different versions of react easier and more consistent.

这篇关于找不到酶-适配器-反应16的声明文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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