javascript - 使用 sinon 和 babel-plugin-rewire 时无法在模块内 stub 函数

标签 javascript unit-testing mocha.js sinon stub

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

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

// 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 的示例文件。同样的事情发生了:

// 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,问题就会消失。虽然很明显这意味着我不能使用 rewire,正如我之前提到的,我需要 rewire 才能在相同的依赖项中 stub 函数。这是模块的错误还是我在这里遗漏了什么?

最佳答案

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

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

不幸的是,我无法再进行监视/ stub 工作了。我不得不像这样重写我的测试:

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');
});

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

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

关于javascript - 使用 sinon 和 babel-plugin-rewire 时无法在模块内 stub 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50277905/

相关文章:

c# - AspNetCore 集成测试多个 WebApplicationFactory 实例?

node.js - 测试 REST API 并使用 EJS 渲染

javascript - azure ARM 消耗: get consumption of a resource group

javascript - 逻辑相似代码的圈复杂度

javascript - 使用CSS从顶部剪辑图像

jquery - 如何为 Chrome 编写 Greasemonkey 脚本以便对其进行单元测试?

javascript - 使用 Regex javascript/jquery 仅允许以下格式的数字和点

c++ - 对 3X3 或 4X4 行列式进行单元测试

javascript - Mongoose 上的这种保存方法如何在不说明哪个数据库的情况下工作?

Node.js mocha 进程 (_mocha) 不会在 CTRL-C 时退出