这是流行但已废弃的 npm 包 chai-param (https://github.com/theblacksmith/chai-param) 的一个分支。

导入路径已修复以支持现代 Node.js 版本。

chai-param-nn

使用 chai 验证方法参数

快速入门

在项目中安装它

npm install chai-param-nn

导入并加载它

var chai = require('chai'),
    chaiParam = require('chai-param-nn'),
    expect = chai.expect,
    should = chai.should(),
    param = chaiParam.param;

chai.use(chaiParam);

愉快地保护方法 :)

function register(user, pass) {
	// using should
	param(user, 'user', 'UsersCtl.register').should.not.be.empty;

	// or using expect
	expect(user).param('user', 'UsersCtl.register')
		.to.have.length.above(5)
		.and.below(16);
}

配置

chai-param-nn 可以通过设置 chaiParam.config 对象属性中的值来配置。例如

var chaiParam = require('chai-param-nn');

chaiParam.config.improveMessages = true;

improveMessages

类型: Boolean 默认值: true

控制 chai-param-nn 是否应该用更具描述性的消息替换 chai 的消息。所以,例如,您不会得到

UserCtl.register: expected 'asdf' to have a length above 5 but got 4

你会得到…

Improved error message

在错误消息中显示调用者

类型: Boolean 默认值: true

此选项仅适用于 node,并且 **需要 improveMessagestrue**。

chaiParam.config.showCaller = true;

disableColors

类型: Boolean 默认值: false

禁用消息着色。无聊,但对测试很有用。