javascript - 诗乃类型错误 : Attempted to wrap <method> which is already wrapped when running multiple scripts

标签 javascript node.js mocha.js sinon

我在使用 sinon.js(和 mocha)进行测试时出错。当我通过 npm 运行所有测试脚本时会发生错误,但当我通过 IDE 运行单个脚本时不会发生错误。单独运行测试脚本没问题,测试通过。

即我有一个目录,里面有几个测试脚本。当我自己运行一个脚本时,测试通过了。当我运行目录中的所有脚本时,tess 失败并出现错误:

测试将失败

TypeError: Attempted to wrap getVariable which is already wrapped

当其他测试失败时:

TypeError: Cannot read property 'restore' of undefined

两个测试脚本都以相同的代码开头:

const
  assert = require('assert'),
  sinon = require('sinon');

global.context = {
  getVariable: function(s) {}
};

var contextGetVariableMethod;

beforeEach(function () {
  contextGetVariableMethod = sinon.stub(context, 'getVariable');
});

afterEach(function () {
  contextGetVariableMethod.restore();
});

我想 mocha 正在同时运行两个测试?而且这些测试相互干扰。我很困惑为什么测试的范围不是独立的......也许是 global 的使用?

谢谢

最佳答案

根据 Sinon JS 文档,如果你有 var stub = sinon.stub(object, "method");
您必须使用 object.method.restore();

进行恢复

因此,在您的情况下:

afterEach(function () {
    context.getVariable.restore()
});

关于javascript - 诗乃类型错误 : Attempted to wrap <method> which is already wrapped when running multiple scripts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44721299/

相关文章:

javascript - Angular google maps自定义图标仅在更新标记时有效

javascript - Node.js - 如何初始化/定义 req.body?

javascript - 使用不同的数据多次运行相同的 mocha 测试

javascript - 为什么我的快速测试没有通过?

javascript - 在没有 else 的情况下使用三元的替代方法

javascript - 在 mousemove 事件监听器函数主体中设置左侧位置不起作用

javascript - jQuery - div 上的 setTimeout 用于保存内容超过 x 秒

node.js - child_process.fork 和 cluster.fork 之间的有效区别是什么?

node.js - 如何在 JavaScript 中创建一个保存数组内容的 txt 文件?

javascript - tsc编译时不报错