6

React 12.2 から React 13.3 にアップグレードすると、テスト スイートで次のエラーが発生します。

エラー: 不変違反: unmountComponentAtNode(...): ターゲット コンテナーは DOM 要素ではありません。

このブログ投稿を使用して、Jasmine を使用してコードをテストしています。エラーは次のコードで発生します。

describe("A component", function() {

  var instance;
  var container = document.createElement("div");

  afterEach(function() {
    if (instance && instance.isMounted()) {
      // Only components with a parent will be unmounted
      React.unmountComponentAtNode(instance.getDOMNode().parent);
    }
  });
  ...rest of test suite...
  // the instances of renderIntoDocument that cause the failure look like the below
  it("Causes my test suite to fail.", function() {
    instance = TestUtils.renderIntoDocument(<MyReactElement/>);
  });
)};

私はそれgetDOMNode()が非推奨であることを知っていますが、それがエラーの原因ではありません。

を検査するinstance.getDOMNode()と、指定されたインスタンスが返されますが、エラーが発生した場合、instance.getDOMNode().parentは未定義です。React.unmountComponentAtNodeこの未定義の変数を呼び出すと、エラーが発生します。

このような回答は、ある種の競合状態が発生していることを示唆していますが、それが私のテスト スイートにどのように適用されるかはわかりません。助けてくれてありがとう!

4

1 に答える 1

6

修正は変更することです:

React.unmountComponentAtNode(instance.getDOMNode().parent);

に:

React.unmountComponentAtNode(instance.getDOMNode().parentNode);

getDOMNode()または、 からに移動する場合findDOMNode():

React.unmountComponentAtNode(React.findDOMNode(instance).parentNode);
于 2015-06-27T15:39:32.627 に答える