Skip to content

Commit

Permalink
Fix crypto.subtle setup for latest jsdom implementation
Browse files Browse the repository at this point in the history
A newer version of jsdom has a base setup for the crypto object, but it
doesn't include the crypto.subtle functions. We were replacing the top-
level crypto object, but the jsdom implementation behaves as the
browser, which is to say that if you try to replace the top-level crypto
object the attempt is silently ignored. As such, to provide access to
`crypto.subtle` in tests, we have to set the subproperty directly.
  • Loading branch information
Shadowfiend committed May 27, 2023
1 parent 8051c57 commit 50aee8d
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions background/tests/keyring-integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,23 @@ import { EIP1559TransactionRequest } from "../networks"
import { ETH, ETHEREUM } from "../constants"
import logger from "../lib/logger"

const originalCrypto = global.crypto
// Below, we replace any existing crypto.subtle implementation with the Node
// one. jsdom ships with an ad hoc, informally-specified, bug-ridden
// implementation of half of WebCrypto, but that includes securing the `crypto`
// variable to prevent it from being replaced, which manes we have to dig into
// the object instead of being able to replace the top-level variable.
const originalCryptoSubtle = globalThis.crypto?.subtle
beforeEach(() => {
// polyfill the WebCrypto API
global.crypto = webcrypto as unknown as Crypto
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
globalThis.crypto.subtle = (webcrypto as unknown as Crypto).subtle
})

afterEach(() => {
global.crypto = originalCrypto
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
globalThis.crypto.subtle = originalCryptoSubtle
})

const validMnemonics = {
Expand Down

0 comments on commit 50aee8d

Please sign in to comment.