简体   繁体   English

如何使用 create-react-app 正确配置 material-ui 进行测试?

[英]how to correctly configure material-ui with create-react-app for testing?

I've made an example from the material-ui Testing page from the site.我从网站的 material-ui 测试页面中做了一个例子。 I've made the app using create-react-app and I've imported enzyme into my project.我已经使用 create-react-app 制作了该应用程序,并将酶导入到我的项目中。

import { createMount } from '@material-ui/core/test-utils';
import MyComponent from './src/MyComponent';

describe('<MyComponent />', () => {
  let mount;

 before(() => {
   mount = createMount();
});

after(() => {
   mount.cleanUp();
});

it('should work', () => {
   const wrapper = mount(<MyComponent />);
 });
});

All my tests fail to run throwing the following error ReferenceError: before is not defined我所有的测试都无法运行,抛出以下错误ReferenceError: before is not defined

Everywhere I've read states that I don't need to configure jest, as it comes out of the box with create-react-app.我读过的所有地方都说我不需要配置 jest,因为它是通过 create-react-app 开箱即用的。 Below is my package.json, how am I supposed to configure my application so I can run these tests?下面是我的 package.json,我应该如何配置我的应用程序才能运行这些测试?

{
"name": "client",
"version": "1.3.0",
"private": true,
"license": "private",
"proxy": {
    "/media/*": {
        "target": "website"
    },
    "/updates/*": {
        "target": "website"
    },
    "/okta/*": {
        "target": "website"
    }
},
"homepage": "http:website",
"dependencies": {
    "@material-ui/core": "^1.5.1",
    "@material-ui/icons": "^2.0.3",
    "axios": "^0.17.1",
    "lodash": "^4.17.4",
    "material-ui-pickers": "^1.0.0-beta.12",
    "materialize-css": "^0.100.2",
    "moment": "^2.20.1",
    "prop-types": "^15.6.0",
    "react": "^16.2.0",
    "react-addons-shallow-compare": "^15.6.2",
    "react-dates": "^16.0.1",
    "react-day-picker": "^7.0.7",
    "react-dom": "^16.2.0",
    "react-props": "^0.0.3",
    "react-redux": "^5.0.6",
    "react-router-dom": "^4.2.2",
    "react-scripts": "1.0.17",
    "react-with-direction": "^1.1.0",
    "redux": "^3.7.2",
    "redux-logger": "^3.0.6",
    "redux-saga": "^0.16.0",
    "redux-thunk": "^2.2.0",
    "universal-cookie": "^2.1.2",
    "video.js": "^6.6.0",
    "videojs-contrib-hls.js": "^3.1.0"
},
"scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
},
"devDependencies": {
    "jest-enzyme": "^6.0.4"
}

You need to change before to beforeAll and after to afterAll您需要将 before 改为 beforeAll 和 after 改为 afterAll

Change改变

   before(() => {
       mount = createMount();
   });

    after(() => {
       mount.cleanUp();
    });

To

   beforeAll(() => {
       mount = createMount();
   });

    afterAll(() => {
       mount.cleanUp();
   });

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

相关问题 如何在没有 create-react-app 的情况下使用 Material-UI - How to use Material-UI without create-react-app 如何使用 create-react-app 和 Typescript 中的 css 模块覆盖 Material-UI? - How do I override Material-UI with css modules in create-react-app and Typescript? 当我安装 material-UI 时,由 create-react-app 构建的 React(打字稿版本)应用程序中断 - React (typescript version) app build by create-react-app breaks when I install material-UI create-react-app / Material-UI 应用程序样式在部署时有所不同 - create-react-app / Material-UI App Styling Different on Deploy 我可以在 create-react-app v2 中使用 css 模块覆盖 Material-UI 吗? - Can I override Material-UI with css modules in create-react-app v2? 通过材料界面使用Create-React-App - Use Create-React-App with Material UI 如何在反应应用程序中创建动态 material-ui 元素? - How to create dynamic material-ui elements in a react app? 如何在 React 测试库中获取 Material-UI 密码输入 - How to get a Material-UI password input in React Testing Library 如何使用Material-UI和react-router提供React应用程序 - How to serve React app with Material-UI and react-router 如何动态配置 material-ui 主题? - How to configure material-ui theme on the fly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM