Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocha will not recognise JSX

I am trying to update my unit tests by using mocha and enzyme. The code that I am testing is in ES6, using JSX and React.

I have been unable to get mocha to not error on the JSX in my test script.

Test script:

import React from 'react';
import assert from 'assert';
import { shallow } from 'enzyme';
import SamplePageMain from '../SamplePageMain';

describe('<SamplePageMain />', () => {

    var samplePage = shallow(<SamplePageMain />);

    it('should render', function () {
        assert.notEqual(samplePage, null);
    });

});

gulpfile.js:

require('babel-core/register');

...

gulp.task('test', function() {
    return gulp.src('scripts/**/test/*.js', {read: false})
        .pipe(mocha());
});

and the output is:

gulp test

[16:19:06] Using gulpfile ~/dev/bikini/gulpfile.js
[16:19:06] Starting 'test'...
[16:19:06] 'test' errored after 62 ms
[16:19:06] SyntaxError in plugin 'gulp-mocha'
Message:
        /Users/me/dev/bikini/scripts/components/test/samplePageMain.js:     Unexpected token (9:26)
Details:
    pos: 206
    loc: [object Object]
    _babel: true
    codeFrame:    7 | 
   8 | 
>  9 |  var samplePage = shallow(<SamplePageMain />);
     |                           ^
  10 | 
  11 |  it('should render', function () {
  12 |      assert.notEqual(samplePage, null);
Stack:
SyntaxError:     /Users/me/dev/bikini/scripts/components/test/samplePageMain.js:     Unexpected token (9:26)
   7 | 
   8 | 
>  9 |  var samplePage = shallow(<SamplePageMain />);
     |                           ^
  10 | 
  11 |  it('should render', function () {
  12 |      assert.notEqual(samplePage, null);
    at Parser.pp.raise (/Users/me/dev/bikini/node_modules/babel-    register/node_modules/babel-core/node_modules/babylon/index.js:1425:13)
    at Parser.pp.unexpected (/Users/me/dev/bikini/node_modules/babel-    register/node_modules/babel-core/node_modules/babylon/index.js:2907:8)
    at Parser.pp.parseExprAtom     (/Users/me/dev/bikini/node_modules/babel-register/node_modules/babel-    core/node_modules/babylon/index.js:754:12)
    at Parser.pp.parseExprSubscripts     (/Users/me/dev/bikini/node_modules/babel-register/node_modules/babel-    core/node_modules/babylon/index.js:509:19)
    at Parser.pp.parseMaybeUnary     (/Users/me/dev/bikini/node_modules/babel-register/node_modules/babel-    core/node_modules/babylon/index.js:489:19)
    at Parser.pp.parseExprOps     (/Users/me/dev/bikini/node_modules/babel-register/node_modules/babel-    core/node_modules/babylon/index.js:420:19)
    at Parser.pp.parseMaybeConditional     (/Users/me/dev/bikini/node_modules/babel-register/node_modules/babel-    core/node_modules/babylon/index.js:402:19)
    at Parser.pp.parseMaybeAssign     (/Users/me/dev/bikini/node_modules/babel-register/node_modules/babel-    core/node_modules/babylon/index.js:365:19)
    at Parser.pp.parseExprListItem     (/Users/me/dev/bikini/node_modules/babel-register/node_modules/babel-    core/node_modules/babylon/index.js:1232:16)
    at Parser.pp.parseCallExpressionArguments     (/Users/me/dev/bikini/node_modules/babel-register/node_modules/babel-    core/node_modules/babylon/index.js:585:20)

I have had the test successfully run by running the source code through browserify and putting it in a test directory to prove that it is not mocha/enzyme itself. My problem is just trying to get the gulp magic right.

like image 583
brendangibson Avatar asked Feb 10 '16 00:02

brendangibson


People also ask

How do I enable JSX in React?

import React from "react"; import ReactDOM from "react-dom"; const App = () => { return ( React. createElement("p", null, "This is first JSX Element!"); React. createElement("p", null, "This is another JSX Element"); ); }; const rootElement = document. getElementById("root"); ReactDOM.

Is JSX XML or HTML?

JSX stands for JavaScript XML.

How do I run a JSX file?

Because JSX files are plain text files, you can open them in any text editor, such as Microsoft Notepad (Windows) or Apple TextEdit (Mac). However, if you plan to edit a JSX file, you should open it in a source code editor, such as Microsoft Visual Studio (Windows) or Github Atom (cross-platform).

Is Babel required for JSX?

If you work on a React project, chances are you have to deal with Babel. It is needed for 2 main tasks: To compile JSX into React. createElement API calls.


1 Answers

This has been a very common problem for users of Babel 6, which on its own (babel-core) doesn't do anything. It requires that transforms / plugins are fed to it during transpilation.

Babel offers bundles of common plugins as presets. Common for React projects are babel-preset-2015, babel-preset-react and babel-preset-stage-0. After npm installing them, add a .babelrc config file that looks something like this:

{
  "presets": ["react", "es2015", "stage-0"]
}

For mocha with gulp checkout this stack gulp-mocha how to pass the compilers flag?.

And read here about setting up Babel 6 generally https://babeljs.io/blog/2015/10/31/setting-up-babel-6

like image 50
azium Avatar answered Sep 28 '22 15:09

azium