CesiumJS with Jest

278 views
Skip to first unread message

jhwohl...@gmail.com

unread,
Jan 6, 2020, 9:46:33 PM1/6/20
to cesium-dev
1. A concise explanation of the problem you're experiencing.
I cannot use CesiumJS with Jest


2. A minimal code example. If you've found a bug, this helps us reproduce and repair it.
Project Directory:
/home/vagrant/dev/sandbox

Import into unit test with:
import * as Cesium from 'cesium'

Error:
/home/vagrant/dev/sandbox/node_modules/cesium/Source/Cesium.js:1
    Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/vagrant/dev/sandbox/node_modules/cesium/Source/Cesium.js
    require() of ES modules is not supported.
    require() of /home/vagrant/dev/sandbox/node_modules/cesium/Source/Cesium.js from /home/vagrant/dev/sandbox/node_modules/cesium/index.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
    Instead rename Cesium.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /home/vagrant/dev/sandbox/node_modules/cesium/package.json.

If I remove the "type" attribute from cesium's package.json, it works as desired (from what I can tell)

An example project can be created with tomo-cli:
tomo new app --use-react --with-cesium



3. Context. Why do you need to do this? We might know a better way to accomplish your goal.

Testing is good. Jest is good. Testing with Jest is good. That is to say, I believe that Jest has sufficient market share to be worthy of support.

4. The Cesium version you're using, your operating system and browser.

Cesium Version: 1.65.0
OS: Ubuntu (Xenial) Vagrant Box
Browser: N/A (Jest)


Mark Erikson

unread,
Jan 7, 2020, 11:27:02 AM1/7/20
to cesium-dev
Have you tried configuring Jest to compile those ES modules back to CommonJS instead?

jhwohl...@gmail.com

unread,
Jan 8, 2020, 10:17:42 PM1/8/20
to cesium-dev
I did try that. Generally, I used the process from the bottom of this article: https://medium.com/@craigmiller160/how-to-fully-optimize-webpack-4-tree-shaking-405e1c76038

Not saying it is not possible I made a mistake, but it did not seem like Jest was getting past the "module" nature of Cesium's package.json...

tiff...@gmail.com

unread,
Feb 13, 2020, 6:09:10 PM2/13/20
to cesium-dev
I am experiencing the same issue with node version 12.16 and cesium. Is there a fix for this?

jhwohl...@gmail.com

unread,
Feb 13, 2020, 6:38:31 PM2/13/20
to cesium-dev
I have not found one...did you try the steps from the link in my reply above? I am not entirely convinced I did not just screw up the setup...it would be nice to have a second person verify that the steps I tried do not work.

arieleb

unread,
Apr 6, 2020, 5:57:52 AM4/6/20
to cesium-dev
This is fixed in cesium 1.68.0
Reply all
Reply to author
Forward
0 new messages