•  
      request #26370 Use specialized expectations in Jest unit tests
    Infos
    #26370
    Joris MASSON (jmasson)
    2022-04-05 11:59
    2022-04-05 10:41
    27899
    Details
    Use specialized expectations in Jest unit tests

    eslint-plugin-jest offers a couple of rules to use specialized assertions instead of the generic toEqual().

    Following code review comments I made, I suggest to enable the following rules:

    • jest/prefer-comparison-matcher
      instead of expect(x > 5).toBe(true);, use expect(x).toBeGreaterThan(5);
    • jest/prefer-equality-matcher
      instead of expect(x === 5).toBe(true);, use expect(x).toBe(5);
    • jest/prefer-to-be
      All equality matchers work the same on primitives, but toBe is shorter and forms a grammatically natural sentence.
    • jest/prefer-to-contain
      instead of expect(a.includes(b)).toBe(true);, use expect(a).toContain(b);
    • jest/prefer-to-have-length
      instead of expect(files.length).toBe(1);, use expect(files).toHaveLength(1); as it gives a better failure message
    • jest/prefer-strict-equal
      toStrictEqual() is stricter with undefined and does not allow comparing class instance to object literal. This one would be enabled as a warning only, because there are >1000 usages that cannot be fixed automatically.
    Dev tools
    Empty
    Empty
    • [ ] enhancement
    • [ ] internal improvement
    Empty
    Stage
    Joris MASSON (jmasson)
    Closed
    2022-04-05
    Attachments
    Empty
    References
    Referencing request #26370
    Referenced by request #26370

    Artifact Tracker v5

    rel #25363 13.8

    Follow-ups

    User avatar
    Thomas Gerbet (tgerbet)2022-04-05 11:59

    Integrated into Tuleap 13.7.99.67.


    • Status changed from Under review to Closed
    • Connected artifacts
    • Close date set to 2022-04-05
    User avatar
    Joris MASSON (jmasson)2022-04-05 10:46
    • Summary
      -Have stricter expectations in Jest unit tests 
      +Use specialized expectations in Jest unit tests