我目前正在为一个React应用程序使用Mapbox,我试图执行单元测试,但似乎不起作用。我已经将Mapbox testMode设置为true,但它仍然给我一个错误消息:
Error: Failed to initialize WebGL.
at new Map (C:\Users\...\node_modules\mapbox-gl\dist\mapbox-gl.js:35:427889)
下面的代码片段是我们如何生成mapbox组件的,该组件还接收一个测试变量来设置testMode。
const MapBox = (props: MapBoxProps) => {
const { floorPlan, isTest } = props;
const { width } = useWindowDimensions();
const mapContainer = useRef(null);
const map = useRef(null);
const [lng, setLng] = useState(48);
const [lat, setLat] = useState(25);
const [zoom, setZoom] = useState(1.8);
const createMapbox = () => {
if (!isTest) {
mapboxgl.accessToken = appConfig.MAPBOX_TOKEN;
}
const mb = new mapboxgl.Map({
attributionControl: false,
container: mapContainer.current,
testMode: isTest,
style: {
version: 8,
sources: {},
layers: [
{
id: "background",
type: "background",
paint: {
"background-color": "white",
},
},
],
},
center: [lng, lat],
zoom: zoom,
maxZoom: 6,
dragRotate: false,
});
mb.addControl(new mapboxgl.NavigationControl({ showCompass: false }));
return mb;
};
return <div
data-cr="mapbox-container"
ref={mapContainer}
/>;
}
测试CustomMapBox组件的代码如下:
const renderMapBox = () => {
return render(<CustomMapBox floorPlan={mockedFloorPlan} isTest={true} />);
};
describe("MapBox", () => {
it("renders without error and two floors", () => {
renderMapBox();
});
}
我已经尽力了:
- 变更版本
- 更改Jest配置
- 将Map框testMode设置为true
而且似乎什么都不管用。
版本:
"mapbox-gl": "^2.8.2",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"ts-jest": "^27.0.7",
"jest": "^27.3.1",
如果有人能帮我的话,我会非常感激的。最好的问候,丹尼尔
1条答案
按热度按时间6pp0gazn1#
可以将其添加到设置Test.ts中