chai-fs

Build Status Dependency Status devDependency Status NPM version

Chai 断言 插件 用于 Node.js 文件系统 API。使用 path 和同步 fs 来断言文件和目录。

所有断言都可以在 expectshouldassert 样式中使用,并支持可选的消息参数。

分叉

由于 peerDependencies 问题而分叉。

用法

服务器端

从 npm 安装

$ npm install chai-fs

让 chai 使用 chai-fs 模块

var chai = require('chai');
chai.use(require('chai-fs'));

浏览器端

没有文件系统。

断言

basename()

断言 path.basename(path) 的返回值

expect(path).to.have.basename(name, ?msg);
expect(path).to.not.have.basename(name, ?msg);

path.should.have.basename(name, ?msg);
path.should.not.have.basename(name, ?msg);

assert.basename(path, name, ?msg);
assert.notBasename(path, name, ?msg);

dirname()

断言 path.dirname(path) 的返回值

expect(path).to.have.dirname(name, ?msg);
expect(path).to.not.have.dirname(name, ?msg);

path.should.have.dirname(name, ?msg);
path.should.not.have.dirname(name, ?msg);

assert.dirname(path, name, ?msg);
assert.notDirname(path, name, ?msg);

extname()

断言 path.extname(path) 的返回值

expect(path).to.have.extname(name, ?msg);
expect(path).to.not.have.extname(name, ?msg);

path.should.have.extname(name, ?msg);
path.should.not.have.extname(name, ?msg);

assert.extname(path, name, ?msg);
assert.notExtname(path, name, ?msg);

path()

断言路径存在。

使用 fs.existsSync()

expect(path).to.be.a.path(?msg);
expect(path).to.not.be.a.path(?msg);

path.should.be.a.path(?msg);
path.should.not.be.a.path(?msg);

assert.pathExists(path, ?msg);
assert.notPathExists(path, ?msg);

使用 Chai 的 exist 链会很不错,它在否定和消息参数方面存在问题。所以不要那样做。

directory()

断言路径存在并且是一个目录。

使用 fs.statSync().isDirectory()

expect(path).to.be.a.directory(?msg);
expect(path).to.not.be.a.directory(?msg);

path.should.be.a.directory(?msg);
path.should.not.be.a.directory(?msg);

assert.isDirectory(path,  ?msg);
assert.notIsDirectory(path, ?msg);

directory().and.empty

断言路径存在,是一个目录并且包含零个项目。

expect(path).to.be.a.directory(?msg).and.empty;
expect(path).to.be.a.directory(?msg).and.not.empty;

path.should.be.a.directory(?msg).and.empty;
path.should.be.a.directory(?msg).and.not.empty;

assert.isEmptyDirectory(path, ?msg);
assert.notIsEmptyDirectory(path, ?msg);
  • directory() 之后链接
  • 使用 fs.readdirSync().length === 0
  • 要使用 expect/should 否定这一点,您需要在常规的 directory() 之后 链接 .not 否定。

file()

断言路径存在并且是一个文件。

使用 fs.statSync().isFile()

expect(path).to.be.a.file(?msg);
expect(path).to.not.be.a.file(?msg);

path.should.be.a.file(?msg);
path.should.not.be.a.file(?msg);

assert.isFile(path, ?msg);
assert.notIsFile(path, ?msg);

file().and.empty

断言路径存在,是一个文件并且大小为零。

expect(path).to.be.a.file(?msg).and.empty;
expect(path).to.be.a.file(?msg).and.not.empty;

path.should.be.a.file(?msg).and.empty;
path.should.be.a.file(?msg).and.not.empty;

assert.isEmptyFile(path, ?msg);
assert.notIsEmptyFile(path, ?msg); 
  • file() 之后链接
  • 使用 fs.statSync().size === 0
  • 要使用 expect/should 否定这一点,您需要在常规的 file() 之后 链接 .not 否定。

file().with.json

断言路径存在,是一个文件并且包含可解析的 json 文本。

expect(path).to.be.a.file(?msg).with.json;
expect(path).to.be.a.file(?msg).with.not.json;

path.should.be.a.file(?msg).with.json;
path.should.be.a.file(?msg).with.not.json;

