Adrian
Adrian

Reputation: 85

Getting SyntaxError: Unexpected token export when trying to import a function to test with Mocha

I am trying to set up Mocha testing for a function I have which is also being used in a React application. I am currently running in circles where I either get an error when trying to import the function for Mocha to use or when I am trying to import the function into my React Component to use and am starting to get frustrated. I am close to giving up and just copying the function I want to test directly into the test file which I would hate to do because of repetition.

My current setup is as follows and the import works in my React component but gets a "SyntaxError: Unexpected token export" error in Mocha.

This function is saved in src/function/helpers.js

export const functionName = () => { 
   // Function logic here
}

React component function import saved in src/components/Component.js

import {functionName} from '../functions/helpers';

Mocha Test file saved in test/basic-test.js

const functionName = require("../src/function/helpers").functionName;

How can I import the function into both files without getting either a React error or an error from Mocha?

Upvotes: 0

Views: 1149

Answers (1)

Alexander
Alexander

Reputation: 1390

You need to connect the babel to compile es6 syntax.

package.json:

"scripts": {
   "test": "mocha --require @babel/register"
},

Upvotes: 1

Related Questions