【问题标题】:jest mock: how to mock random output with react-native-randombytes笑话模拟:如何使用 react-native-randombytes 模拟随机输出
【发布时间】:2019-09-18 05:57:06
【问题描述】:

我正在尝试通过替换 react-native-randombytes 来编写一个有趣的单元测试,因为它使用了一些 NativeModules

我的错误信息:

Test suite failed to run

TypeError: Cannot read property 'seed' of undefined

> 1 | const RandomBytes = jest.genMockFromModule('react-native-randombytes');
    |                          ^
  2 | 
  3 | const randomBytes = (l) => {
  4 |   let uint8 = new Uint8Array(l);

  at seed (node_modules/react-native-randombytes/index.js:15:21)
  at Object.init (node_modules/react-native-randombytes/index.js:57:1)
  at Object.genMockFromModule (__mocks__/react-native-randombytes.js:1:26)

我将文件react-native-randombytes 放在__mocks__ 文件夹中node_modules 旁边

const RandomBytes = jest.genMockFromModule('react-native-randombytes');

const randomBytes = (l) => {
  let uint8 = new Uint8Array(l);
  uint8 = uint8.map(() => Math.floor(Math.random() * 90)+10);
  return uint8;
};

const seed = randomBytes(4096);

RandomBytes.randomBytes = randomBytes;
RandomBytes.seed = seed;

export default RandomBytes;

我打开了我想模拟的库,我发现它不是一个类,而是在它的 index.js 文件的末尾执行以下代码部分。 link

function init () {
  if (RNRandomBytes.seed) {
    let seedBuffer = toBuffer(RNRandomBytes.seed)
    addEntropy(seedBuffer)
  } else {
    seedSJCL()
  }
}

似乎使用 jest.genMockFromModule 会触发 init 函数,所以整个模拟失败了。在选择使用哪种模拟方法时我应该考虑哪些因素?在文档中,它列出了各种方法,但没有明确建议何时使用哪些方法。

我应该使用 jest.fn() 吗?

请指教。

更新 1:

我在测试文件中尝试了以下内容

jest.mock('react-native-randombytes');
const randomBytes = jest.fn().mockImplementation((l) => {
    let uint8 = new Uint8Array(l);
    uint8 = uint8.map(() => Math.floor(Math.random() * 90)+10);
    return uint8;
  });

结果:它不起作用。它有同样的错误。

更新 2:在 mock 中更改我的文件,如下所示

const R = jest.genMockFromModule('react-native-randombytes');

R.randomBytes = (l) => {
  let uint8 = new Uint8Array(l);
  uint8 = uint8.map(() => Math.floor(Math.random() * 90)+10);
  return uint8;
};

R.init = () => {};

export default R;

结果:同样的错误信息。它仍然是原始的 react-native-randombytes。

更新 3:就像更新 1,但有一些变化 灵感来自post

jest.genMockFromModule('react-native-randombytes');
// eslint-disable-next-line import/first
import randomBytes from 'react-native-randombytes';

randomBytes.mockImplementation((l) => {
    let uint8 = new Uint8Array(l);
    uint8 = uint8.map(() => Math.floor(Math.random() * 90)+10);
    return uint8;
  });

结果:同样的错误信息。

【问题讨论】:

    标签: javascript react-native jestjs


    【解决方案1】:

    如果您是开玩笑的新手,请先参考我的suggestions

    有许多依赖于 react-native-randombytes 的 node_modules 模块。你会在他们每个人之后疯狂奔跑。相反,您应该找到一个上层模块并对其进行模拟,如果它只有一个函数,则只需模拟函数即可。示例如下所示。

    我建议使用手动模拟,因为这些模块位于 node_modules

    示例 1:

    jest.mock('react-native-securerandom', (size) => {
      return {
        generateSecureRandom: jest.fn(() => {
          let uint8 = new Uint8Array(size);
          uint8 = uint8.map(() => Math.floor(Math.random() * 90)+10);
          return uint8;
        }),
      };
    });
    

    示例 2:

    'use strict';
    
    const bip39 = jest.mock('react-native-bip39'); // genMockFromModule causes problem
    
    bip39.generateMnemonic = jest.fn((l) => [
      'furth',
      'edessa',
      'injustices',
      'frankston',
      'serjeant',
      'khazar',
      'sihanouk',
      'longchamp',
      'stags',
      'pogroms',
      'coups',
      'upperparts',
      'endpoints',
      'infringed',
      'nuanced',
      'summing',
      'humorist',
      'pacification',
      'ciaran',
      'jamaat',
      'anteriorly',
      'roddick',
      'springboks',
      'faceted'
      ].slice(0, l));
    
    bip39.validateMnemonic = jest.fn((_) => true);
    
    bip39.mnemonicToSeed = jest.fn((_) => 'I am a mnemonic seed');
    
    export default bip39;
    

    【讨论】:

    • 请注意,Math.random() 不是 安全随机源(请参阅MDN
    猜你喜欢
    • 1970-01-01
    • 2019-07-23
    • 2018-07-15
    • 2021-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-26
    相关资源
    最近更新 更多