考虑我们有以下Map组件。这是在TypeScript中,但同样适用于普通的JavaScript。
import * as React from 'react';
import ReactMapboxGl from 'react-mapbox-gl';
const MapBox = ReactMapboxGl({
accessToken: 'pk.<redacted>'
});
export default class Map extends React.Component {
render() {
return (
<MapBox
style="mapbox://styles/mapbox/streets-v9"
zoom={[0]}
containerStyle={{
height: '500px',
width: 'inherit'
}}
/>);
}
}
然后它是一些反应应用程序的一部分,如下所示:
import * as React from 'react';
export default class App extends React.Component {
render() {
return (
<Map />
);
}
}
为了测试这个设置,我们使用Jest和JSDOM。
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import App from './App';
it('renders without crashing', () => {
const div = document.createElement('div');
ReactDOM.render(<App />, div);
});
此测试将无法运行并产生以下输出:
TypeError: window.URL.createObjectURL is not a function
at Object.254.../../source/worker (node_modules/mapbox-gl/dist/mapbox-gl.js:509:100)
at s (node_modules/mapbox-gl/dist/mapbox-gl.js:1:711)
at node_modules/mapbox-gl/dist/mapbox-gl.js:1:762
at Object.280.../ (node_modules/mapbox-gl/dist/mapbox-gl.js:561:28)
at s (node_modules/mapbox-gl/dist/mapbox-gl.js:1:711)
at node_modules/mapbox-gl/dist/mapbox-gl.js:1:762
at Object.263../worker_pool (node_modules/mapbox-gl/dist/mapbox-gl.js:527:29)
at s (node_modules/mapbox-gl/dist/mapbox-gl.js:1:711)
at node_modules/mapbox-gl/dist/mapbox-gl.js:1:762
at Object.191.../render/glyph_manager (node_modules/mapbox-gl/dist/mapbox-gl.js:383:809)
at s (node_modules/mapbox-gl/dist/mapbox-gl.js:1:711)
at node_modules/mapbox-gl/dist/mapbox-gl.js:1:762
at Object.248.../geo/lng_lat (node_modules/mapbox-gl/dist/mapbox-gl.js:497:338)
at s (node_modules/mapbox-gl/dist/mapbox-gl.js:1:711)
at node_modules/mapbox-gl/dist/mapbox-gl.js:1:762
at Object.72.../package.json (node_modules/mapbox-gl/dist/mapbox-gl.js:144:148)
at s (node_modules/mapbox-gl/dist/mapbox-gl.js:1:711)
at e (node_modules/mapbox-gl/dist/mapbox-gl.js:1:882)
at node_modules/mapbox-gl/dist/mapbox-gl.js:1:900
at Object.<anonymous>.i (node_modules/mapbox-gl/dist/mapbox-gl.js:1:177)
at Object.<anonymous> (node_modules/mapbox-gl/dist/mapbox-gl.js:1:413)
at Object.<anonymous> (node_modules/react-mapbox-gl/lib/map.js:21:16)
at Object.<anonymous> (node_modules/react-mapbox-gl/lib/index.js:3:13)
at Object.<anonymous> (src/Map.tsx:14:25)
at Object.<anonymous> (src/NewOrder.tsx:21:13)
at Object.<anonymous> (src/Routes.ts:17:18)
at Object.<anonymous> (src/App.tsx:16:16)
at Object.<anonymous> (src/App.test.tsx:6:169)
at <anonymous>
亲爱的读者,您的问题很简单:是否有可能解决这个问题?我们可以使用接缝来注入模拟的MapBoxGL库吗?
我在GitHub上发现了与此问题相关的多个问题,但没有一个问题提供解决方案:1,2。使用mapbox-gl-js-mock的一些要点,而其他人声称它没有用,因为它也需要真正的浏览器才能运行。
JSDOM项目中还有related issue关于添加URL.createObjectURL的问题,这可能会解决根本问题。
答案 0 :(得分:3)
您可以将其添加到setupTest.ts
的测试条目文件中
jest.mock('mapbox-gl/dist/mapbox-gl', () => ({
GeolocateControl: jest.fn(),
Map: jest.fn(() => ({
addControl: jest.fn(),
on: jest.fn(),
remove: jest.fn(),
})),
NavigationControl: jest.fn(),
}));
答案 1 :(得分:2)
我遇到了同样的问题,当我在测试块的顶部添加下面列出的here代码时,它起作用了。
jest.mock('mapbox-gl/dist/mapbox-gl', () => ({
Map: () => ({})
}));
答案 2 :(得分:0)
如果您使用导入和打字稿:
jest.mock('mapbox-gl/dist/mapbox-gl', () => {
return {
'default': {
accessToken: '',
GeolocateControl: jest.fn(),
Map: jest.fn(() => ({
addControl: jest.fn(),
on: jest.fn(),
remove: jest.fn(),
fitBounds: jest.fn(),
})),
NavigationControl: jest.fn(),
}
}
});