# @cloudcmd/stub 中文文档

规范地址：https://npmcn.com/package/%40cloudcmd/stub/

版本：5\.1\.0
npm 元数据更新时间：2026-07-13T08:51:19.879Z
中文译文更新时间：2026-11-10T02:47:34.808Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40cloudcmd/stub
原文对照：https://npmcn.com/package/%40cloudcmd/stub/?lang=original
项目主页：http://github.com/cloudcmd/stub
源码仓库：https://github.com/cloudcmd/stub

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：13 kB，7 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- Node.js 要求：\>=22
- 运行依赖：1 个
- 首次发布：2018-11-23T09:24:02.062Z
- 最新版本发布：2026-07-13T08:51:19.763Z
- 近 30 天下载量：3650（2026-10-10 至 2026-11-08，npm 官方下载统计）
- 已发布版本数：24（近 12 个月 2 个）

## 中文简介

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

## 安装

```sh
npm install @cloudcmd/stub
```

也可以使用：`pnpm add @cloudcmd/stub`、`yarn add @cloudcmd/stub`、`bun add @cloudcmd/stub`

## README

# 稳住 [![NPM version](https://img.shields.io/npm/v/@cloudcmd/stub.svg?style=flat&longCache=true)](https://npmjs.org/package/@cloudcmd/stub "npm") [![Build Status](https://github.com/coderaiser/putout/workflows/Node%20CI/badge.svg)](https://github.com/coderaiser/putout/actions?query=workflow%3A%22Node+CI%22 "Build Status") [![Coverage Status](https://coveralls.io/repos/cloudcmd/stub/badge.svg?branch=master&service=github)](https://coveralls.io/github/cloudcmd/stub?branch=master)

最简单 [sinon.stub( ) (中文( ) )](https://sinonjs.org/) 备选案文。 支持显示 diff 在 `calleddWith`。 。 。 .

## 安装

```
npm i @cloudcmd/stub
```

## API 密码

### stub( 缩写)

-   `impl` - 支架执行

```js
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 (). 返回(\[值\])

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

### stub (. srows (\[ 错误\])

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

// throws
Error('hello');
```

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

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

// rejects
Error('hello');
```

### stub (. resolves (\[值\])

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

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

```js
const fn = stub();

fn('hello', 'world');

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

### stub (). 称为 With New ()

```js
const fn = stub();

new fn();

fn.calledWithNew();
// returns
true;
```

### stub (). 之前称为 (fn)

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

fn1();
fn2();

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

### stub (). 称为 After( fn)

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

fn1();
fn2();

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

### stub( ). 所谓的

```js
const fn = stub();

fn.called;
// returns
false;

fn();

fn.called;
// returns
true;
```

### stub (.). callCount (英语)

```js
const fn = stub();

fn.callCount;

// returns
0;

fn();

fn.callCount;

// returns
1;
```

### stub (. args) (英语)

```js
const fn = stub();

fn.args;
// returns
[];

fn(1);

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

### stub (. callId) (英语)

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

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

fn1();
fn2();

fn1.callId;

// returns
1;

fn2.callId;

// returns
2;
```

### 是斯图( fn)

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

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

isStub(fn);
// returns
true;

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

## 相关

-   [{\\fn黑体\\fs22\\bord1\\shad0\\3ahbe\\4ah00\\fscx67\\fscy66\\2chffffff\\3ch808080}我被叫作"无所事事" {\\fn黑体\\fs22\\bord1\\shad0\\3ahbe\\4ah00\\fscx67\\fscy66\\2chffffff\\3ch808080}我..](https://github.com/coderaiser/sinon-called-with-diff) - 西农 `calledWith` 微调
-   [尝试到磁带](https://github.com/coderaiser/try-to-tape) - 说吧 `try catch` 用于合成磁带测试
-   [渔获量](https://github.com/coderaiser/try-catch "TryCatch") - 实用的试捕包装.
-   [试捕](https://github.com/coderaiser/try-to-catch "TryToCatch") - 实用的捕捉口袋以兑现承诺.

## 许可证

麻省理工学院

## 常见问题

### @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 和提交记录判断。

