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

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


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.


As the other answers have clarified, they do the same thing.

I believe the two are offered to allow for either 1) "RSpec" style tests like:

const myBeverage = {  delicious: true,  sour: false,};describe('my beverage', () => {  it('is delicious', () => {    expect(myBeverage.delicious).toBeTruthy();  });  it('is not sour', () => {    expect(myBeverage.sour).toBeFalsy();  });});

or 2) "xUnit" style tests like:

function sum(a, b) {  return a + b;}test('sum adds 1 + 2 to equal 3', () => {  expect(sum(1, 2)).toBe(3);});

Documentation: