簡體   English   中英

使用Jest,Class構造函數進行單元測試

[英]Unit Test with Jest , Class constructor

如何使用set測試以下具有構造驗證的類。

const BaseParameter = class BaseParameter {
  constructor(addr, fullname, value) {
    this.addr = addr;
    this.fullname = fullname;
    this.value = value;
  }

  get value() {
    return this._value;
  }

  set value(value) {
    if (typeof value !== "number") {
      throw new TypeError(`Parameter ${this.fullname} should be a number`);
    }
    this._value = value;
  }
};

我嘗試過以下Jest方法。

test("BaseParameter with invalid constructor", () => {
  expect(new BaseParameter("test", "test fullname", "a")).toThrowError(
    TypeError
  );
});

但拋出錯誤,傳遞失敗。

文檔有明顯的例子,我只是卡住了

test("BaseParameter with invalid constructor", () => {
  expect(() => new BaseParameter("test", "test fullname", "a")).toThrowError(
    TypeError
  );
});

https://jestjs.io/docs/en/es6-class-mocks

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM