node-fetch-response-matchers
node-fetch プロミスの応答用のMatcherを含む Chai のプラグインです。テストをより宣言的に行えるようになります。
TL;DR
- このライブラリは、fetch の応答を宣言的にアサートする方法を提供します。また、プロミスとそのコールバックのノイズを隠します
it('some-test', function(){ return expect(fetch('https:///')).to.be.successful() .and.to.haveBodyText('foo'); }); - このライブラリを使用しない場合、非常に冗長になります
it('some-test', function(done){ fetch('https:///') .then(res => { expect(res.status).to.equal(200); return res.text(); }).then(text => { expect(text).to.equal('foo'); done(); }) });
インストール(開発用のみ - テストで使用)
$ npm install --save-dev node-fetch-response-matchers
使用方法の例
const nodeFetchMatchers = require('node-fetch-response-matchers');
const fetch = require('node-fetch');
const chai = require('chai');
chai.use(nodeFetchMatchers);
describe('test suite', function(){
it('http success test', function(){
return expect(fetch('https:///')).to.be.successful();
});
it('and', function(){
return expect(fetch('https:///')).to.be.successful()
.and.haveBodyText('foo');
});
});
Chai ネイティブプラグイン
Chai の「否定(not)」をすべて使用できます
it('not', function(){
return expect(fetch('https:///')).to.not.be.successful();
});
ステータスマッチャー
it('http success test', function(){
return expect(fetch('https:///')).to.be.successful();
});
it('http status assert', function(){
return expect(fetch('https:///')).to.haveStatus(500);
});
ステータスマッチャーの完全な一覧
| API 関数 | パラメータ | 説明 |
|---|---|---|
| successful() | () | ステータスが 200 OK であることをアサートします |
| created() | () | ステータスが 201 であることをアサートします |
| badRequest() | () | ステータスが 400 であることをアサートします |
| unauthorized() | () | ステータスが 401 であることをアサートします |
| rejected() | () | ステータスが 403 であることをアサートします |
| notFound() | () | ステータスが 404 であることをアサートします |
| serverError() | () | ステータスが 500 であることをアサートします |
| serviceUnAvailable() | () | ステータスが 503 であることをアサートします |
| haveStatus() | (status) | ステータスが指定された数値引数であることをアサートします |
本文マッチャー
it('have body object', () => {
return expect(fetch('https:///').to.haveBodyObject({foo: 'bar'});
});
本文マッチャーの完全な一覧
| API 関数 | パラメータ | 説明 |
|---|---|---|
| haveBodyObject() | (obj) | 提供されたオブジェクトと等しいことをアサートします |
| haveBodyText() | (text) | 提供された文字列テキストと等しいことをアサートします |
| haveBodyBuffer() | (Buffer) | 提供されたノードバッファーと等しいことをアサートします |
| haveBodyRegexpMatch() | (regexp) | 正規表現でボディが一致することをアサートします |
| haveBodyThat() | (predicate(text)) | テキストの指定された関数述語で、ボディが一致することをアサートします |
ヘッダーマッチャー
it('have header', () => {
return expect(fetch('https:///').to.haveHeader('connection', 'close');
});
ヘッダーマッチャーの一覧
| API 関数 | パラメータ | 説明 |
|---|---|---|
| haveHeader() | (name, value) | 応答に提供された名前と値でヘッダーが含まれていることをアサートします |
| headerExists() | (name) | 応答に提供された名前でヘッダーが含まれていることをアサートします |
| haveHeaderThat() | (name, predicate(value)) | 指定された名前のヘッダーが、指定された述語に対して値で真であることをアサートします |
| haveHeaders() | (headersMap) | 指定されたキーと値のヘッダーが、ヘッダーの応答に存在することをアサートします |
クッキーマッチャー
it('have cookie', () => {
return expect(fetch('https:///').to.haveCookie('foo', 'bar');
});
クッキーマッチャーリスト
| API 関数 | パラメータ | 説明 |
|---|---|---|
| haveCookieByName() | (name) | 名前でクッキーが応答に書き込まれていることをアサートします |
| haveCookie() | (name, value) | 名前と値でクッキーが応答に書き込まれていることをアサートします |
| haveCookieThat() | (name, predicate(cookie)) | 名前でクッキーがあり、クッキーのプロパティの指定された述語と一致することをアサートします |
キャッシュ制御応答マッチャー
it('must-revalidate', () => {
return expect(fetch('https:///').to.have.cacheControlMustRevalidate();
});
it('max-age', () => {
return expect(fetch('https:///').to.have.cacheControlmMaxAge(120);
});
キャッシュ制御の完全なマッチャー一覧
| API 関数 | パラメータ |
|---|---|
| cacheControlMustRevalidate() | () |
| cacheControlNoCache() | () |
| cacheControlNoStore() | () |
| cacheControlNoTransform() | () |
| cacheControlPublic() | () |
| cacheControlPrivate() | () |
| cacheControlProxyMaxRevalidate | () |
| cacheControlmMaxAge() | (秒単位の経過時間) |
| cacheControlSMaxAge() | (秒単位の経過時間) |