Skip to content

angular-mocks tricks Leaflet.GeometryUtil into thinking modules are available #78

Open
@ReeseJones

Description

@ReeseJones

image

I am setting up unit tests using angular 1, karma and jasmine. All our modules and plugins and scripts are statically included. When using jasmine and angular-mocks module is defined in the window scope. So when leaflet.GeometryUtil tries to add itself to leaflet it assumes module.export will exist (because the module object is defined), but it doesn't exist. In my case I expect it to install itself to a global L that is statically included, and not required.

we are using a few other leaflet plugins, but this seems to be the only one with this problem.

Perhaps you can look to see what these ones are doing when performing the // Packaging/modules magic dance.
Leaflet-semicircle
Leaflet-heatmap
Leaflet.markercluster

this is what the heatmap plugin is doing:

  // Supports UMD. AMD, CommonJS/Node.js and browser context
  if (typeof module !== "undefined" && module.exports) {
    module.exports = factory(
      require('heatmap.js'),
      require('leaflet')
    );
  } else if (typeof define === "function" && define.amd) {
    define(['heatmap.js', 'leaflet'], factory);
  } else {
    // browser globals
    if (typeof window.h337 === 'undefined') {
      throw new Error('heatmap.js must be loaded before the leaflet heatmap plugin');
    }
    if (typeof window.L === 'undefined') {
      throw new Error('Leaflet must be loaded before the leaflet heatmap plugin');
    }
    context[name] = factory(window.h337, window.L);
  }

I think an extra check for the fully qualified module.exports would solve my problem.
Why they put that in the global scope to begin with, I am not sure.

I cant upgrade frameworks for business reasons.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions