首页 > 解决方案 > 与 Leaflet Markercluster 开玩笑

问题描述

我在测试与 markerCluster 相关的内容时遇到问题。我正在使用 Leaflet.markercluster npm 包,版本 1.5.1。我正在使用以下导入做类似于 git 页面上的示例的操作:

    import * as L from 'leaflet';
    import 'leaflet.markercluster';
    import "leaflet.markercluster/dist/leaflet.markercluster";
    import "leaflet.markercluster/dist/MarkerCluster.css";
    import "leaflet.markercluster/dist/MarkerCluster.Default.css";

我正在写的变量:

        const clusterItems = L.markerClusterGroup({
            chunkedLoading: true,
            showCoverageOnHover: false,
            iconCreateFunction: customIcon,
            removeOutsideVisibleBounds: true,
            disableClusteringAtZoom:18
        });

我也使用 react 作为我的前端库,如果你需要知道的话,可以使用 typescript。

问题是,在实际运行服务时工作正常。但是在使用 Jest 和 Enzyme 进行测试时,我总是会立即得到这个错误:

测试套件无法运行

    TypeError: L.markerClusterGroup is not a function

并且结果总是指向正确的线

    const clusterItems = L.markerClusterGroup

我知道其他人也有同样的错误,但通常在尝试运行他们的项目时似乎会发生这种情况。在这种情况下,我只在尝试运行测试时收到此错误。我尝试了一些想法,比如尝试使用 spyOn clusterItems 变量、模拟实现、模拟传单包,但无济于事。而且我环顾四周时也找不到任何这样的具体案例。

标签: javascriptunit-testingjestjsleafletleaflet.markercluster

解决方案


推荐阅读