Closed colbin8r closed 7 years ago
I've realized that it might be better to take this up directly with the Leaflet folks, although there seems to already have been some discussion about it.
in our own mocha/chai/sinon test suite we pre-load the compiled versions of both leaflet and esri-leaflet via the 'files' parameter in karma.conf.js
files: [
'node_modules/leaflet/dist/leaflet.css',
'node_modules/leaflet/dist/leaflet-src.js',
'dist/esri-leaflet-debug.js',
// then start loading our actual unit tests
],
this allows us to instantiate classes based on compiled UMD class dependencies instead of relying on actual ES6 imports.
// https://github.com/Esri/esri-leaflet/blob/0da3cf7e17bcd4ef1abd3970bac221b3881eb86a/spec/Services/FeatureLayerSpec.js#L1beforeEach(function () {
xhr = sinon.useFakeXMLHttpRequest();
// ...
service = L.esri.featureLayerService({url: featureServiceUrl});
});
@jgravois Please correct me if I'm wrong or misunderstand, but the only reason this works is that your tests are running in an actual browser environment where window
is available and is an actual browser window
. Karma is not a headless test runner. I'm running my tests with mocha which uses node, a headless environment where there is no actual browser causing there to be no window
object.
Thanks for the answer, but my issue is regarding loading Esri-Leaflet in a headless environment.
Edit: a word
my issue is regarding loading Esri-Leaflet in a headless environment.
i didn't realize that PhantomJS acts like a real browser. @patrickarlt do you have any ideas/suggestions for @colbin8r?
@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!
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.
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 becauseesri-leaflet
depends onleaflet
, which depends upon a globalwindow
resulting in aReferenceError: 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 globalwindow
dependency and allows me toimport { 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:
window
global (I've explored this some using jsdom with no luck)Version of Leaflet (
L.version
): 1.1.0, 1.0.3Version of esri Leaflet (
L.esri.VERSION
): 2.0.8Code I want to work:
Resulting errors:
npm ls | grep leaflet
: