question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

window.crypto is undefined

See original GitHub issue

i’m trying to run tests using jsdom and mocha but I got this error:

ReferenceError: crypto is not defined

Is crypto really defined in jsdom?

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:11
  • Comments:19 (2 by maintainers)

github_iconTop GitHub Comments

20reactions
detjcommented, Jul 23, 2020

I used @peculiar/webcrypto in jest setupFiles

// jest.setup.js
import { Crypto } from "@peculiar/webcrypto"

global.crypto = new Crypto()

Seems to be working fine.

20reactions
nealeucommented, Jan 14, 2019

If you only require random numbers to be polyfilled, then you can also do

var nodeCrypto = require('crypto');
global.crypto = {
    getRandomValues: function(buffer: T) { return nodeCrypto.randomFillSync(buffer);}
};

Note: You’ll need Node v10 if you want buffer to be able to be Uint32Array as in v8 only Uint8Array and Buffer are supported

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use SubtleCrypto in chrome (window.crypto.subtle is ...
According to the spec (via Github issues) a la this Google page for WebCrypto: crypto.subtle is supposed to be undefined in insecure ...
Read more >
self.crypto - Web APIs - MDN Web Docs - Mozilla
The global read-only crypto property returns the Crypto object associated to the global object. This object allows web pages access to ...
Read more >
Web Crypto API | Node.js v19.3.0 Documentation
crypto or require('node:crypto').webcrypto to access this module. const { subtle } = globalThis.crypto; (async function() ...
Read more >
builtins.Window.crypto JavaScript and Node.js code examples
src/assets/js/helpers.js/undefined/generateRandomString. generateRandomString() { const crypto = window.crypto || window.msCrypto; let array = new ...
Read more >
How to Use window.crypto in Node.js - David Walsh Blog
I've been writing a bunch of jest tests recently for libraries that use the underlying window.crypto methods like getRandomValues() and.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found