Search code examples
javascriptreactjsunit-testingjestjscomponents

Why in the unit test components always use `should <action>` phrase form?


In a lot of javascript libraries (e.g react-dom test) I have used or in components libraries (e.g Vuetify component) For its unit test are using the should form: should not respond to click

  1. This form is a unit test standard?
  2. Where could I find more information about it?

In my personal opinion a prefer to use Given? - When - Then form from BDD testing, but if should form is very popular I want to know why and then adopt it!


Solution

  • This form is specific to the use of Jest it(...) function that represents a subject and was inherited from Jasmine, also applicable to test(...). It reads as "it should ..." or "test should ...".

    It doesn't make a good sentence with test() because it's usually a tested unit that should do something, not a test.

    In test reports, describe description becomes a subject:

    describe('Foo' => {
      it('should have a button', ...);
      it('should do a request', ...);
    });
    

    reads as

    Foo
      ✓ should have a button
      ✓ should do a request
    

    This makes less sense if there's no describe block, this wasn't allowed in Jasmine but is allowed in Jest.

    A downside of this convention is that test reports are polluted with "should" word. "should" can be omitted for this reason so it becomes "has a button" and "does a request" instead of "should have a button" and "should do a request".