2016-08-02 89 views
0

我的測試文件的代碼是:無法導入Reactjs類與摩卡測試和酶

var React = require('react'); 
var {shalow} = require('enzyme') 
var {SearchBox} = require('../static/js/functions') 
var expect = require('expect'); 

describe('Test', function(){ 
    it('1', function(){ 
    expect(true).toEqual(true); 
    }); 
}); 

這是我作出反應類functions.js

var SearchBox = React.createClass({ 
    render: function() { 
    return (
     <div> 
     <SearchList data={this.props.data}></SearchList> 
     <li> 
      <button id="previous_page" className="previous_page" onClick={back}>Previous</button> 
      <button id="next_page" className="next_page" onClick={next}>Next</button> 
     </li> 
     </div> 
    ) 
    } 
}); 

據工作很好,但我想寫測試,當我運行它時,它輸出以下錯誤:

[email protected]:~/Desktop/Kamal Hasan/pedialink$ mocha ./js_test/*.js /home/irtza/Desktop/Kamal Hasan/pedialink/static/js/functions.js:39 , ^SyntaxError: Unexpected token < at exports.runInThisContext (vm.js:53:16) at Module._compile (module.js:511:25) at Object.Module._extensions..js (module.js:550:10) at Module.load (module.js:456:32) at tryModuleLoad (module.js:415:12) at Function.Module._load (module.js:407:3) at Module.require (module.js:466:17) at require (internal/module.js:20:19) at Object. (/home/irtza/Desktop/Kamal Hasan/pedialink/js_test/test.js:3:19) at Module._compile (module.js:541:32) at Object.Module._extensions..js (module.js:550:10) at Module.load (module.js:456:32) at tryModuleLoad (module.js:415:12) at Function.Module._load (module.js:407:3) at Module.require (module.js:466:17) at require (internal/module.js:20:19) at /usr/local/lib/node_modules/mocha/lib/mocha.js:220:27 at Array.forEach (native) at Mocha.loadFiles (/usr/local/lib/node_modules/mocha/lib/mocha.js:217:14) at Mocha.run (/usr/local/lib/node_modules/mocha/lib/mocha.js:485:10) at Object. (/usr/local/lib/node_modules/mocha/bin/_mocha:405:18) at Module._compile (module.js:541:32) at Object.Module._extensions..js (module.js:550:10) at Module.load (module.js:456:32) at tryModuleLoad (module.js:415:12) at Function.Module._load (module.js:407:3) at Function.Module.runMain (module.js:575:10) at startup (node.js:159:18) at node.js:444:3

回答

0

您需要更新節點(mocha取決於node.js)版本(以支持es6模塊)或使用requireJS - module.exports ... +導出組件您應該檢查您的節點版本支持的es6的哪些部分,因爲傳遞給摩卡的代碼不是「babelized」:)

+0

它是6.0的最新 –

+0

檢查:https://kangax.github.io/compat-table/es6/如果您不使用JS的不受支持的部分。加上什麼是下一個和後面(傳遞給onClick)?定義在哪裏? – Moonjsit

0

您的代碼是ES5和ES6語法的混合。

我認爲,從這個代碼部分來的問題:

var React = require('react'); 
var {shalow} = require('enzyme'); 
var {SearchBox} = require('../static/js/functions'); 

如果你想使用ES5語法:

var React = require('react'); 
var shalow = require('enzyme').shalow; // notice how we access object property `.objectProperty` 
var SearchBox = require('../static/js/functions'); 

如果你想使用ES6語法:

import React from 'react'; 
import {shalow} form 'enzyme'; // notice how we access object property `{objectProperty}` 
import SearchBox form '../static/js/functions'; 

也會確保在您的function.js文件的結尾處導出組件:

module.exports = SearchBox; // ES5 syntax 
export default SearchBox; // ES6 syntax