007

中文文档
0.0.2发布于 2013/07/30MITCommonJS零依赖

Returns a deep copy of an object with all functions converted to spies

原始使用文档
文档目录6 节
  1. Install
  2. Example
  3. API
  4. doubleOhSeven()
  5. mockFn.implementation
  6. License

007

Returns a deep copy of an object with all functions converted to spies.

Install

Install with npm:

npm install 007

Example

// we want to test github
var github = require('github');
var doubleOhSeven = require('007');
var mockGithub = doubleOhSeven(github);

mockGithub.pullRequests.getCommits.returns = function () {
  return {
    foo: 'beep'
  };
};

assert(mockGithub.pullRequests.getCommits('lalala').foo === 'beep');
assert(mockGithub.pullRequests.getCommits.callCount === 1);

API

doubleOhSeven()

Takes an object or function as an argument, returns a function or object matching the original, but with mocked out functions.

mockFn.implementation

The implementation of the mock function. Set with =:

Ex:

mockFn.implementation = function (val, cb) {
  return cb(val + 1);
};

The following functions are helpers for implementation; using them will override mockFn.implementation.

mockFn.returns()

The return value of the mock.

Ex:

mockFn.returns('foo');
mockFn(function (arg) {
  console.log(arg); // outputs 'foo'
});

mockFn.callbackArgs

The callback args to be invoked by the mock.

Ex:

mockFn.callbackArgs(['foo']);
mockFn(function (arg) {
  console.log(arg); // outputs 'foo'
});

License

MIT

版本与发布记录

007 共发布 3 个版本,首次发布于 2013/07/27;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.0.2latest2013/07/3013 年前
0.0.12013/07/2713 年前
0.0.02013/07/2713 年前

关于 007 的常见问题

007 是什么?

返回对象的深层副本,其中所有函数都转换为间谍

如何安装 007?

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

007 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/007 是否存在。

007 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

007 可以商用吗?

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

007 还在维护吗?

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