
sinon-chai 是什么为 Chai 断言库注入 Sinon.JS 能力的完整指南【免费下载链接】sinon-chaiExtends Chai with assertions for the Sinon.JS mocking framework.项目地址: https://gitcode.com/gh_mirrors/si/sinon-chaisinon-chai 是什么它是 Chai 断言库最流行的插件之一专门为 Sinon.JS 模拟框架设计把 spy、stub、mock 的全部断言能力转化为原生的expect/should链式语法。本指南将带你快速掌握 sinon-chai 的安装配置、断言速查表、实战用例与进阶技巧帮助你写出更清晰、更优雅的单元测试代码。sinon-chai 是什么为什么单元测试需要它在 JavaScript 单元测试中Mocha Chai Sinon.JS 是经典三件套Mocha测试运行框架Chai断言库提供expect/should/assert三种风格Sinon.JSspy、stub、mock 模拟工具三者各自优秀但组合使用时有一个尴尬点Sinon.JS 自带的断言是函数式写法不够直观sinon.assert.calledWith(mySpy, foo);而 Chai 的链式语法虽然优美却不能直接作用于 spy 对象强行写会非常别扭mySpy.calledWith(foo).should.be.ok;sinon-chai 正是为此而生它把 Sinon.JS 的所有断言翻译成 Chai 风格的链式断言让测试代码读起来像一句完整的自然语言mySpy.should.have.been.calledWith(foo); expect(mySpy).to.have.been.calledWith(foo);它的核心实现非常轻量就是一个标准的 Chai 插件全部源码集中在 lib/sinon-chai.js 一个文件里通过chai.use()注册即可生效。sinon-chai 快速安装npm 三步配置法第一步安装依赖确保已安装 Node.js 环境然后执行npm install --save-dev sinon-chaisinon-chai 通过 peerDependencies 要求项目中已有 chai^5 或 ^6和 sinon4所以别忘了npm install --save-dev chai sinon第二步注册插件在测试入口文件或 Mocha 的 setup 文件中引入并注册import * as chai from chai; import sinonChai from sinon-chai; chai.use(sinonChai);第三步选择断言风格sinon-chai 同时支持expect与should两种风格任君挑选const expect chai.expect; expect(spy).to.have.been.called; // expect 风格 spy.should.have.been.called; // should 风格官方测试项目中的公共配置 test/common.js 就是标准的注册范例可以直接参考。sinon-chai 断言速查表从 called 到 threwsinon-chai 几乎覆盖了 Sinon.JS 的全部常用断言下面以should风格列出expect等价写法同样可用Sinon.JS 属性/方法sinon-chai 断言calledspy.should.have.been.calledcallCountspy.should.have.callCount(n)calledOncespy.should.have.been.calledOncecalledTwicespy.should.have.been.calledTwicecalledThricespy.should.have.been.calledThricecalledBeforespy1.should.have.been.calledBefore(spy2)calledAfterspy1.should.have.been.calledAfter(spy2)calledWithspy.should.have.been.calledWith(...args)calledWithExactlyspy.should.have.been.calledWithExactly(...args)calledWithMatchspy.should.have.been.calledWithMatch(...matchers)calledOnspy.should.have.been.calledOn(context)calledWithNewspy.should.have.been.calledWithNewreturnedspy.should.have.returned(value)threwspy.should.have.thrown(error)这些断言不仅适用于 spy 本身还可以直接作用于某一次具体调用spy.getCall(0)非常灵活。完整的断言行为定义与边界测试用例都可以在 test/callArguments.js、test/callCount.js、test/returning.js 等测试文件中找到。sinon-chai 实战编写第一个完整用例以回调函数的测试为例来自官方 README 的经典场景import * as chai from chai; import sinon from sinon; import sinonChai from sinon-chai; chai.should(); chai.use(sinonChai); function hello(name, cb) { cb(hello name); } describe(hello, function () { it(应该用正确的问候语调用回调, function () { const cb sinon.spy(); hello(foo, cb); cb.should.have.been.calledWith(hello foo); }); });整个断言读起来就是一句完整的英文句子cb 应当已经被调用过且参数是 hello foo。相比sinon.assert.calledWith(cb, hello foo)可读性提升了不止一个档次测试失败时的错误信息也来自 Sinon 的格式化输出定位问题一目了然。sinon-chai 进阶技巧.not 取反与 always 断言用 .not 轻松表达不应该发生任何断言都可以通过 Chai 的.not取反例如spy.should.not.have.been.called; // 从未被调用 spy.should.not.have.been.calledWith(foo); // 未被用 foo 调用过用 always 表达每一次调用都如此当 spy 被多次调用时可以用always前缀要求每一次调用都满足条件spy.should.always.have.been.calledWith(foo); // ✅ 正确写法⚠️ 注意always必须放在have之前写成should.have.been.alwaysCalledWith是无效的这是新手最容易踩的坑。更严格的组合断言calledOnceWith、calledOnceWithExactly这类组合断言可以同时校验调用次数 参数一个断言搞定两件事spy.should.always.have.been.calledOnceWithExactly(foo);sinon-chai 常见问题速查报错 is not a spy 怎么办sinon-chai 只能用于 Sinon.JS 创建的 spy、stub、mock 及其调用对象。对普通函数使用会抛出TypeError: xx is not a spy or a call to a spy!请检查断言对象是否来自sinon.spy()或sinon.stub()。calledWith 和 calledWithExactly 有何区别calledWith宽松匹配允许实际调用包含额外参数calledWithExactly严格匹配要求参数完全一致如何校验抛出的错误使用thrown断言可以传入错误对象、错误类型字符串或什么都不传spy.should.have.thrown(); spy.should.have.thrown(TypeError); spy.should.have.thrown(new Error(boom));总结sinon-chai 用极小的体积把 Chai 断言库和 Sinon.JS 模拟框架无缝打通让 spy、stub、mock 的验证从函数式调用进化为自然语言式断言同时保留了 Sinon 精确、信息丰富的错误消息。对于使用 Mocha Chai Sinon.JS 技术栈的团队来说它是一个即插即用、收益立竿见影的测试利器强烈推荐加入你的开发依赖。如果你想查看完整源码和全部测试用例可以克隆仓库https://gitcode.com/gh_mirrors/si/sinon-chai【免费下载链接】sinon-chaiExtends Chai with assertions for the Sinon.JS mocking framework.项目地址: https://gitcode.com/gh_mirrors/si/sinon-chai创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考