We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

TypeError: Cannot read property 'getRandomValues' of undefined for Unit test

i have created component for Syncfusion Column Chart, and tried to unit test my code using JEST (react-test-renderer), got error.
TypeError: Cannot read property 'getRandomValues' of undefined

    TypeError: Cannot read property 'getRandomValues' of undefined

      31 | describe('<Column />', () => {
      32 |     test('Column changes the class when hovered', () => {
    > 33 |         const component = renderer.create(
         |                                    ^
      34 |             <Column data={data1} chartProp={chartProp1} />,
      35 |             );
      36 |             const tree = component.toJSON();

      at m (node_modules/@syncfusion/ej2-base/dist/ej2-base.umd.min.js:10:2061)
      at e.r [as constructor] (node_modules/@syncfusion/ej2-base/dist/ej2-base.umd.min.js:10:72492)
      at e.o [as constructor] (node_modules/@syncfusion/ej2-charts/dist/ej2-charts.umd.min.js:10:137363)
      at new e (node_modules/@syncfusion/ej2-react-charts/dist/ej2-react-charts.umd.min.js:20:8204)
      at constructClassInstance (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:4833:18)
      at updateClassComponent (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:6685:5)
      at beginWork (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:7540:16)
      at performUnitOfWork (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:10358:12)
      at workLoop (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:10390:24)
      at renderRoot (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:10476:7)
      at performWorkOnRoot (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:11342:7)
      at performWork (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:11254:7)
      at performSyncWork (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:11228:3)
      at requestWork (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:11097:5)
      at scheduleWork (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:10907:5)
      at scheduleRootUpdate (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:11494:3)
      at updateContainerAtExpirationTime (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:11522:10)
      at updateContainer (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:11533:10)
      at Object.create (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:12079:5)
      at Object.create (src/lib/charts/column/Column.test.jsx:33:36)

9 Replies

JD Jayakumar Duraisamy Syncfusion Team January 17, 2019 12:21 PM UTC

Hi Mohammad, 
We need to mock all functionalities of window and others related to DOM in JEST for properly running the unit test cases in it. We will analyze this further and get back to you with more details within 2 working days on January 21, 2019. 
Regards, 
Jayakumar D 



JD Jayakumar Duraisamy Syncfusion Team January 17, 2019 02:52 PM UTC

Hi Mohammad, 
Please ignore the previous update. 
Since JEST does not have all window functions, we need to mock all functionalities of window and others related to DOM if that are not implemented in the JEST. Please refer the mocking options provided in JEST documentation. 
However, we will analyze the possibilities of using all the functionalities used in our components in testing environment and get back to you with more details within 2 working days on January 21, 2019. 
Regards, 
Jayakumar D 



MO Mohammad replied to Jayakumar Duraisamy January 22, 2019 08:37 AM UTC

Hi Mohammad, 
Please ignore the previous update. 
Since JEST does not have all window functions, we need to mock all functionalities of window and others related to DOM if that are not implemented in the JEST. Please refer the mocking options provided in JEST documentation. 
However, we will analyze the possibilities of using all the functionalities used in our components in testing environment and get back to you with more details within 2 working days on January 21, 2019. 
Regards, 
Jayakumar D 


Still waiting for update

Can you please provide update?


MO Mohammad replied to Mohammad January 22, 2019 09:02 AM UTC

Hi Mohammad, 
Please ignore the previous update. 
Since JEST does not have all window functions, we need to mock all functionalities of window and others related to DOM if that are not implemented in the JEST. Please refer the mocking options provided in JEST documentation. 
However, we will analyze the possibilities of using all the functionalities used in our components in testing environment and get back to you with more details within 2 working days on January 21, 2019. 
Regards, 
Jayakumar D 


Still waiting for update

Can you please provide update?

Found following solution, adding following lines in test file
Object.defineProperty(global.self, 'crypto', {
value: {
getRandomValues: (arr) => crypto.randomBytes(arr.length)
}
});

It works



JD Jayakumar Duraisamy Syncfusion Team January 23, 2019 05:44 AM UTC

Hi Mohammad, 
Thanks for your update. 
We are glad to hear that your error was resolved. Please let us know, if you need any further assistance. 
Regards, 
Jayakumar D 



SD Sabyasachi Dey replied to Mohammad December 7, 2021 02:32 PM UTC

Thanks a lot @Mohammad. We are also facing the same issue. Using SyncFusion React Studio.



SD Sabyasachi Dey December 7, 2021 03:25 PM UTC

Although this solved the previous error and my unit test cases are passing now. Thakns a lot for the help.

We are still observing the following error while runnign unit tests in jest and enzyme.

  console.error

    Error: Not implemented: window.computedStyle(elt, pseudoElt)



GV Gokulraj Varatharajan Syncfusion Team December 9, 2021 11:52 AM UTC

Hi Sabyasachi, 
 
Currently, we are validating your query and we will update the further details in two business days on December 13, 2021. 
 
We appreciate your patience until then. 
 
Regards, 
Gokul 



GV Gokulraj Varatharajan Syncfusion Team December 16, 2021 11:04 AM UTC

Hi Sabyasachi,  
 
We have validated your query and would like to let you know that we are unable to replicate the reported issue at our end. If you are still facing an issue, please share a testing sample which will be helpful for us to assist you better. 
 
Regards,  
Gokul


Loader.
Up arrow icon