@cloudcmd/stub

中文文档
5.1.0发布于 2026/07/13MITTypeScriptES Module近 30 天下载 3650

最简单的 sinon. stub 有 diff 支持的替代品

中文使用文档
文档目录18 节
  1. 安装
  2. API 密码
  3. stub( 缩写)
  4. stub (). 返回([值])
  5. stub (. srows ([ 错误])
  6. stub (). rejects ([error]) (中文(简体) )
  7. stub (. resolves ([值])
  8. stub (). called With ([args]) (德语)
  9. stub (). 称为 With New ()
  10. stub (). 之前称为 (fn)
  11. stub (). 称为 After( fn)
  12. stub( ). 所谓的
  13. stub (.). callCount (英语)
  14. stub (. args) (英语)
  15. stub (. callId) (英语)
  16. 是斯图( fn)
  17. 相关
  18. 许可证

稳住 NPM version Build Status Coverage Status

最简单 sinon.stub( ) (中文( ) ) 备选案文。 支持显示 diff 在 calleddWith。 。 。 .

安装

npm i @cloudcmd/stub

API 密码

stub( 缩写)

  • impl - 支架执行
import {stub} from '@cloudcmd/stub';

const fn = stub();

// fn contains stubbed function
const asyncFn = stub(async () => {
    throw Error('hi');
}); // asyncFn contains stub async function

stub (). 返回([值])

const fn = stub().returns('hello');
fn();
// returns
'hello';

stub (. srows ([ 错误])

const fn = stub().throws(Error('hello'));
fn();

// throws
Error('hello');

stub (). rejects ([error]) (中文(简体) )

const fn = stub().rejects(Error('hello'));
await fn();

// rejects
Error('hello');

stub (. resolves ([值])

const fn = stub().resolves('hello');
await fn();
// resolves
'hello';

stub (). called With ([args]) (德语)

const fn = stub();

fn('hello', 'world');

fn.calledWith('hello', 'world');
// returns true

stub (). 称为 With New ()

const fn = stub();

new fn();

fn.calledWithNew();
// returns
true;

stub (). 之前称为 (fn)

const fn1 = stub();
const fn2 = stub();

fn1();
fn2();

fn1.calledBefore(fn2);
// returns
true;

stub (). 称为 After( fn)

const fn1 = stub();
const fn2 = stub();

fn1();
fn2();

fn2.calledAfter(fn1);
// returns
true;

stub( ). 所谓的

const fn = stub();

fn.called;
// returns
false;

fn();

fn.called;
// returns
true;

stub (.). callCount (英语)

const fn = stub();

fn.callCount;

// returns
0;

fn();

fn.callCount;

// returns
1;

stub (. args) (英语)

const fn = stub();

fn.args;
// returns
[];

fn(1);

fn.args;
// returns
[
    [1],
];

stub (. callId) (英语)

每个 stub 拿着 callId,用于确定 stub 电话:

const fn1 = stub();
const fn2 = stub();

fn1();
fn2();

fn1.callId;

// returns
1;

fn2.callId;

// returns
2;

是斯图( fn)

检查是否提供了函数为 stub .

const {stub, isStub} = require('@cloudcmd/stub');
const fn = stub();

isStub(fn);
// returns
true;

isStub(() => {});
// returns
false;

许可证

麻省理工学院

版本与发布记录

@cloudcmd/stub 共发布 24 个版本,首次发布于 2018/11/23;过去 12 个月发布了 2 个版本。

版本发布日期距今
5.1.0latest2026/07/133 个月前
5.0.02026/01/129 个月前
4.0.12022/04/074 年前
4.0.02022/04/074 年前
3.7.12021/09/245 年前
3.7.02021/09/245 年前

关于 @cloudcmd/stub 的常见问题

@cloudcmd/stub 是什么?

最简单的 sinon. stub 有 diff 支持的替代品

如何安装 @cloudcmd/stub?

在项目目录运行 npm install @cloudcmd/stub,或使用 pnpm add / yarn add / bun add。

@cloudcmd/stub 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@cloudcmd/stub 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@cloudcmd/stub 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@cloudcmd/stub 可以商用吗?

@cloudcmd/stub 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@cloudcmd/stub 的下载量有多少?

根据 npm 官方下载统计,@cloudcmd/stub 在 2026/10/10 – 2026/11/08 这 30 天内被下载 3,650 次,日均约 122 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@cloudcmd/stub 还在维护吗?

最新版本 5.1.0 发布于 2026/07/13(3 个月前);至今共发布 24 个版本,其中过去 12 个月发布了 2 个;首次发布于 2018/11/23。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。