2016-06-21 17 views
1

mochaとjsdomを使用してReactの基本ユニットテストを行っています。ここでmochaとjsdomが反応しないユニットユニットテスト

は私のコードです:

var React = require('react/addons'), 
    assert = require('assert'), 
    TodoItem = require('../app/components/todo-item.jsx'), 
    TestUtils = React.addons.TestUtils; 

describe('Todo-item component', function(){ 
    before('render and locate element', function() { 
    var renderedComponent = TestUtils.renderIntoDocument(
     <TodoItem done={false} name="Write Tutorial"/> 
    ); 

    // Searching for <input> tag within rendered React component 
    // Throws an exception if not found 
    var inputComponent = TestUtils.findRenderedDOMComponentWithTag(
     renderedComponent, 
     'input' 
    ); 

    this.inputElement = inputComponent.getDOMNode(); 
    }); 

    it('<input> should be of type "checkbox"', function() { 
    assert(this.inputElement.getAttribute('type') === 'checkbox'); 
    }); 

    it('<input> should not be checked', function() { 
    assert(this.inputElement.checked === false); 
    }); 

});

TodoItem反応コードは次のとおりです。

Unexpected token (10:6) 
    8 | before('render and locate element', function() { 
    9 |  var renderedComponent = TestUtils.renderIntoDocument(
> 10 |  <TodoItem done={false} name="Write Tutorial"/> 
    |  ^
    11 | ); 
    12 | 
    13 |  // Searching for <input> tag within rendered React component 

は、以下の私が使用していますgulpfileがされて:

gulp.task('test', function() { 
    gulp.src('./tst/**/*.js') 
    .pipe(mocha({ 
    reporter: 'spec', 
    compilers: { 
     js: require('babel-core/register') 
    } 
    })); 
}); 

は何

var React = require('react'); 

module.exports = React.createClass({ 
    displayName: 'TodoItem', 

    getInitialState: function() { 
    return { done: this.props.done } 
    }, 

    render: function() { 
    return (
     <label> 
     <input type="checkbox" defaultChecked={this.state.done} /> 
     {this.props.name} 
     </label> 
    ); 
    } 
}); 

私は未定義のトークンを言ってエラーが出ます私は間違っている。私はチュートリアルに従おうとしています。

答えて

0

リンクされたチュートリアルのpackage.jsonからこれをコピーしました。そのようなあなたのテストを実行してみてください:

mocha test/**/*-test.js --compilers js:babel-core/register --recursive

+0

これはコメントではなく、答え:) – robertklep

+0

http://www.bebetterdeveloper.com/coding/getting-started-react-mocha.html – chrisrhyno2003

+0

@でなければなりませんrobertklep right :)私の投稿を今編集しました –

関連する問題