2016-11-07 4 views
3

I反応するプロジェクトを持っており、いくつかのテストにより、以下のセットアップガイド/問題へモジュール反応/ libに/ ReactTestUtils

セットアップしようとしています:

マイpackage.jsonは以下の冗談の設定が含まれます。

"jest": { 
    "bail": false, 
    "collectCoverage": true, 
    "collectCoverageFrom": [ 
     "<rootDir>/src/**/*.js", 
     "!<rootDir>/node_modules/**" 
    ], 
    "coverageDirectory": "coverage", 
    "coveragePathIgnorePatterns": [ 
     "<rootDir>/node_modules" 
    ], 
    "coverageThreshold": { 
     "global": { 
     "branches": 50, 
     "functions": 50, 
     "lines": 50, 
     "statements": 50 
     } 
    }, 
    "globals": { 
     "SOURCEMAP": true 
    }, 
    "modulePaths": [ 
     "<rootDir>/src", 
     "<rootDir>/sass", 
     "<rootDir>/public", 
     "<rootDir>/node_modules" 
    ], 
    "resetModules": true, 
    "testPathDirs": [ 
     "<rootDir>/test" 
    ], 
    "testRegex": "(/test/.*|\\.(test|spec))\\.(js|jsx)$", 
    "verbose": true 
    } 

を私は私のコンポーネント(GenericButton)を持って配置<rootDir>/components/buttons/GenericButton.jsと次のように内容を<rootDir>/test/componnets/buttons/GenericButtonTest.jsで私のテストで:

import React from 'react'; 
import GenericButton from 'components/buttons/GenericButton'; 
import { shallow } from 'enzyme'; 
import { shallowToJson } from 'enzyme-to-json'; 
import ReactTestUtils from 'react-addons-test-utils' 

describe('Generic Button',() => { 
    test('Button action called when clicked',() => { 
     var clicked = false; 
     const component = shallow(
      <GenericButton action={() => { 
       clicked = true; 
      }}/> 
     ); 

     console.log("Clicking the button!"); 
     ReactTestUtils.Simulate.click(component); 
     expect(clicked).toBeTruthy(); 
    }); 
}) 

マイbabelrcファイルは次のとおりです。

{ 
    "presets": [ 
    "es2015-loose", 
    "react", 
    "stage-0" 
    ], 
    "plugins": [ 
    "babel-root-slash-import", 
    "transform-decorators-legacy", 
    "react-hot-loader/babel", 
    "transform-runtime" 
    ], 
    "compact": true, 
    "ignore": [ 
    "/node_modules/(?!react-number-input)" 
    ] 
} 

私はしかし、私のテストを実行したときに、私は次のエラーを取得する:

> [email protected] test C:\path\to\project 
> jest 

FAIL test\components\buttons\GenericButtonTest.js 
    ● Test suite failed to run 

    Cannot find module 'react/lib/ReactTestUtils' from 'index.js' 

     at Resolver.resolveModule (node_modules\jest-resolve\build\index.js:144:17) 
     at Object.<anonymous> (node_modules\react-addons-test-utils\index.js:1:107) 
     at node_modules\enzyme\build\react-compat.js:128:19 

----------|----------|----------|----------|----------|----------------| 
File  | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines | 
----------|----------|----------|----------|----------|----------------| 
All files | Unknown | Unknown | Unknown | Unknown |    | 
----------|----------|----------|----------|----------|----------------| 
Test Suites: 1 failed, 1 total 
Tests:  0 total 
Snapshots: 0 total 
Time:  5.843s 
Ran all test suites. 
    console.error node_modules\enzyme\build\react-compat.js:131 
    react-addons-test-utils is an implicit dependency in order to support [email protected] Please add the appropriate version to your devDependencies. See https://github.com/airbnb/enzyme#installation 

npm ERR! Test failed. See above for more details. 

私はこの簡単なテストに合格するために何ができますか?編集


:ファイル/-アドオンテスト-utilsの反応LIB/ReactTestUtilsの反応が必要とされる/を/ libフォルダを反応node_modulesではありません。

これは15.4.0-rc.3に反応-アドオンテスト-utilsのアップグレードすることで固定されているが、その後私が得た:

TypeError: Cannot read property '__reactInternalInstance$t476n6b4jes0zxw0n18vbzkt9' of undefined 

    at getClosestInstanceFromNode (node_modules\react-dom\lib\ReactDOMComponentTree.js:106:11) 
    at Object.getInstanceFromNode (node_modules\react-dom\lib\ReactDOMComponentTree.js:140:14) 
    at Object.click (node_modules\react-dom\lib\ReactTestUtils.js:326:74) 
    at Object.<anonymous> (test\components\buttons\GenericButtonTest.js:17:67) 

答えて

3

あなたがであなたのテストで一緒にReactTestUtilsenzymeを使用することはできません瞬間幸いにも、酵素を使ってクリックをシミュレートすることもできます。唯一の問題は、simulateがコンポーネントの子に伝播しないため、最初に子を見つけてsimulateを呼び出す必要があることです。

import React from 'react'; 
import GenericButton from 'components/buttons/GenericButton'; 
import { shallow } from 'enzyme'; 
import { shallowToJson } from 'enzyme-to-json'; 

describe('Generic Button',() => { 
    test('Button action called when clicked',() => { 
     var clicked = false; 
     const component = shallow(
      <GenericButton action={() => { 
       clicked = true; 
      }}/> 
     ); 
     component.find('selectrorOfChildWithClickHandler').first().simulate('click') 
     expect(clicked).toBeTruthy(); 
    }); 
}) 
+0

ReferenceError:クリックが定義されていません -------- Object。 (test \ components \ buttons \ GenericButtonTest.js:18:42) -------- process._tickCallback(internal \ process \ next_tick.js:103:7) – mangusbrother

+0

私の答えにはタイプミスがあります。 –

+0

そのエラーが解決しました。 now: メソッド "props"は、単一ノード上でのみ実行されることを意図しています。代わりに0が見つかりました。----- ShallowWrapper.single(node_modules \ enzyme \ build \ ShallowWrapper.js:1401:17) ----- ShallowWrapper.props(node_modules \ enzyme \ build \ ShallowWrapper.js:791:21) (node_modules \ enzyme \ build \ ShallowWrapper.js:762:28) ----- ShallowWrapper.simulate(node_modules \ enzyme \ build \ ShallowWrapper.js:762:28) -----オブジェクトで。 (test \ components \ buttons \ GenericButtonTest.js:17:33) ----- process._tickCallback(internal \ process \ next_tick.js:103:7) – mangusbrother

1

酵素は、これらの依存関係を必要とします:

npm i --save-dev enzyme 
npm i --save-dev react-addons-test-utils 
npm i --save-dev react-dom 

devDependenciesとしてインストールします。これは私のために働いた。

関連する問題