Search code examples
reactjsjestjsopenlayers

TypeError: Cannot read property 'setTransform' of null when using Openlayers and Jest + Enzyme


Currently using the OpenLayers ExtentInteraction and when I run my tests I manage to get the error as run test with jest:

TypeError: Cannot read property 'setTransform' of null

with the following code:

import ExtentInteraction from 'ol/interaction/Extent';

new ExtentInteraction({
  // option
});

Error Below

Any suggestions as it's getting late and I'm weary. Thanks in advance.


Solution

  • The issue here is that jsdom browser doesn't support canvas API so in order to fix this you can install this dev dependency jest-canvas-mock to add canvas api to jsdom window. Here is a few steps:

    Install:

    yarn add -D jest-canvas-mock
    
    // or npm
    
    npm i -D jest-canvas-mock
    
    

    Add this package as your setupFiles of jest:

    {
      setupFiles: ['jest-canvas-mock']
    }
    

    That's it!