chai-fs
Chai アサーション プラグイン は Node.js ファイルシステム API 用です。ファイルとディレクトリのアサートに path
と同期 fs
を使用します。
すべての assertion を expect
、should
、および assert
スタイルで使用いただけます。また、オプションの message パラメーターもサポートします。
使用方法
サーバー側
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
パスが存在し、ディレクトリであり、アイテムが 0 個含まれていることをアサートします。
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
否定をチェーンします。
directory().with.contents([…])
パスが存在し、ディレクトリで、特定のコンテンツ(ファイル、サブディレクトリ、シンボリックリンクなど)を含むことをアサートします。
expect(path).to.be.a.directory(?msg).with.contents(array, ?msg);
expect(path).to.be.a.directory(?msg).and.not.have.contents(array, ?msg);
expect(path).to.be.a.directory(?msg).with.deep.contents(array, ?msg);
expect(path).to.be.a.directory(?msg).and.not.have.deep.contents(array, ?msg);
expect(path).to.be.a.directory(?msg).and.include.contents(array, ?msg);
expect(path).to.be.a.directory(?msg).and.not.include.contents(array, ?msg);
path.should.be.a.directory(?msg).with.contents(array, ?msg);
path.should.be.a.directory(?msg).and.not.have.contents(array, ?msg);
path.should.be.a.directory(?msg).with.deep.contents(array, ?msg);
path.should.be.a.directory(?msg).and.not.have.deep.contents(array, ?msg);
path.should.be.a.directory(?msg).and.include.contents(array, ?msg);
path.should.be.a.directory(?msg).and.not.include.contents(array, ?msg);
assert.directoryContent(path, array, ?msg);
assert.notDirectoryContent(path, array, ?msg);
assert.directoryDeepContent(path, array, ?msg);
assert.notDirectoryDeepContent(path, array, ?msg);
assert.directoryInclude(path, array, ?msg);
assert.notDirectoryInclude(path, array, ?msg);
- コンテンツのパスはディレクトリに対する相対パスです
- チェーンに
.deep
が含まれていない限り、トップレベルのコンテンツのみが含まれます - チェーンに
.include
または.contain
が含まれている場合、ディレクトリは少なくとも指定されたコンテンツを含んでいる必要がありますが、それ以上のコンテンツを含んでもかまいません .content()
または.contents()
を使用できます。どちらも同じです。expect/should
を使用してこれを否定するには、通常のdirectory()
の後に.not
否定をチェーンします。
directory().with.files([…])
パスが存在し、ディレクトリであり、特定のファイルを格納していることを検証する。
expect(path).to.be.a.directory(?msg).with.files(array, ?msg);
expect(path).to.be.a.directory(?msg).and.not.have.files(array, ?msg);
expect(path).to.be.a.directory(?msg).with.deep.files(array, ?msg);
expect(path).to.be.a.directory(?msg).and.not.have.deep.files(array, ?msg);
expect(path).to.be.a.directory(?msg).and.include.files(array, ?msg);
expect(path).to.be.a.directory(?msg).and.not.include.files(array, ?msg);
path.should.be.a.directory(?msg).with.files(array, ?msg);
path.should.be.a.directory(?msg).and.not.have.files(array, ?msg);
path.should.be.a.directory(?msg).with.deep.files(array, ?msg);
path.should.be.a.directory(?msg).and.not.have.deep.files(array, ?msg);
path.should.be.a.directory(?msg).and.include.files(array, ?msg);
path.should.be.a.directory(?msg).and.not.include.files(array, ?msg);
assert.directoryFiles(path, array, ?msg);
assert.notDirectoryFiles(path, array, ?msg);
assert.directoryDeepFiles(path, array, ?msg);
assert.notDirectoryDeepFiles(path, array, ?msg);
assert.directoryIncludeFiles(path, array, ?msg);
assert.notDirectoryIncludeFiles(path, array, ?msg);
- ファイルパスはディレクトリに対して相対である
- チェーンに
.deep
が含まれない限り、最上位レベルのファイルのみが含まれる - チェーンに
.include
または.contain
が含まれる場合、ディレクトリには少なくとも指定されたファイルが含まれていなければならないが、それ以外にもファイルを含んでもよい expect/should
を使用してこれを否定するには、通常のdirectory()
の後に.not
否定をチェーンします。
directory().with.subDirs([…])
パスが存在し、ディレクトリであり、特定のサブディレクトリを含んでいることを検証する。
expect(path).to.be.a.directory(?msg).with.subDirs(array, ?msg);
expect(path).to.be.a.directory(?msg).and.not.have.subDirs(array, ?msg);
expect(path).to.be.a.directory(?msg).with.deep.subDirs(array, ?msg);
expect(path).to.be.a.directory(?msg).and.not.have.deep.subDirs(array, ?msg);
expect(path).to.be.a.directory(?msg).and.include.subDirs(array, ?msg);
expect(path).to.be.a.directory(?msg).and.not.include.subDirs(array, ?msg);
path.should.be.a.directory(?msg).with.subDirs(array, ?msg);
path.should.be.a.directory(?msg).and.not.have.subDirs(array, ?msg);
path.should.be.a.directory(?msg).with.deep.subDirs(array, ?msg);
path.should.be.a.directory(?msg).and.not.have.deep.subDirs(array, ?msg);
path.should.be.a.directory(?msg).and.include.subDirs(array, ?msg);
path.should.be.a.directory(?msg).and.not.include.subDirs(array, ?msg);
assert.directorySubDirs(path, array, ?msg);
assert.notDirectorySubDirs(path, array, ?msg);
assert.directoryDeepSubDirs(path, array, ?msg);
assert.notDirectoryDeepSubDirs(path, array, ?msg);
assert.directoryIncludeSubDirs(path, array, ?msg);
assert.notDirectoryIncludeSubDirs(path, array, ?msg);
- コンテンツのパスは開始ディレクトリに対して相対である
- チェーンに
.deep
が含まれない限り、最上位レベルのサブディレクトリのみが含まれる - チェーンに
.include
または.contain
が含まれる場合、ディレクトリには少なくとも指定されたサブディレクトリが含まれていなければならないが、それ以外にもディレクトリを含んでもよい expect/should
を使用してこれを否定するには、通常のdirectory()
の後に.not
否定をチェーンします。
directory().with.contents.that.satisfy(fn)
任意の配列に対する Chai.js アサーション(.lengthOf()
、.satisfy()
、.members()
など)を使用する .contents
、.files
、.subDirs
をチェーンできます
expect(path).to.be.a.directory().and.content.is.an('array');
expect(path).to.be.a.directory().and.files.have.lengthOf(5);
path.should.be.a.directory().with.subDirs.that.include.members(['subDir1', 'subDir2']);
path.should.be.a.directory().with.files.that.satisfy(function(files) {
return files.every(function(file) {
return file.substr(-4) === '.txt';
});
})
- コンテンツのパスはディレクトリに対する相対パスです
- チェーンに
.deep
が含まれていない限り、トップレベルのコンテンツのみが含まれます .content()
または.contents()
を使用できます。どちらも同じです。expect/should
を使用してこれを否定するには、通常のdirectory()
の後に.not
否定をチェーンします。
directory().and.equal(otherPath)
両方のパスが存在し、ディレクトリであり、同じコンテンツ(ファイル、サブディレクトリ、シンボリックリンクなど)を含んでいることを検証する。
expect(path).to.be.a.directory(?msg).and.equal(otherPath, ?msg);
expect(path).to.be.a.directory(?msg).and.not.equal(otherPath, ?msg);
expect(path).to.be.a.directory(?msg).and.deep.equal(otherPath, ?msg);
expect(path).to.be.a.directory(?msg).and.not.deep.equal(otherPath, ?msg);
path.should.be.a.directory(?msg).and.equal(otherPath, ?msg);
path.should.be.a.directory(?msg).and.not.equal(otherPath, ?msg);
path.should.be.a.directory(?msg).and.deep.equal(otherPath, ?msg);
path.should.be.a.directory(?msg).and.not.deep.equal(otherPath, ?msg);
assert.directoryEqual(path, otherPath, ?msg);
assert.notDirectoryEqual(path, otherPath, ?msg);
assert.directoryDeepEqual(path, otherPath, ?msg);
assert.notDirectoryDeepEqual(path, otherPath, ?msg);
- チェーンに
.deep
が含まれない限り、最上位レベルのコンテンツのみが比較される 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.content(str)
パスが存在し、ファイルであり、特定のコンテンツを含んでいることを検証する。
expect(path).to.be.a.file(?msg).with.content(data, ?msg);
expect(path).to.be.a.file(?msg).and.not.have.content(data, ?msg);
path.should.be.a.file(?msg).with.content(data, ?msg);
path.should.be.a.file(?msg).and.not.have.content(data, ?msg);
assert.fileContent(path, data, ?msg);
assert.notFileContent(path, data, ?msg);
- ファイルは UTF8 テキストとして読み取られます(base64、バイナリ Buffer などをサポートするように更新できます)。
.content()
または.contents()
を使用できます。どちらも同じです。- 否定するには
expect/should
を使用する。通常のfile()
の後に.not
-否定をチェーンします。
file().with.contents.that.match(/xyz/)
パスが存在し、ファイルであり、正規表現と一致するコンテンツを含んでいることを検証する。
expect(path).to.be.a.file(?msg).with.contents.that.match(/xyz/, ?msg);
expect(path).to.be.a.file(?msg).and.not.have.contents.that.match(/xyz/, ?msg);
path.should.be.a.file(?msg).with.contents.that.match(/xyz/, ?msg);
path.should.be.a.file(?msg).and.not.have.contents.that.match(/xyz/, ?msg);
assert.fileContentMatch(path, /xyz/, ?msg);
assert.notFileContentMatch(path, /xyz/, ?msg);
- ファイルは UTF8 テキストとして読み取られます(base64、バイナリ Buffer などをサポートするように更新できます)。
.content
または.contents
のどちらを使用できます。どちらも同じです。- 否定するには
expect/should
を使用する。通常のfile()
の後に.not
-否定をチェーンします。
file().and.equal(otherPath)
両方のパスが存在し、ファイルであり、同じコンテンツを含んでいることを検証する
expect(path).to.be.a.file(?msg).and.equal(otherPath, ?msg);
expect(path).to.be.a.file(?msg).and.not.equal(otherPath, ?msg);
path.should.be.a.file(?msg).and.equal(otherPath, ?msg);
path.should.be.a.file(?msg).and.not.equal(otherPath, ?msg);
assert.fileEqual(path, otherPath, ?msg);
assert.notFileEqual(path, otherPath, ?msg);
- 両方のファイルを UTF8 テキストとして読み取ります(base64、バイナリ Buffer などをサポートするように更新できます)。
- 否定するには
expect/should
を使用する。通常のfile()
の後に.not
-否定をチェーンします。
file().and.deep.equal(otherPath)
どちらのパスも存在し、ファイルであり、同じ内容を含み、以下を含む同じ属性を持つことをアサートします。
- 所有者 (
stats.uid
) - グループ (
stats.gid
) - 作成日時 (
stats.birthtime
) - 最終更新日時 (
stats.mtime
) -
最終変更時刻 (
stats.ctime
)expect(path).to.be.a.file(?msg).and.deep.equal(otherPath, ?msg); expect(path).to.be.a.file(?msg).and.not.deep.equal(otherPath, ?msg);
path.should.be.a.file(?msg).and.deep.equal(otherPath, ?msg); path.should.be.a.file(?msg).and.not.deep.equal(otherPath, ?msg);
assert.fileDeepEqual(path, otherPath, ?msg); assert.notFileDeepEqual(path, otherPath, ?msg);
- 両方のファイルを UTF8 テキストとして読み取ります(base64、バイナリ Buffer などをサポートするように更新できます)。
- 否定するには
expect/should
を使用する。通常のfile()
の後に.not
-否定をチェーンします。 - 最終アクセス日時(
stats.atime
)は比較に含まれません。なぜなら、この値を読み取るだけでは(fs.stat()
) 一部のオペレーティングシステムでは変更され、テストが不安定になる可能性があるからです。
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スキーマに準拠する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
之后的チェーン- スキーマパラメータは有効なJSONスキーマv4である必要があります。
- chai-json-schemaプラグインに依存しており、
chai.use()
を使用して別途アクティベートする必要があります。 expect/should
を使用してこれを否定するには、通常のjson
の後に.not
否定をチェーンします。with
およびusing
チェーンは単なる構文糖です。
計画中のアサーション
今後のアサーションのアイデアがいくつかあり、こちらのドキュメントに保存されています。。
履歴
- 0.1.0 - content.matchフィーチャーを追加しました(感謝:@legendary-mich)
- 0.0.2 - プラグインのリリース
- 0.0.1 - アルファリリース
コントリビューション
コントリビューションは大歓迎です。コード、テスト、スタイルパターンに従って、JSHintを満足させてください。少なくともWidows/Mac/Linuxのすべてプラットフォームで動作することを確認してください。
ビルドとテスト
git checkoutに開発用の依存関係をインストールします
$ npm install
グローバルなgruntコマンドが必要です
$ npm install grunt-cli -g
ビルドしてテストを実行します
$ grunt
他のコマンドについてはGruntfile
を参照してください。
:wrench: テストジェネレーター
このプラグインでは、「アサーションプラグインテストジェネレーター」のプロトタイプを使用して、アサーションのすべての側面のテストを生成しますが、仕様はDRYに保ちます。
パターンは、テストをスタイル宣言ツリーと、3種類のテストシナリオの派生に分割します。次に、ジェネレーターは、すべてのケースの良好なカバー率を得るために、あらゆるシナリオの派生をスタイルツリーデータと組み合わせて「掛け算」します。
スタイルツリーは、アサーションを使用する方法を定義します。最初のレベルはスタイル(expect/shouldとアサート)です。次に、通常の使用方法と否定を定義し、それらをスタイルごとのさまざまな呼び出しパターンに分割します。メッセージの有無や、チェーンメソッドまたはプロパティとしてなど、テストできます。
テストは、アサーションとテストの期待値を指定する方法です。
valid
- テストは通過する(ネゲートは失敗する)ことが期待されていますinvalid
- テストで失敗されることを期待しています(ただしその否定にパスします)。error
- データが無効のため、テストでは常に失敗すると予想されています(否定されても)。(例:データ型の不備、パラメーターの欠落など).
レポート フィールドは、テストが失敗した場合にエラーメッセージを確認するために使用されます。それはアサーションのデータオブジェクトを使用して簡単なテンプレートフォーマットをサポートしています。
理由:
これは少し複雑で面倒に思えますが、すべてのアサーションに対して、非常にたくさんの詳細なテストをすぐに追加することができます。今まで、力を与えるために機能しているように見えるので、後でこれら別の npm モジュールに抽出するかもしれません。
たくさんのケースのバリエーションを生成するので、コードまたはテストのセットアップでわずかなエラーがあると、数多くのアサーションが失敗してスイートが破棄されることに注意してください。どのテストが失敗し、何が原因であるかを見極めるために、失敗するテストを注意深く見てください。
ライセンス
著作権 (c) 2013 Bart van der Schoor
MIT ライセンスに基づきライセンスされています。