gpt4 book ai didi

typescript - 使用 jsodom 和 jest 测试 react-mapbox-gl

转载 作者:搜寻专家 更新时间:2023-10-30 20:42:24 28 4
gpt4 key购买 nike

假设我们有以下 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'
}}
/>);
}
}

然后它是某些 react 应用程序的一部分,这样呈现:

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>

亲爱的读者,您的问题很简单:是否可以解决此问题?是否存在可用于注入(inject)模拟 MapBoxGL 库的接缝?

我在 GitHub 上发现了多个与此问题相关的问题,但没有一个提供解决方案:1 , 2 .使用 mapbox-gl-js-mock 的一些要点而其他人则声称它没有用,因为它也需要真正的浏览器才能运行。

还有 related issue关于添加 URL.createObjectURL 的 JSDOM 项目,这可能会解决潜在的问题。

最佳答案

我遇到了同样的问题,当我添加下面列出的代码时 here到我的测试 block 的顶部,它起作用了。

jest.mock('mapbox-gl/dist/mapbox-gl', () => ({
Map: () => ({})
}));

关于typescript - 使用 jsodom 和 jest 测试 react-mapbox-gl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48866088/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com