LoginSignup
11
1

More than 3 years have passed since last update.

enzymeでshallowやmountしてきたcomponentの中身を調べたいとき

Posted at

debug() を使ってください。

const wrapper = shallow(<MyComponent />);
console.log(wrapper.debug());

出力


<MyComponent>
  <Button>ボタン1</Button>
  <Button>ボタン2</Button>
</MyComponent>

同じようなメソッドに html() がありますが、抽出される要素が複数あるとエラーになります。

const wrapper = shallow(<MyComponent />);
console.log(wrapper.find('Button').html()); //エラーになる
// Method “html” is meant to be run on 1 node. 2 found instead.

debug()を使ってください。
抽出されたものが全部出ます。

const wrapper = shallow(<MyComponent />);
console.log(wrapper.find('Button').debug());

出力

<Button>ボタン1</Button>

<Button>ボタン2</Button>

2ヶ月前に知りたかった。

参考
https://airbnb.io/enzyme/docs/api/ShallowWrapper/debug.html

11
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
1