What is the difference between 'it' and 'test' in Jest?

后端 未结 7 1982
滥情空心
滥情空心 2021-01-29 20:12

I have two tests in my test group. One of the tests use it and the other one uses test. Both of them seem to be working very similarly. What is the dif

7条回答
  •  滥情空心
    2021-01-29 21:11

    They do the same thing, but their names are different and with that their interaction with the name of the test.

    test

    What you write:

    describe('yourModule', () => {
      test('if it does this thing', () => {});
      test('if it does the other thing', () => {});
    });
    

    What you get if something fails:

    yourModule > if it does this thing
    

    it

    What you write:

    describe('yourModule', () => {
      it('should do this thing', () => {});
      it('should do the other thing', () => {});
    });
    

    What you get if something fails:

    yourModule > should do this thing
    

    So it's about readability not about functionality.

    In my opinion, it really has a point when it comes to read the result of a failing test that you haven't written yourself. It helps to faster understand what the test is about.

    Some developer also shorten the Should do this thing to Does this thing which is a bit shorter and also fits semantically to the it notation.

提交回复
热议问题