TypeError:无法读取未定义的属性"contextTypes"

Jua*_*eno 13 unit-testing reactjs jestjs enzyme

我正在尝试使用Jest测试React-app,我使用Enzyme的浅层来渲染我的App.js组件,App-test-js但是我收到了这个错误:TypeError: Cannot read property 'contextTypes' of undefined

这是我的App.js:

/* global google */
import React, { Component } from 'react';
import Geosuggest from 'react-geosuggest';
import { getAirQuality } from './Client'
import DataTable from './DataTable'
import Errors from 'react-errors'


class App extends Component {

  .
  .
  .

  render() {
    return (
      <div className="App">
        <form onSubmit={this.searchAirQuality.bind(this)}>
          <Geosuggest
            placeholder="Type a location and press SEARCH button!"
            onSuggestSelect={this.onSuggestSelect.bind(this)}
            initialValue={this.state.place}
            location={new google.maps.LatLng(53.558572, 9.9278215)}
            radius="20"/>
          <button className="my-button" type="submit" disabled={!this.state.place}>Button</button>
        </form>
        <DataTable items={this.state.items} />
        <Errors
          errors={this.state.errors}
          onErrorClose={this.handleErrorClose}
        />
      </div>
    )
  }
}

export default App;
Run Code Online (Sandbox Code Playgroud)

这是我的App-test.js:

import React from 'react'
import { shallow } from  'enzyme'
import App from '../components/App.js'

describe( '<App />', () => {
  it('Button disable when input is empty', () => {
    const App = shallow(<App />);

    expect(App.find('.my-button').hasClass('disabled')).to.equal(true);

  });

});
Run Code Online (Sandbox Code Playgroud)

这是运行时的错误npm test:

终端截图

我是第一次开玩笑测试,有人可以帮我解决这个错误吗?

san*_*ari 29

这里的问题是你正在使用浅调用的结果重新定义app组件

//    Redefining
//    ?
const App = shallow(<App />);
Run Code Online (Sandbox Code Playgroud)

解决方案是使用不同的名称:

//    Use a different name
//    ?
const app = shallow(<App />);
Run Code Online (Sandbox Code Playgroud)

  • 使用 `const wrapper = Shallow(&lt;App /&gt;);` 总是好的,因为 `shallow()` 的返回是一个浅层封装。 (3认同)

Ser*_*Ser 20

TypeError: Cannot read property 'contextTypes' of undefined当您导入不存在的内容时,这将是相同的错误.

这是一个例子:(
AccountFormComponent.jsx不正确的类名):

export class FoeFormComponent extends React.Component { .... }
Run Code Online (Sandbox Code Playgroud)

AccountFormComponent.test.jsx:

import { shallow } from 'enzyme'
import { expect, assert } from 'chai'
import { AccountFormComponent } from '../../src/components/AccountFormComponent

describe('', function () {
  it('', function () {
    const enzymeWrapper = shallow(<AccountFormComponent {...props} />)
  })  
})  
Run Code Online (Sandbox Code Playgroud)

只需将以下内容添加到测试文件中即可确保组件存在:

it('should exists', function () {
    assert.isDefined(AccountFormComponent)
})
Run Code Online (Sandbox Code Playgroud)

其打印AssertionError: expected undefined to not equal undefined代替

  • 这对我来说是个问题.我不小心通过解构思考导入导出是正常的,而不是"导出默认值".不得不改变:`import {MyComponent}从'./ index'`到:`import MyComponent from'./ index'` (9认同)

Las*_*zlo 6

在我的情况下,导入只有一个默认导出但我使用单次导入的模块时发生错误。

所以代替:

import { Foo } from './Foo'
Run Code Online (Sandbox Code Playgroud)

使用:

import Foo from './Foo'
Run Code Online (Sandbox Code Playgroud)

Foo具有默认导出的位置:

class Foo extends Component {
  render() {
   return (<div>foo</div>)
  }
}
export default Foo;
Run Code Online (Sandbox Code Playgroud)