当前位置:网站首页>should.js Preliminary study

should.js Preliminary study

2020-12-08 10:52:34 Tao Baba

One 、 summary

background

In order to improve personal basic ability , Learn how to design and code good projects , So learn should.js Source code and Analysis

What is? should.js


should is an expressive, readable, framework-agnostic assertion library.

should.js It's an expressive one 、 High readability 、 Frame independent assertion Library .Node.js Built in assertion Library Assert,10 Multiple assertion test functions , But it has limited functionality , and should.js Can make the test more friendly , Therefore, the general front-end unit testing will introduce should.js.

Two 、 Usage method

Example

var should = require('should');

var user = {
    name: 'tj'
  , pets: ['tobi', 'loki', 'jane', 'bandit']
};

user.should.have.property('name', 'tj');
user.should.have.property('pets').with.lengthOf(4);

// if the object was created with Object.create(null)
// then it doesn't inherit `Object` and have the `should` getter
// so you can do:

should(user).have.property('name', 'tj');
should(true).ok;

someAsyncTask(foo, function(err, result){
  should.not.exist(err);
  should.exist(result);
  result.bar.should.equal(foo);
});

Usage method


1、 install

$ npm install should --save-dev

2、 Use

var should = require('should');

(5).should.be.exactly(5).and.be.a.Number;

3、 ... and 、 Introduction to project structure

Project directory


image.png

Four 、 Source code analysis

index.js


Project entry documents , export node.js

/lib/node.js


var should = require('./should');

should
  .use(require('./ext/assert'))
  .use(require('./ext/chain'))
  .use(require('./ext/bool'))
  .use(require('./ext/number'))
  .use(require('./ext/eql'))
  .use(require('./ext/type'))
  .use(require('./ext/string'))
  .use(require('./ext/property'))
  .use(require('./ext/http'))
  .use(require('./ext/error'))
  .use(require('./ext/match'))
  .use(require('./ext/deprecated'));

 module.exports = should;

introduce should.js, then use Method introduction ext Extension method under folder , It is convenient to use the extended method introduced in the test process . Finally, export should. Let's first introduce should.

/lib/should.js


/**
 * Our function should
 * @param obj
 * @returns {Assertion}
 */
var should = function(obj) {
  return new Assertion(util.isWrapperType(obj) ? obj.valueOf(): obj);
};

/**
 * Initialize a new `Assertion` with the given _obj_.
 *
 * @param {*} obj
 * @api private
 */

var Assertion = should.Assertion = function Assertion(obj) {
  this.obj = obj;
};

...
exports = module.exports = should;

should Method returns a new one Assertion example , and Assertion The assertion function is passed through obj Parameter initialization , And in Assertion Defined on the prototype of assert、getMessage、copy、copyIfMissing Method .

Next, define the method to extend the assertion function :add、alias, It uses some logic to define only positive assertions and negative assertions themselves . All the actions take place in the subtext , This method considers negation . We can add modifiers that don't depend on the state of the assertion .

adopt util by should Definition assertion error 、format and use Method , The final will be should export , It is convenient for the front end to pass the unit test require Introduce it . among uti Some functions used in the project are defined in the file , For example, judge whether the parameter is a string 、 take b Copy object properties to a Object and return a Object's merge Such method .

/lib/ext/assert.js


take assert Exposed to the should, adopt util In the document merge Methods will asset The method in is defined in should On , In this way, there is no need to use require() And then asset Module introduction .

by should Definition exist Method , Used to determine whether the first parameter passed in is null, At the same time, the first parameter is null An error is thrown to display the information given by the second parameter .should.not.exist Method defines when the first parameter passed in is not null Times wrong .

/lib/ext/chain.js


module.exports = function(should, Assertion) {

  function addLink(name) {
    Object.defineProperty(Assertion.prototype, name, {
      get: function() {
        return this;
      }
    });
  }

  ['an', 'of', 'a', 'and', 'be', 'have', 'with', 'is', 'which', 'the'].forEach(addLink);
};

Yes 'an', 'of', 'a', 'and', 'be', 'have', 'with', 'is', 'which', 'the' Word passing addLink Methods are defined to Assert On the prototype , bring Assertion It also includes these properties , And back to Assertion Object itself , Realization should Chain call of .

Source learning summary


  • should.js The design of file structure is reasonable , For example should The extension method of the unified put in ext Under the folder , And according to the function, it is stored in the file separately , This not only has a clear structure , And modular way to improve code reusability , It also enhances the scalability of the project
  • The project by chain.js The method in the file implements Assertion Chain call of

5、 ... and 、 summary

should.js The project structure is very clear , Although it's the same as what I learned before axios、promise And so on , A little bit more code , But according to the structure of the project, it will not be particularly difficult to read .should.js Very semantic , It's like writing a test in English as a natural language , Easy to use and easy to read , Recommended should.js Conduct front end unit test .

版权声明
本文为[Tao Baba]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/20201208105225707s.html