question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Running tests depending on esri-leaflet headlessly

See original GitHub issue

I’m writing a plugin that uses Leaflet and Esri-Leaflet. For my plugin, I have a reasonably large suite of unit tests powered by Mocha, Chai, Sinon, etc. My plugin is structured as a series of ES6 modules.

In my unit tests, I import { FeatureLayerService } from 'esri-leaflet', but this causes the tests to break because esri-leaflet depends on leaflet, which depends upon a global window resulting in a ReferenceError: window is not defined error in a headless environment like Node. I’ve been able to use pieces of Leaflet itself in my tests thanks to the leaflet-headless package, which sort of hacks over the global window dependency and allows me to import { Map } from 'leaflet-headless' and the like.

My question is: because esri-leaflet has depends on leaflet and not leaflet-headless (and it obviously should), is there any way to allow my unit tests to run, perhaps by:

  1. Forcing esri-leaflet to resolve the leaflet dependency to leaflet-headless (I’m transpiling with Babel and Webpack is my bundler)
  2. Polyfilling/stubbing the window global (I’ve explored this some using jsdom with no luck)
  3. Ejecting/removing the leaflet dependency from esri-leaflet (not likely since there are obvious dependencies)
  4. Another option I haven’t thought of
  • Version of Leaflet (L.version): 1.1.0, 1.0.3

  • Version of esri Leaflet (L.esri.VERSION): 2.0.8

Code I want to work:

// inside a unit spec
import { Map } from 'leaflet-headless'; // works fine
import { FeatureLayerService } from 'esri-leaflet'; // throws

Resulting errors:

ReferenceError: window is not defined
    at Object.<anonymous> (/mnt/c/dev/Leaflet.Control.NestedLayers/node_modules/leaflet/dist/leaflet-src
.js:13250:3)
    at Module._compile (module.js:570:32)
    at Module._extensions..js (module.js:579:10)
    at Object.require.extensions.(anonymous function) [as .js] (/mnt/c/dev/Leaflet.Control.NestedLayers/
node_modules/babel-register/lib/node.js:152:7)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at L$1__default (/mnt/c/dev/Leaflet.Control.NestedLayers/node_modules/esri-leaflet/dist/esri-leaflet
-debug.js:5:82)
    at Object.<anonymous> (/mnt/c/dev/Leaflet.Control.NestedLayers/node_modules/esri-leaflet/dist/esri-l
eaflet-debug.js:8:2)
    at Module._compile (module.js:570:32)
    at Module._extensions..js (module.js:579:10)
    at Object.require.extensions.(anonymous function) [as .js] (/mnt/c/dev/Leaflet.Control.NestedLayers/
node_modules/babel-register/lib/node.js:152:7)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/mnt/c/dev/Leaflet.Control.NestedLayers/src/Leaflet.LayerHierarchy.MapServerP
arser.js:10:1)
    at Module._compile (module.js:570:32)
    at loader (/mnt/c/dev/Leaflet.Control.NestedLayers/node_modules/babel-register/lib/node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (/mnt/c/dev/Leaflet.Control.NestedLayers/
node_modules/babel-register/lib/node.js:154:7)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/mnt/c/dev/Leaflet.Control.NestedLayers/test/Leaflet.LayerHierarchy.MapServer
Parser.spec.js:2:1)
    at Module._compile (module.js:570:32)
    at loader (/mnt/c/dev/Leaflet.Control.NestedLayers/node_modules/babel-register/lib/node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (/mnt/c/dev/Leaflet.Control.NestedLayers/
node_modules/babel-register/lib/node.js:154:7)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at /mnt/c/dev/Leaflet.Control.NestedLayers/node_modules/mocha/lib/mocha.js:230:27
    at Array.forEach (native)
    at Mocha.loadFiles (/mnt/c/dev/Leaflet.Control.NestedLayers/node_modules/mocha/lib/mocha.js:227:14)
    at Mocha.run (/mnt/c/dev/Leaflet.Control.NestedLayers/node_modules/mocha/lib/mocha.js:495:10)
    at loadAndRun (/mnt/c/dev/Leaflet.Control.NestedLayers/node_modules/mocha/bin/_mocha:426:22)
    at rerun (/mnt/c/dev/Leaflet.Control.NestedLayers/node_modules/mocha/bin/_mocha:454:5)
    at /mnt/c/dev/Leaflet.Control.NestedLayers/node_modules/mocha/bin/_mocha:462:7
    at StatWatcher.<anonymous> (/mnt/c/dev/Leaflet.Control.NestedLayers/node_modules/mocha/lib/utils.js:
196:9)
    at emitTwo (events.js:106:13)
    at StatWatcher.emit (events.js:191:7)
    at StatWatcher._handle.onchange (fs.js:1501:10)

npm ls | grep leaflet:

├── chai-leaflet@0.0.12
├─┬ esri-leaflet@2.0.8
│ ├── leaflet-virtual-grid@1.0.4
├── leaflet@1.0.3
├─┬ leaflet-headless@0.2.5
│ └─┬ leaflet-image@0.4.0

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
colbin8rcommented, Jul 19, 2017

@jgravois I also didn’t realize that Karma could run tests in PhantomJS… my mistake! PhantomJS is the closest thing to a headless browser I know of (the goal of the project IIRC). I’ll look into using Karma. Sorry for the misunderstanding, @jgravois!

0reactions
jgravoiscommented, Jul 28, 2017

i’m going to go ahead and close this issue as there’s nothing actionable for esri-leaflet at the moment, but i’d be more than happy to discuss the issue further and/or talk about changes in this repo down the line if anyone can recommend something.

Read more comments on GitHub >

github_iconTop Results From Across the Web

GIS unit testing frameworks - Esri Community
Hi guys I wanted to know what type of testing frameworks, do you use when developing application using arcgis javascript api.
Read more >
Esri Leaflet | ArcGIS Developers
Esri Leaflet is a light-weight, open source Leaflet plug-in for accessing ArcGIS location services and ArcGIS Enterprise services . ... Where to start....
Read more >
L.layerGroup equivalent in ESRI Leaflet for AGO feature ...
I'm creating four variables from the same feature service (AGO) because I want to separate them into four separate layers even though they...
Read more >
How to query points with a polygon layer using Bootleaf / esri ...
Run query to test points layer against active features of polygon layer. Within eachActiveFeature , we can run a query on the pointsFeatureLayer ......
Read more >
esri-leaflet | Yarn - Package Manager
This will compile minified source in a brand new dist directory, launch a tiny webserver and begin watching the raw source for changes....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found