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 的規範。請確保在所有平台,或至少在 Widows/Mac/Linux 上正常運作。

建置 & 測試

在您的 git 結帳中安裝開發相依性

$ npm install

您需要全域的 grunt 命令

$ npm install grunt-cli -g

建置並執行測試

$ grunt

請參閱 Gruntfile 以取得其他命令。

:wrench: 測試產生器

此外掛程式使用「斷言外掛程式測試產生器」的原型,為斷言的所有方面產生測試,同時保持規格 DRY。

此模式將測試拆分為樣式宣告樹狀結構和 3 種測試情境的一組變體。然後,產生器將每個情境變體與樣式樹狀結構資料組合(「相乘」)以獲得所有案例的良好覆蓋率。

樣式樹狀結構定義了使用斷言的方式:第一層是樣式:expect/should 和 assert。然後它定義了正常使用和否定,然後將它們分為每個樣式的不同呼叫模式。因此,您可以測試有無訊息,或作為鏈式方法或屬性等。

測試是指定斷言和測試期望的方法。

  • valid - 預期測試通過 (但否定會失敗)
  • invalid - 預期測試失敗 (但否定會通過)。
  • error - 預期測試始終失敗(即使被否定),因為資料無效(例如:資料類型錯誤、缺少參數等)。

如果測試失敗,報告欄位會用於驗證錯誤訊息。它支援使用斷言資料物件的簡單範本格式。

為什麼?

這看起來有點複雜和麻煩,但它確實允許快速為所有斷言添加大量詳細測試。到目前為止,它似乎有效,所以我稍後可能會將其提取到單獨的 npm 模組中。

請注意,它會產生大量的案例變體,因此程式碼或測試設定中的小錯誤可能會導致套件出現許多失敗的斷言。仔細查看哪些測試失敗以了解原因。

授權條款

Copyright (c) 2013 Bart van der Schoor

在 MIT 授權條款下授權。