Orbity keystore depends on elliptic which depends on the brorand library. When running Jest tests with --env=jsdom, the brorand library throws an error.
jsdom lacks crypto: https://github.com/jsdom/jsdom/issues/1612. As a result, brorand/index (lines 30-50) looks for self.crypto and doesn't find it, concludes that we are on Safari, and throws an error.
This error can be avoided in one of two ways (so far): (1) Use node as the environment. (2) Install a monkey patch before the environment is set up (in a config file, not in the test file), like this:
import crypto from 'crypto';
self.crypto = crypto;
self.crypto.getRandomValues = (arr) => {
crypto.randomBytes(arr.length);
};
This is really a Jest/jsdom issue but thought I'd document here for posterity!
There is an open PR for brorand about this (https://github.com/indutny/brorand/pull/5) but it's been stuck there for years now
If anyone needs a quickfix for that:
const crypto = require('crypto');
Object.defineProperty(global.self, 'crypto', {
value: {
getRandomValues: arr => crypto.randomBytes(arr.length),
},
});
And then passing it to jest config should work:
"jest": {
"setupFiles": ["fixCrypto.js"]
}
Most helpful comment
If anyone needs a quickfix for that:
And then passing it to jest config should work: