[Vuejs]-TypeError: mxgraph is not a function When Running Jest Unit Tests

0👍

The mxGraph npm package has always been a mess.

Notice that maxGraph, the mxGraph successor, will handle it: https://github.com/maxGraph/maxGraph

As you are using Vue and your snippet indicates you are using TypeScript, I suggest you to use https://github.com/typed-mxgraph/typed-mxgraph. You will get TypeScript types and a fully working solution for the mxGraph npm integration. The README provides comprehensive usage examples and demos are also available.
When using mxGraph in jest test, depending on the mxGraph parts you are running, you will need to use the jsdom environment in the related tests. This is because mxGraph uses the window object, that is not available with the node environment.

If you still want to keep the js way for the mxGraph integration, you can have a look at https://github.com/jgraph/mxgraph/issues/175#issuecomment-482008896 or at other stack overflow questions (they are plenty).

Leave a comment