Jest:如何在node_modules中模拟库?

问题描述 投票:2回答:2

我正在尝试为使用node-forge的代码编写测试。出于某种原因,当我打电话给forge.md.sha256.create();时,测试会挂起:

  import forge from "node-forge";

  const privateKey = "foo";
  const storagePin = "bar";

  const md = forge.md.sha256.create();
  md.update(privateKey + storagePin);

  const metadataKey = md.digest().toHex();

作为一种解决方法,我试图模拟该方法的实现,以便它只返回一个硬编码的字符串:

import forge from "node-forge";
jest.mock("node-forge");

forge.mockImplementation(() => {
  return {
    md: {
      sha256: {
        create: () => {
          return {
            update: () => {},
            digest: () => {
              toHex: () => "foobar";
            }
          };
        }
      }
    }
  };
});


// tests

但是,我的测试仍然失败:

TypeError: _nodeForge2.default.mockImplementation is not a function

  at Object.<anonymous> (src/redux/epics/authentication-epic.test.js:20:27)
      at new Promise (<anonymous>)
  at Promise.resolve.then.el (node_modules/p-map/index.js:46:16)
  at processTicksAndRejections (internal/process/next_tick.js:81:5)

奇怪的是,当我尝试模拟自己的文件时,这种策略完全正常。

嘲弄第三方图书馆的正确方法是什么?

javascript reactjs jestjs
2个回答
1
投票

你有这样的尝试吗?关于这个here的更多信息。

jest.mock('node-forge', () => ({
  md: {
    sha256: {
      create: () => ({
        update: () => {},
        digest: () => ({
          toHex: () => 'foobar'
        }),
      }),
    },
  },
}));

0
投票

default导出不是函数所以Jest自动模拟不会用模拟函数替换默认导出...

...但是default出口是一个对象。

来自Exploring ES6

...虽然您无法更改导入的值,但您可以更改它们所引用的对象。

所以你可以将对象的md属性设置为mock:

import forge from 'node-forge';
jest.mock('node-forge');

const toHex = jest.fn(() => 'foobar');
const digest = jest.fn(() => ({ toHex }));
const update = jest.fn();

forge.md = {  // <= set the md property to your mock
  sha256: {
    create: jest.fn(() => ({
      update,
      digest
    }))
  }
};

test('code uses the mock', () => {
  require('./path to your code');  // <= the mock will be used in the required code
  expect(forge.md.sha256.create).toHaveBeenCalled();  // Success!
  expect(update).toHaveBeenCalledWith('foobar');  // Success!
  expect(digest).toHaveBeenCalled();  // Success!
  expect(toHex).toHaveBeenCalled();  // Success
});
© www.soinside.com 2019 - 2024. All rights reserved.