node-fetch-response-matchers
Chai 外掛,提供 node-fetch 承諾回應的匹配器。它能讓測試更具聲明性。
簡而言之
- 此函式庫為您提供聲明式的方式來斷言 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() | (狀態碼) | 斷言狀態碼與提供的數字參數相同 |
主體匹配器
it('have body object', () => {
return expect(fetch('https://#/').to.haveBodyObject({foo: 'bar'});
});
完整主體匹配器列表
API 函式 | 參數 | 描述 |
---|---|---|
haveBodyObject() | (物件) | 斷言主體與提供的物件相等 |
haveBodyText() | (文字) | 斷言主體與提供的字串文字相等 |
haveBodyBuffer() | (Buffer) | 斷言主體與提供的 Node Buffer 相等 |
haveBodyRegexpMatch() | (正規表達式) | 斷言主體符合正規表達式 |
haveBodyThat() | (predicate(text)) | 斷言主體符合針對文字的提供的函式謂詞 |
標頭匹配器
it('have header', () => {
return expect(fetch('https://#/').to.haveHeader('connection', 'close');
});
標頭匹配器列表
API 函式 | 參數 | 描述 |
---|---|---|
haveHeader() | (名稱, 值) | 斷言回應包含具有提供的名稱和值的標頭 |
headerExists() | (名稱) | 斷言回應包含具有提供的名稱的標頭 |
haveHeaderThat() | (名稱, predicate(值)) | 斷言具有給定名稱的標頭,其值對給定的謂詞為真 |
haveHeaders() | (標頭映射) | 斷言給定的鍵值標頭存在於回應標頭中 |
Cookie 匹配器
it('have cookie', () => {
return expect(fetch('https://#/').to.haveCookie('foo', 'bar');
});
Cookie 匹配器列表
API 函式 | 參數 | 描述 |
---|---|---|
haveCookieByName() | (名稱) | 斷言 Cookie 由名稱寫入回應 |
haveCookie() | (名稱, 值) | 斷言 Cookie 由名稱和值寫入回應 |
haveCookieThat() | (名稱, predicate(cookie)) | 斷言 Cookie 由名稱,並符合給定 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() | (以秒為單位的時間) |