简体   繁体   English

React Jest匹配快照,使用子组件测试组件时崩溃

[英]React Jest to match snapshot, crash when testing component with child components

I have some components with child components from third parties addons/libraries. 我有一些组件,这些组件带有第三方插件/库中的子组件。 I use Jest for my unit test and toMatchSnapshot() method. 我将Jest用于单元测试和toMatchSnapshot()方法。 I tried to exclude the child components with jest.unmock('ChildComponet.js') and i get this error: 我试图用jest.unmock('ChildComponet.js')排除子组件, jest.unmock('ChildComponet.js')此错误:

jest.unmock('ChildComponet.js') was called but automocking is disabled. 调用了jest.unmock('ChildComponet.js'),但禁用了自动模拟功能。 Remove the unnecessary call to jest.unmock or enable automocking for this test via jest.enableAutomock(); 删除不必要的对jest.unmock调用,或者通过jest.enableAutomock();启用此测试的自动jest.enableAutomock(); . This warning is likely a result of a default configuration change in Jest 15. 此警告很可能是Jest 15中默认配置更改的结果。

I enabled jest.enableAutomock(); 我启用了jest.enableAutomock(); and now i have tis error: 现在我有此错误:

TypeError: Cannot read property 'DEFINE_MANY' of undefined TypeError:无法读取未定义的属性“ DEFINE_MANY”

I put this on my package.json but nothing happens: 我把它放在我的package.json上,但是什么也没发生:

"unmockedModulePathPatterns": ["rootDir/node_modules/react"] “ unmockedModulePathPatterns”:[“ rootDir / node_modules / react”]

Any ideas? 有任何想法吗?

is the correct way to make unit test of components in React? React中对组件进行单元测试的正确方法是什么?

The easiest way to mock out react components I found so far is to use: 模拟到目前为止到目前为止发现的组件的最简单方法是使用:

jest.mock('component', ()=> 'ComponentName')

before you the import statement of the module you want to test. 在要测试的模块的import语句之前。

The first parameter is either the name of global npm module or the path to your local component (note that the path in relative to your test file). 第一个参数是全局npm模块的名称或本地组件的路径(请注意,相对于测试文件的路径)。 The second parameter is just a function that returns a string (I always return the same name I use in jsx). 第二个参数只是一个返回字符串的函数(我总是返回我在jsx中使用的相同名称)。 This will result in a dump component that does nothing but have the same name as your original component. 这将导致转储组件不执行任何操作,但其名称与原始组件相同。 So in your snapshot you will see no difference, beside the mocked component will not render any childs. 因此,在快照中您将看不到任何区别,除了模拟组件不会渲染任何子项。

Now to the error messages you got. 现在到您收到的错误消息。

jest.unmock('ChildComponet.js') was called but automocking is disabled... jest.unmock('ChildComponet.js')被调用但是自动模拟被禁用了...

The problem is that you use jest.unmock instead of jest.mock . 问题是您使用jest.unmock而不是jest.mock Jest has the feature to automatically mock all the dependencies of your modules. Jest具有自动模拟模块所有依赖项的功能。 With auto mock enabled you could use jest.unmock to get the real implantation for some essential libs like lodash or moment. 启用自动模拟后,您可以使用jest.unmock来获取lodash或moment等一些基本库的真实植入。 As the auto mock feature was confusing for a lot of people, they decide to make it optional. 由于自动模拟功能使很多人感到困惑,因此他们决定将其设为可选。 Tldr you tried to un mock something that was not mocked in the first place as you don't enabled auto mocking. 在Tldr中,您尝试取消模拟最初未模拟的内容,因为您未启用自动模拟。

TypeError: Cannot read property 'DEFINE_MANY' of undefined TypeError:无法读取未定义的属性“ DEFINE_MANY”

When you enable auto mocking every imported module is replaced with undefined . 启用自动模拟时,每个导入的模块都将被undefined替换。 I cant say much about the unmockedModulePathPatterns setting but I believe you have to use the same pattern you use to import the module, so if it is a global you don't have to put the path to the node_modules folder in it. 对于unmockedModulePathPatterns设置,我不能说太多,但我相信您必须使用与导入模块相同的模式,因此,如果它是全局的,则不必在其中放置node_modules文件夹的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM