chenjuneking / quill-image-drop-and-paste

A quill editor module for drop and paste image, with a callback hook before insert image into the editor
ISC License
101 stars 42 forks source link

Jest error: SyntaxError: Unexpected token 'export' #12

Closed Huihuawk closed 3 years ago

Huihuawk commented 3 years ago

` Jest encountered an unexpected token

This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

Here's what you can do:
 • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
 • If you need a custom transformation specify a "transform" option in your config.
 • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html

Details:

/xxx/node_modules/quill-image-drop-and-paste/src/QuillImageDropAndPaste.js:171
export default ImageDropAndPaste
^^^^^^

SyntaxError: Unexpected token 'export'`

How can I deal with this problem? sorry to bother you.

chenjuneking commented 3 years ago

` Jest encountered an unexpected token

This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

Here's what you can do:
 • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
 • If you need a custom transformation specify a "transform" option in your config.
 • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html

Details:

/xxx/node_modules/quill-image-drop-and-paste/src/QuillImageDropAndPaste.js:171
export default ImageDropAndPaste
^^^^^^

SyntaxError: Unexpected token 'export'`

How can I deal with this problem? sorry to bother you.

@Huihuawk This is because Node.js cannot handle ES6 modules. You should transform your modules to CommonJS.

If you use Babel 7 =>

Install npm install --save-dev @babel/plugin-transform-modules-commonjs

And to use only for test cases add to .babelrc, Jest automatically gives NODE_ENV=test global variable.

"env": {
    "test": {
      "plugins": ["@babel/plugin-transform-modules-commonjs"]
    }
}

or if you use Babel 6 =>

npm install --save-dev babel-plugin-transform-es2015-modules-commonjs

to .babelrc

"env": {
    "test": {
      "plugins": ["transform-es2015-modules-commonjs"]
    }
}

The solution from this link: https://stackoverflow.com/a/49679658/4715721