node-fetch-response-matchers
Chai 插件,包含用于 node-fetch 承诺响应的匹配器。它有助于测试更加声明式。
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) | 断言等于提供的 Node 缓冲区 | 
| 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) | 断言给定的键值标头存在于标头响应中 | 
Cookie 匹配器
   it('have cookie', () => {
     return expect(fetch('https:///').to.haveCookie('foo', 'bar');
   });
Cookie 匹配器列表
| API 函数 | 参数 | 描述 | 
|---|---|---|
| haveCookieByName() | (name) | 断言按名称写入 Cookie 到响应 | 
| haveCookie() | (name, value) | 断言按名称和值写入 Cookie 到响应 | 
| haveCookieThat() | (name, 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() | (age-in-sec) | 
| cacheControlSMaxAge() | (age-in-sec) |