How to test a component containing OpenLayers map using createReactApp

…衆ロ難τιáo~ 提交于 2019-12-12 15:07:41

问题


I have a component like this one:

import React, { Component } from 'react';
import Map from 'ol/map';
import View from 'ol/view';
import Tile from 'ol/layer/tile';
import OSM from 'ol/source/osm';
import { mapInitialized } from '../../actions/map';

export default class MapWrapper extends Component<Props> {
  componentDidMount() {
    const map = new Map({
      layers: [
        new Tile({
          source: new OSM(),
        }),
      ],
      view: new View({
        center: [0, 0],
        zoom: 2,
      }),
      target: 'map',
    });
  }
  render() {
    return (
      <MapWrapper id="map" className="map" />
    );
  }
}

And a test like this:

import React from 'react';
import {Shallow} from 'enzyme';
import MapWrapper from '../mapWrapper/MapWrapper'

it ('renders without crashing', ()=>{
    Shallow(<MapWrapper/>);
})

After running "npm test" i gives me error message:

"node_modules\ol\map.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest)
{import _ol_ from './index.js';
^^^^^^
SyntaxError: Unexpected token import
at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/ScriptTransformer.js:289:17)

I figured out, its due to not transforming the ol/map.js file trough babel. But what can I do about it?


回答1:


I had to add the following to my jest.config.js file. Hope this helps.

"transformIgnorePatterns": [
  "node_modules/(?!(ol)/)"
]


来源:https://stackoverflow.com/questions/46971729/how-to-test-a-component-containing-openlayers-map-using-createreactapp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!