【问题标题】:Cannot stub functions inside a module when using sinon and babel-plugin-rewire使用 sinon 和 babel-plugin-rewire 时无法在模块内存根函数
【发布时间】:2018-05-10 16:56:12
【问题描述】:

在 mocha/chai 设置中,我尝试将 babel-plugin-rewire 与 sinon 结合使用,以在同一模块中测试和存根函数。这些是下面的示例文件:

首先,一个 index.js 和一个同时使用 sinon 和 babel-plugin-rewire 的测试文件。重新布线工作,但由于某种原因,我的存根不起作用。它所应用的函数永远不会被存根,只会返回原始值:

// index.js

function foo() {
  return "foo";
}

export function bar() {
  return foo();
}

export function jar() {
  return "jar";
}

//index.test.js

import chai from "chai";
import sinon from "sinon";
import * as index from "./index";
const expect = chai.expect;

const sandbox = sinon.sandbox.create();

describe("babel-plugin-rewire", () => {
  it("should be able to rewire", () => {
    index.default.__set__("foo", () => {
      return "rewired"; // successfullly rewires
    });
    expect(index.bar()).to.equal("rewired"); // works fine
    index.default.__ResetDependency__("foo");
    expect(index.bar()).to.equal("bar"); // works fine
  });
});

describe("sinon", () => {
  afterEach(() => {
    sandbox.restore();
  });

  it("should call the original jar", () => {
    expect(index.jar()).to.equal("jar"); // works fine
  });

  it("should call the stubbed jar", () => {
    sandbox.stub(index, "jar").returns("stub");
    expect(index.jar()).to.equal("stub"); // fails
  });
});

这里是两个单独使用 sinon 存根的示例文件。同样的事情也会发生:

// stub.js
export function stub() {
  return "stub me";
}

// stub.test.js
import * as stub from "./stub";
import sinon from "sinon";
import chai from "chai";

const expect = chai.expect;
const sandbox = sinon.createSandbox();

const text = "I have been stubbed";

describe("sinon stubs", () => {
  afterEach(() => {
    sandbox.restore();
  });
  it("should stub", () => {
    sandbox.stub(stub, "stub").returns(text);
    expect(stub.stub()).to.equal(text); // fails
  });
});

这是用于 mocha 的 babelrc

{
   "presets": [
     "@babel/preset-env"
   ],
   "plugins": [
     "rewire"
   ]
}

如果我从插件中删除重新布线,问题就会消失。虽然很明显这意味着我不能使用 rewire,正如我之前提到的,我需要它来在同一个依赖项中存根函数。这是模块的错误还是我在这里遗漏了什么?

【问题讨论】:

    标签: javascript unit-testing mocha.js sinon stub


    【解决方案1】:

    我的一项测试遇到了同样的问题。退出用户后,有一个操作触发了页面重定向。重定向本身是在单独的文件中实现的。

    import * as utils from '../utils.js';
    import sinon from 'sinon';
    
    it('will dispatch an action and redirect the user when singing out', () => {
        const redirectStub = sinon.stub(utils, 'redirect').returns(true);
        // dispatch and assertion of action omitted
        expect(redirectStub.calledOnce).toBeTruthy();
        redirectStub.restore();
    });
    

    然后,由于各种原因,我不得不将babel-rewire-plugin 添加到我的测试中。这打破了上面的特定测试。 calledOnce 或任何其他 sinon 方法总是返回 false

    不幸的是,我无法再进行间谍/存根工作了。我不得不像这样重写我的测试:

    import { __RewireAPI__ } from '../utils.js';
    import sinon from 'sinon';
    
    it('will dispatch an action and redirect the user when singing out', () => {
        __RewireAPI__.__Rewire__('redirect', () => true);
        // dispatch and assertion of action omitted
        __RewireAPI__.ResetDependencty('redirect');
    });
    

    如您所见,不再有间谍或存根断言。我重新连接了redirect 方法,然后将其重置。

    不过,有一个 babel-plugin-rewire-exports 库,它似乎允许您重新布线和 spy/stub。我自己没有尝试过,但如果你不想重写你的测试,它可以是一个选择。这是link

    【讨论】:

      猜你喜欢
      • 2021-01-24
      • 1970-01-01
      • 2018-08-27
      • 1970-01-01
      • 1970-01-01
      • 2016-05-11
      • 2021-01-21
      • 1970-01-01
      • 2021-04-19
      相关资源
      最近更新 更多