From 97687dd29f1297d9070c829090f74b4c98cb7ac3 Mon Sep 17 00:00:00 2001 From: Viktor Chernodub <37013688+chernodub@users.noreply.github.com> Date: Wed, 3 Jul 2024 01:29:29 +0300 Subject: [PATCH] fix: use error-prone reference to crypto module (#11882) --- modules/yandexIdSystem.js | 2 +- test/spec/modules/yandexIdSystem_spec.js | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/modules/yandexIdSystem.js b/modules/yandexIdSystem.js index f24e33a8c44..24b1f611ccd 100644 --- a/modules/yandexIdSystem.js +++ b/modules/yandexIdSystem.js @@ -128,7 +128,7 @@ class YandexUidGenerator { } _getRandomGenerator() { - if (crypto) { + if (window.crypto) { return () => { const buffer = new Uint32Array(1); crypto.getRandomValues(buffer); diff --git a/test/spec/modules/yandexIdSystem_spec.js b/test/spec/modules/yandexIdSystem_spec.js index d5f614dafb9..8ae7a374bfa 100644 --- a/test/spec/modules/yandexIdSystem_spec.js +++ b/test/spec/modules/yandexIdSystem_spec.js @@ -110,12 +110,17 @@ describe('YandexId module', () => { describe('crypto', () => { it('uses Math.random when crypto is not available', () => { - sandbox.stub(window, 'crypto').value(undefined); + const cryptoTmp = window.crypto; + + // @ts-expect-error -- Crypto is always defined in modern JS. TS yells when trying to delete non-nullable property. + delete window.crypto; yandexIdSubmodule.getId(CORRECT_SUBMODULE_CONFIG); expect(randomStub.calledOnce).to.be.true; expect(getCryptoRandomValuesStub.called).to.be.false; + + window.crypto = cryptoTmp; }); it('uses crypto when it is available', () => {