assert.jsonFile(path, ?msg);
assert.notJsonFile(path, ?msg); 
  • file() 之后链接
  • 要使用 expect/should 否定这一点,您需要在常规的 file() 之后 链接 .not 否定。
  • with 链只是语法糖。

file().using.json.schema(obj)

断言路径存在,是一个文件,包含符合给定 JSON-Schema 的可解析的 json 文本。

expect(path).to.be.a.file(?msg).with.json.using.schema(obj);
expect(path).to.be.a.file(?msg).with.json.not.using.schema(obj);

path.should.be.a.file(?msg).with.json.using.schema(obj);
path.should.be.a.file(?msg).with.json.not.using.schema(obj);

assert.jsonSchemaFile(path, schema,?msg);
assert.notJsonSchemaFile(path, schema, ?msg); 
  • file().with.json 之后链接
  • schema 参数必须是有效的 JSON-Schema v4。
  • 依赖于 chai-json-schema 插件,需要使用 chai.use() 单独激活。
  • 要使用 expect/should 否定这一点,您需要在常规的 json 之后 链接 .not 否定。
  • withusing 链只是语法糖。

content()

断言路径存在,是一个文件并且具有特定的内容。

expect(path).to.have.content(data, ?msg);
expect(path).to.not.have.content(data, ?msg);

path.should.have.content(data, ?msg);
path.should.not.have.content(data, ?msg);

assert.fileContent(path, data, ?msg);
assert.notFileContent(path, data, ?msg);
  • 以 utf8 文本读取文件(可以更新以支持 base64、二进制 Buffer 等)。

注意:在将来的版本中,这可能作为 file() 和 directory() 背后的链来支持

content.that.match(/xyz/)

断言路径存在,是一个文件并且具有匹配正则表达式的内容。

expect(path).to.have.content.that.match(/xyz/, ?msg);
expect(path).to.not.have.content.that.match(/xyz/, ?msg);

path.should.have.content.that.match(/xyz/, ?msg);
path.should.not.have.content.that.match(/xyz/, ?msg);

assert.fileContentMatch(path, /xyz/, ?msg);
assert.notFileContentMatch(path, /xyz/, ?msg);
  • 以 utf8 文本读取文件。

计划的断言

有一些关于未来断言的想法保存在 此文档中

历史

  • 0.1.0 - 添加了 content.match 功能(感谢 @legendary-mich)
  • 0.0.2 - 插件发布
  • 0.0.1 - Alpha 发布

贡献

欢迎贡献。请遵循代码、测试和样式模式,并保持 JSHint 的快乐。请确保一切在所有平台上都能正常工作,或者至少在 Windows/Mac/Linux 上能正常工作。

构建和测试

在您的 git 检出中安装开发依赖项

$ npm install

您需要全局的 grunt 命令

$ npm install grunt-cli -g

构建并运行测试

$ grunt

请参见 Gruntfile 以了解其他命令。

:wrench: 测试生成器

此插件使用 “断言插件测试生成器” 的原型来生成断言所有方面的测试,同时保持规范的 DRY。

该模式将测试拆分为样式声明树和 3 种测试场景变化的集合。然后,生成器将每个场景变化与样式树数据组合(“相乘”)以获得所有情况的良好覆盖率。

样式树定义了使用断言的方式:第一级是样式:expect/should 和 assert。然后它定义了正常使用和否定,然后将它们划分为每种样式的不同调用模式。因此,您可以使用/不使用消息进行测试,或者作为链式方法或属性等进行测试。

测试是指定断言和测试期望的方式。

  • valid - 预计通过的测试(但否定失败)。
  • invalid - 预计失败的测试(但否定通过)。
  • error - 预计始终失败的测试(即使在否定时也是如此),因为数据无效(例如:数据类型错误、缺少参数等)。

report 字段用于在测试失败时验证错误消息。它支持使用断言数据对象的简单模板格式。

为什么?

这看起来有点复杂和繁琐,但它确实允许快速为所有断言添加大量的详细测试。到目前为止,它似乎在增强功能方面很有效,因此我以后可能会将其提取到一个单独的 npm 模块中。

请注意,它将生成大量的案例变化,因此代码或测试设置中的一个小错误会导致套件爆炸,出现许多断言失败。仔细查看哪些测试失败,以了解是什么导致了什么。

许可证

版权所有 (c) 2013 Bart van der Schoor

根据 MIT 许可证授权。