Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

TypeError: cryptoObj.getRandomValues is not a function; unrelated error when using jest

I have a project where I've used powerbi-client in some part of my application. When I try to test some other components (which doesn't use powerbi-client) using testing-library/react and run npm test I get TypeError: cryptoObj.getRandomValues is not a function error. Before I was getting crypto_nonexistent error like mentioned in this issue and managed to fix this error by adding crypto to jest globals using craco.

 

Here's my jest config

126031307-cfe65639-2d21-4d17-a3a6-9165bf660165

Error stack trace:

TypeError: cryptoObj.getRandomValues is not a function

  12 |
  13 | import React, { useState, useEffect } from 'react';
> 14 | import { models, Embed, service } from 'powerbi-client';
     | ^
  15 | import { PowerBIEmbed } from 'powerbi-client-react';
  16 | import { appServices } from 'common/services';
  17 | import { useAccount, useMsal } from '@azure/msal-react';

  at Function.WindowPostMessageProxy.createRandomString (node_modules/powerbi-client/dist/webpack:/powerbi-client/node_modules/window-post-message-proxy/dist/windowPostMessageProxy.js:125:1)
  at new WindowPostMessageProxy (node_modules/powerbi-client/dist/webpack:/powerbi-client/node_modules/window-post-message-proxy/dist/windowPostMessageProxy.js:77:1)
  at wpmpFactory (node_modules/powerbi-client/dist/webpack:/powerbi-client/src/factories.ts:27:10)
  at new Service (node_modules/powerbi-client/dist/webpack:/powerbi-client/src/service.ts:160:17)
  at Object../src/powerbi-client.ts (node_modules/powerbi-client/dist/webpack:/powerbi-client/src/powerbi-client.ts:64:15)
  at __webpack_require__ (node_modules/powerbi-client/dist/webpack:/powerbi-client/webpack/bootstrap:19:1)
  at node_modules/powerbi-client/dist/webpack:/powerbi-client/webpack/bootstrap:83:1
  at node_modules/powerbi-client/dist/powerbi.js:100:10
  at webpackUniversalModuleDefinition (node_modules/powerbi-client/dist/webpack:/powerbi-client/webpack/universalModuleDefinition:3:1)
  at Object.<anonymous> (node_modules/powerbi-client/dist/webpack:/powerbi-client/webpack/universalModuleDefinition:10:2)
  at Object.<anonymous> (src/analytics/PowerBi/powerbi.tsx:14:1)
  at Object.<anonymous> (src/analytics/PowerBi/index.tsx:5:1)
  at Object.<anonymous> (src/main/App/App.tsx:18:1)
  at Object.<anonymous> (src/home/OfficesExplore/offices-explore.tsx:12:1)
  at Object.<anonymous> (src/home/OfficesExplore/index.tsx:1:1)
  at Object.<anonymous> (src/home/__tests__/offices-explore.test.tsx:3:1)

Expected Behavior

Npm test command should work without any problems because the component that I am testing doesn't rely on powerbi-client.

1 ACCEPTED SOLUTION
v-yiruan-msft
Community Support
Community Support

Hi @Anonymous ,

Please review the content in the following thread, hope they can help you resolve the problem.

When importing PowerBIEmbed in jest tests, fails right away with Cannot read property 'getRandomValues' of undefined

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

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

1 REPLY 1
v-yiruan-msft
Community Support
Community Support

Hi @Anonymous ,

Please review the content in the following thread, hope they can help you resolve the problem.

When importing PowerBIEmbed in jest tests, fails right away with Cannot read property 'getRandomValues' of undefined

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

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.