跳到內容

jest/prefer-to-have-length 樣式

🛠️ 此規則提供自動修正。

作用

為了獲得更好的失敗訊息,在斷言物件的長度屬性時,應使用 toHaveLength()

為何這不好?

如果使用 toBe()toEqual()toStrictEqual() 來斷言物件的長度屬性,此規則會觸發警告。

範例

javascript
// valid
expect.hasAssertions;
expect.hasAssertions();
expect(files).toHaveLength(1);
expect(files.name).toBe("file");

// invalid
expect(files["length"]).toBe(1);
expect(files["length"]).toBe(1);
expect(files["length"])["not"].toBe(1);

參考

以 MIT 授權發布。