0
urlパラメータを使用するコンポーネントの単体テストを記述しています。React:url paramを使用してテストを中断する
BarcodePage line 14: TypeError: Cannot read property 'name' of undefined
ここにテストコード:
import React from 'react';
import { shallow } from 'enzyme';
import BarcodePage from './BarcodePage';
const component = shallow(
<BarcodePage />
);
describe('<BarcodePage />',() => {
it('render one header',() => {
expect(component.find('h1').length).toBe(1);
});
})
コンポーネントは、テストが失敗しているにもかかわらず、正常に動作するようですが、それはビルドを中断しますのですが
テストは、常にこのエラーで失敗しています私の実装がわからなくなってしまいます。 部品コード:
import React, { Component } from 'react';
class BarcodePage extends Component {
constructor(props) {
super(props);
}
render() {
return (
<h1>Barcode view {this.props.params.name}</h1>
);
}
}
export default BarcodePage;
注:私は、コンストラクタなしthis.props
が未定義になることに反応ドキュメントを読み込むので、私は、私はそれが必要だとは思わないとき、コンストラクタを持っている理由があります。
を働かせた方法です'あなたのコンポーネントに' params.name'のゲッターが失敗しています。単純な '{this.props.params.name} 'の代わりに' {this.props.params && this.props.params.name} 'のコンポーネントでこれを試してください – yuyokk
これで、' ReferenceError:props is defined'でテストが行われました。 – user3462064
テストで ' ' –
yuyokk