簡體   English   中英

如何使用 create-react-app 正確配置 material-ui 進行測試?

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

我從網站的 material-ui 測試頁面中做了一個例子。 我已經使用 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 />);
 });
});

我所有的測試都無法運行,拋出以下錯誤ReferenceError: before is not defined

我讀過的所有地方都說我不需要配置 jest,因為它是通過 create-react-app 開箱即用的。 下面是我的 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"
}

您需要將 before 改為 beforeAll 和 after 改為 afterAll

改變

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

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

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM