简体   繁体   English

测试捆绑没有反应的 React 组件库时的开玩笑错误

[英]Jest error when testing React Component library bundled without react

I've been building React apps for a while now so I wanted to make a React Component library.我已经构建 React 应用程序有一段时间了,所以我想制作一个 React 组件库。 I've been reading about approaches and went with one where you use Webpack and Babel to bundle without React because if you don't there will be errors because two versions of React will be imports (one in the component library and one in the app).我一直在阅读有关方法,并选择了一种方法,您使用 Webpack 和 Babel 在没有 React 的情况下进行捆绑,因为如果您不这样做会出现错误,因为将导入两个版本的 React(一个在组件库中,一个在应用程序中) )。 Anyway, I have a Jest setup file that has this:无论如何,我有一个 Jest 设置文件,其中包含:

import { configure } from 'enzyme'
import Adapter from 'enzyme-adapter-react-16'

configure({ adapter: new Adapter() })

Also, I have my component library that transpiles and for testing I've created a basic Form Component that basically is just an empty form that I can use it in a demo app but when I do a simple Jest test:另外,我有我的组件库,可以进行转换和测试,我创建了一个基本的表单组件,它基本上只是一个空表单,我可以在演示应用程序中使用它,但是当我做一个简单的 Jest 测试时:

import React from 'react'
import { shallow } from 'enzyme'

import { Form } from './form.js'

describe('Test forms', () => {
  test('render correctly', () => {
    expect(shallow(<Form />)).toEqual(<form />)
  })
})

I get a ReferenceError: React is not defined error.我得到一个ReferenceError: React is not defined错误。 It specifically points to my Form Component's return <form /> line.它特别指向我的表单组件的return <form />行。

Any help would be greatly appreciated.任何帮助将不胜感激。 Also if there is a better approach for building React Component libraries please let me know.另外,如果有更好的方法来构建 React 组件库,请告诉我。 Thanks!谢谢!

Update 1: I've removed enzyme and added react-test-renderer and I get the same error ReferenceError: React is not defined and it still points to the Form Component's return <form /> line.更新 1:我删除了酶并添加了 react-test-renderer,我得到了同样的错误ReferenceError: React is not defined ,它仍然指向表单组件的return <form />行。

Update 2: I switched back to enzyme.更新 2:我切换回酶。 I've commented out my Jest setup file to confirm Jest doesn't run tests without the Adapter configured and got the error you'd expect so I'm confident it's not that.我已经注释掉了我的 Jest 设置文件,以确认 Jest 在没有配置适配器的情况下不会运行测试,并且得到了您期望的错误,所以我相信它不是那样的。 To try to narrow down the problem I imported React in my Component and now get a different error:为了缩小问题的范围,我在我的组件中导入了 React,现在得到了一个不同的错误:

- Expected
+ Received

- <form />
+ ShallowWrapper {}

   8 | describe('Test forms', () => {
   9 |   test('render correctly', () => {
> 10 |     expect(shallow(<Form />)).toEqual(<form />)
     |                               ^
  11 |     // const renderer = ReactTestRenderer.create(<Form />)
  12 |     // expect(renderer.toJSON()).toMatchSnapshot()
  13 |   })

So I'm pretty confident my problem is that since my React Component library isn't including React in the bundle any test with JSX will fail.所以我很有信心我的问题是,因为我的 React 组件库没有在包中包含 React,所以使用 JSX 的任何测试都会失败。 And then once that is fixed I'll have to figure out the Jest failure which is probably just me not reading the updated documentation because a quick glance at there docs shows I should be doing something like this:然后一旦修复,我将不得不找出 Jest 失败,这可能只是我没有阅读更新的文档,因为快速浏览那里的文档表明我应该做这样的事情:

const wrapper = shallow(<Component />);
expect(wrapper.contains(<div className="unique" />)).to.equal(true);

Update 3: Fixed the Jest error by adding enzyme-to-json and comparing the component to a snapshot.更新 3:通过添加酶到 json 并将组件与快照进行比较来修复 Jest 错误。 Still need to research how to include React in the Jest tests.仍然需要研究如何在 Jest 测试中包含 React。

You need to configure Enzyme Adapter before writing tests编写测试前需要配置 Enzyme Adapter

import React from 'react'
import { shallow, configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
configure({ adapter: new Adapter() });

//tests

With jest as a standalone, you'd need the react renderer.jest作为独立的,你需要 react 渲染器。 Since you're using enzyme , you'll need to configure the enzyme adapter in a file called setupTests.js : https://airbnb.io/enzyme/docs/installation/由于您使用的是enzyme ,因此您需要在名为setupTests.js的文件中配置酶适配器: https://airbnb.io/enzyme/docs/installation/

npm i -D enzyme-adapter-react-16
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

configure({ adapter: new Adapter() });

You can then reference that file in your jest.config : https://airbnb.io/enzyme/docs/guides/jest.html然后,您可以在jest.config中引用该文件: https://airbnb.io/enzyme/docs/guides/jest.html

 "setupFilesAfterEnv": ["<rootDir>src/setupTests.js"]

As documented in my updates, I think I have to settle for the solution where I modify my React Components in my React Component library to import React.正如我的更新中所记录的,我认为我必须接受在我的 React 组件库中修改我的 React 组件以导入 React 的解决方案。 Before they were treating React as already included external library.在他们将 React 视为已包含的外部库之前。 Then shallow has a wrapper for testing that was empty so I added the enzyme-to-json library to compare json instead and it worked.然后 shallow 有一个用于测试的包装器是空的,所以我添加了酶到 json 库来比较 json 并且它起作用了。 Now my snapshot reflects the React Components I am trying to render in the Jest tests.现在我的快照反映了我试图在 Jest 测试中呈现的 React 组件。

Unless anyone else can give me a better answer I'll have to settle for this answer but before I was trying to make my library have a external peer dependency for React so to run it it wasn't required that I import it but Jest does require me to import it.除非其他人可以给我一个更好的答案,否则我将不得不接受这个答案,但在我试图让我的库对 React 具有外部对等依赖项之前,我不需要导入它,但 Jest 确实要求我导入它。

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

相关问题 在组件中使用 React.Children 时,Jest 和 React 测试库返回未定义的错误 - Jest and React Testing Library returning undefined error when using React.Children in a component 用Jest测试React组件 - Testing a React Component with Jest 用玩笑测试反应组件 - Testing react component with jest 使用 Jest 测试 React 组件时出现 UnhandledPromiseRejectionWarning - UnhandledPromiseRejectionWarning when testing React component with Jest 如何使用 onClick 函数 Jest/React 测试库测试 React 组件 - How to test react component with onClick function Jest/React testing library 使用 react 测试库和 jest 进行 antd 组件测试 - antd component testing using react testing library and jest React 测试库/Jest - 呈现父组件时不存在子组件 - React Testing Library / Jest - Child component not present when rendering parent component Jest &amp; React &amp; Typescript &amp; React-Testing-Library 错误 - Jest & React & Typescript & React-Testing-Library error “翻译不是一个功能”-反应管理与反应测试库和笑话的反应错误 - 'translate is not a function'-error in react admin with react testing library and jest React 测试库 + Jest:如何测试接收数字然后对其进行格式化的组件 - React testing library + Jest: How to test a component that receives a number then format it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM