代码之家  ›  专栏  ›  技术社区  ›  Remi

如何检查类定义中方法返回值的类型?

  •  0
  • Remi  · 技术社区  · 6 年前

    举下面的例子 按钮.js ,其中 javascript class

    import htmlToElement from './htmlToElement';
    
    class Button {
      constructor() {
        this._html = htmlToElement('<button>simple html</button>');
      }
    
      render() {
       return this._html;
      }
    }
    

    ./按钮.spec.js

    describe('Rectangle...', function() {
      it(`should be a class constructor..`, function() {
        // how to test against a specific type?
        expect(new Button().render()).to.be('HTMLButtonElement');
      });
    });
    // note: mocha and chai are included, jsdom is used also. So the button is rendering.
    

    0 回复  |  直到 6 年前
        1
  •  0
  •   Remi    6 年前

    expect(new Button().render()).to.be.a('HTMLButtonElement') 会有用的。