CesiumJS with Jest

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)

Have you tried configuring Jest to compile those ES modules back to CommonJS instead?

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…

I am experiencing the same issue with node version 12.16 and cesium. Is there a fix for this?

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.