Select to view content in your preferred language

Jest unit test with GraphicsLayer

269
1
02-27-2024 01:36 AM
AdamPigg
New Contributor II

I have a custom Experience Builder widget which itself works fine, but im having trouble writing a unit test for it.  The widget creates a GraphicsLayer in the constructor and the import for it looks like

import { GraphicsLayer } from "esri/layers/GraphicsLayer";
 
When it comes to the unit test, it fails with the error
TypeError: GraphicsLayer_1.GraphicsLaye is not a constructor
 
If i change the import to 
import GraphicsLayer from "esri/layers/GraphicsLayer";
the error changes to 
TypeError: GraphicsLayer_1.default is not a constructor
 
What am I missing, i tried adding a mock like
jest.mock("esri/layers/GraphicsLayer");
But that didnt help.
 
Thanks
Tags (1)
0 Kudos
1 Reply
AdamPigg
New Contributor II

So, i can get past that error with the following mock:

jest.mock("esri/layers/GraphicsLayer", () => {
    return {
        __esModule: true,
        default: jest.fn().mockImplementation(() => {
            return {
                init: mockedExp
            };
        })
    };
});
 
But then i get stuck on a similar error
TypeError: projection_1.default.load is not a function
 
Which so far ive been unable to mock...is there an easier trick im missing?
0 Kudos