Unable to import Reactjs Class for testing with mocha and enzyme

My test file's code is:

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);
  });
});

This is my React class in 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>
    )
  }
});

It is working fine but I want to write tests and when I run it, it outputs the following error :

irtza@irtza-Lenovo-G50-70:~/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


您需要更新Node(mocha取决于node.js)版本(以支持es6模块)或使用requireJS - module.exports ... +导出组件。+您应该检查您的节点版本支持的es6的哪些部分,因为代码已通过摩卡是不是“babelized”:)


Your code is a mix between ES5 and ES6 syntax.

I think that the problem come from this portion of code :

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

If you wanna use ES5 syntax :

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

If you wanna use ES6 syntax :

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

also make sure at the end of your function.js file, you export the component with :

module.exports = SearchBox; // ES5 syntax
export default SearchBox; // ES6 syntax
链接地址: http://www.djcxy.com/p/33028.html

上一篇: 命令“npm”返回一个错误

下一篇: 无法导入用于摩卡和酶测试的Reactjs类