Spy on Cesium with Jasmine and Karma

1. A concise explanation of the problem you’re experiencing.

I am trying to spy on the Viewer constructor inside a Jasmine unit test but I do not know the proper way to do it, now that Cesium is written in ES6 modules.
Before version 1.63, Cesium was added to the window object and it was pretty easy to spy on it.

2. A minimal code example. If you’ve found a bug, this helps us reproduce and repair it.

I used to spy on Viewer with the following code in pre-ES6 version

const viewer = new Cesium.Viewer(…);

it(‘should create a Cesium viewer’, () => {
const spy = spyOn(window[‘Cesium’], ‘Viewer’);



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

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

Cesium 1.63

Windows 10 1903

Google Chrome 78.0.3904.70

How are you importing Cesium in your tests? You could just set up window.Cesium = Cesium yourself like we do here to keep this working:


Thanks very much for your answer. I tried your suggestion and it seems that Cesium is loaded correctly on the window but when I try to spy on it, I get the error below:

Error: : Viewer is not declared writable or has no setter

Please note that I am testing a piece code that loads Cesium itself using ES6 modules:

import { Viewer } from ‘cesium’;

const viewer = new Viewer(…);


Any ideas on this?