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);