В настройке 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
});
});
И это вот babelrc, который используется для мокко
{
"presets": [
"@babel/preset-env"
],
"plugins": [
"rewire"
]
}
Если я удалю rewire из плагинов, проблема исчезнет. Хотя, очевидно, это означает, что я не могу использовать rewire, который, как я упоминал ранее, мне нужен для того, чтобы заглушить функции в пределах той же зависимости. Это ошибка модуля или я что-то здесь упускаю?