Home javascript SyntaxError: Unexpected token 'export'

SyntaxError: Unexpected token ‘export’

Author

Date

Category

tell me how to solve the problem with the tests
my main js (activeBtnClick.js):

export default function activeButtonClick (button) {
 if (button.classList.contains ( 'active')) {
 button.classList.remove ( 'active');
 } else {
 button.classList.add ( 'active');
 }
}

activeBtnClick.test.js – test:

const activeButtonClick = require ( './ activeBtnClick');
describe ( "activeBtnClick", () = & gt; {
 const pencil = document.createElement ( 'div');
 activeButtonClick (pencil);
 it ( "add class to the button", () = & gt; {
 . I expect (pencil.classList.contains ( 'active')) toEqual (true);
 });
});

jest.config.js HAS NO
babel NOT use
use webpack
package.json:

{
 "Name": "simple-piskel-clone",
 "Version": "1.0.0",
 "Description": "Simple Piskel Clone",
 "Main": "index.js",
 "scripts": {
  "Test": "Echo \" error: no test specified \ "& amp; & amp; exit 1",
  "Build": "webpack",
  "Watch": "webpack --watch",
  "Test": "jest"
},
 "Author": "",
 "License": "ISC",
 "DevDependencies": {
  "Css-loader": "^ 3.4.0",
  "Eslint-loader": "^ 3.0.3",
  "Html-webpack-plugin": "^ 3.2.0",
  "Jest": "^ 24.9.0",
  "Node-sass": "^ 4.13.0",
  "Sass-loader": "^ 8.0.0",
  "Style-loader": "^ 1.1.1",
  "Url-loader": "^ 3.0.0",
  "Webpack": "^ 4.41.4",
  "Webpack-cli": "^ 3.3.10"
 },
 "Dependencies": {
  "Eslint": "^ 6.8.0",
  "Eslint-config-airbnb-base": "^ 14.0.0",
  "Eslint-plugin-import": "^ 2.19.1"
 }
}

Error:


Answer 1, Authority 100%

Apparently, Jest without Babel does not understand the ESM (import and export is the syntax ECMAScript Modules) – then, in the syntax CommonJS be so:

module.exports = function activeButtonClick () {...}
const activeButtonClick = require ( './ activeBtnClick.js');

Answer 2

has worked like that for me.

npx mocha --require ts-node / register --require esm src / ** / *. Spec.ts

or package.json scripts (both options work, use one that is more like it):

"scripts": {
  "Test": "mocha -r esm -r ts-node / register src / ** / * spec.ts.",
  "Test-ts": "ts-mocha -r esm -p tsconfig.json src / ** / * spec.ts."
}

The main thing do not forget to add a esm to devDependencies :

npm install --save-dev esm

In order to package.json was

"devDependencies": {
  "Esm": "^ 3.2.25"
}

If you use the ts-mocha is it just to add to the package.json .

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions