【问题标题】:Unable to run jest test case无法运行笑话测试用例
【发布时间】:2018-05-11 10:37:02
【问题描述】:

我编写了一个小测试用例来测试ThankYouPage 组件,如下所示

import ToggleDisplay from 'react-toggle-display';
import styles from '../styles.css';

function ThankYouPage(props) {
  return (
    <ToggleDisplay show={props.show}>
      <div className={styles.thankyouText}> Thank you!</div>
      <div className={styles.helpText}>
        The more you thanks, the better.
      </div>
    </ToggleDisplay>
  );
}

export default ThankYouPage;

以下是开玩笑的测试用例-

import React from 'react';
import ThankYouPage from './components/thank-you-page';
import { shallow } from 'enzyme';


describe('<ThankYouPage />', () => {
  it('renders 1 ThankYouPage component', () => {
    const component = shallow(<ThankYouPage show=true />);
    expect(component).toHaveLength(1);
  });
});

以下是我在运行npm test后得到的控制台跟踪

> myreactapp@1.0.0 test /Users/rahul/myreactapp
> jest

 FAIL  tests/thank-you-page.test.js
  ● Test suite failed to run

    /Users/cominventor/myreactapp/tests/thank-you-page.test.js: Unexpected token (8:30)
         6 | describe('<ThankYouPage />', () => {
         7 |   it('renders 1 ThankYouPage component', () => {
      >  8 |     const component = shallow(<ThankYouPage show=true />);
           |                               ^
         9 |     expect(component).toHaveLength(1);
        10 |   });
        11 | });

我是否缺少在浅层中解释 jsx 的依赖项?以下是我的部门的样子

"devDependencies": {
    "babel-jest": "^22.4.3",
    "oc-template-react-compiler": "5.0.2",
    "prettier": "^1.10.2",
    "prettier-eslint": "^8.8.1"
  },
  "dependencies": {
    "axios": "^0.18.0",
    "enzyme": "^3.3.0",
    "jest": "^22.4.3",
    "jsdom": "^11.10.0",
    "querystring": "^0.2.0",
    "react-cookie": "^2.1.4",
    "react-scripts": "^1.1.4",
    "react-toggle-display": "^2.2.0"
  }

【问题讨论】:

标签: javascript reactjs jestjs


【解决方案1】:

尝试安装

npm i --save-dev enzyme enzyme-adapter-react-16

将 transform-class-properties 添加到 .babelrc 文件中,如下所示 -

"plugins": [
    "transform-class-properties"
  ]

在 package.json 的顶层添加以下内容

"jest": {
    "moduleNameMapper": {
      "\\.(css)$": "jest-css-modules"
    }

接下来的单元测试应该可以正常工作。

import React from 'react';
import Enzyme, { shallow } from 'enzyme';
import ThankYouPage from './components/thank-you-page';
import Adapter from 'enzyme-adapter-react-16';
Enzyme.configure({ adapter: new Adapter() });

describe('<ThankYouPage />', () => {
  it('renders 1 ThankYouPage component', () => {
    const component = shallow(<ThankYouPage show=true />);
    expect(component).toHaveLength(1);
  });
});

【讨论】:

  • Enzyme.configure 对我不起作用,但 configure 起作用了。此外,我必须使用 Håken Lid 建议的线程对 babelrc 进行更改。剩下的就是得到开玩笑的过程import styles from '../styles.css';。我尝试安装 jest-css-modules 但似乎我需要一些 babel 设置才能让它工作
  • 解决 css 问题的方法是包含 jest-css-modules
  • 我建议您通过包含上述内容来完成答案。我认为没有 css 建议,答案是不完整的。
  • 如果 css 有问题,你必须在 package.json 中包含这些行 "jest": { "transform": { ".*": "/node_modules/babel-jest " }, "moduleNameMapper": { "\\.(css|scss)$": "/node_modules/jest-css-modules" } }
【解决方案2】:

试试这个:

    import * as React from 'react';
    import * as Adapter from 'enzyme-adapter-react-16';
    import {shallow, configure} from 'enzyme';
    import ThankYouPage from './components/thank-you-page';

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

   describe('my test', () => {
     ...
     ...
   });

从 npm 安装 enzyme-adapter-react-16 包。

【讨论】:

  • 另外,根据我使用其他语言的经验,将 * 作为 React 导入的想法看起来有点糟糕。我认为我们应该只进口需要的东西。瓦特赛?
【解决方案3】:

你以错误的方式导入酶(不是相对路径),试试import { shallow } from 'enzyme';

【讨论】:

  • 感谢@krystian-sztadhaus。这很有帮助,但没有解决问题:(
猜你喜欢
  • 2021-05-17
  • 2020-07-12
  • 2019-09-23
  • 1970-01-01
  • 2018-06-10
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多