visjs / vis-network

:dizzy: Display dynamic, automatically organised, customizable network views.
https://visjs.github.io/vis-network/
Apache License 2.0
3.06k stars 372 forks source link

Jest Error (Syntax error, unexpected export) #688

Open AgustinBanchio opened 4 years ago

AgustinBanchio commented 4 years ago

Hi, I am having an issue when trying to run jest while using vis-network.

I get the following error:

Details: /node_modules/vis-data/peer/index.js:1 ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export * from "./esm"; ^^^^^^ SyntaxError: Unexpected token export at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1260:14) at node_modules/vis-network/peer/umd/vis-network.js:28:82 at Object.<anonymous> (node_modules/vis-network/peer/umd/vis-network.js:31:2)

I don't understand much about transpiling, jest config and babel config. As far as I have looked online this should be related. Is there any specific config required for this packages?

The versions I am using are:

vis-data@^6.5.1: version "6.5.1" resolved "https://registry.yarnpkg.com/vis-data/-/vis-data-6.5.1.tgz#cd11f8bbf4a17064559b7c75532c3d6f419ff290" integrity sha512-bOIG0REskXot0JbuAYLFRR61xf4ls+nIYNnZ7X04iq2y/Vrg896A/bfquQjhsERZipOmCgllAR0d58OGyo9s0g==

vis-network@^7.6.2: version "7.6.2" resolved "https://registry.yarnpkg.com/vis-network/-/vis-network-7.6.2.tgz#36e69cbedc2d25b11801a390b60ae4e6d03f1783" integrity sha512-8jJTjeJJoF0KB4NwztrsJAMHYW1Blbbavj4Jy20br5LfWJk1djCqv+8HANyeQ7GaPsmiHZ+BgcYM4WsGjpUJIA==

If you have any suggestion on how I could solve this I would appreciate it! Thanks

hesto2 commented 4 years ago

I am running into this issue as well, it is very frustrating

Thomaash commented 4 years ago

You have to provide an MWE (ideally a project where I could simply run npm cit to reproduce the issue as I have no experience with Jest) to have good chances of this getting solved.

From the little you provided so far I guess that Jest can't handle ES6 on it's own and needs something like Babel to convert it to ES5 or maybe just to convert ESM to CJS if it's simply running on Node.

Potential solution 1

Add Babel (or other transpiller) to convert ESM to CJS.

Potential solution 2

All the index files are ESM (they're meant for bundlers which universally handle ESM just fine). If you want to avoid ESM altogether you have to import the final files directly (that is vis-network/peer/umd/vis-network.js).

olgeorge commented 2 years ago

Adding the below to jest.config.js solved it for me:

  transformIgnorePatterns: [
    "/node_modules/(?!vis-network).+\\.js$",
  ],

https://www.angularfix.com/2021/12/jest-fails-with-token-on-import.html

codergautam commented 1 year ago

For me in Next.js importing from vis-network/standalone/umd/vis-network.js worked