Jest Difference between it and test with examples

Jest Framework provides it and test functions to define the test case functions., It represents a single test case. it is an alias for the test function, both do the same thing in terms of test case functionality.

Syntax:

test(name, fn, timeout);
it(name, fn, timeout);

name: name of a function fn: function to handle test case logic timeout: optional timeout

test and it are global functions in the Jest framework.

it is used for readability instead of test.

Here is an example of writing test cases using it function

describe("calculator", () => {
  it("add", () => {
    expect(2 + 3).toBe(5);
  });

  it("substract", () => {
    expect(20 - 6).toBe(14);
  });
});

Here is an example of writing test cases using the test function

describe("calculator", () => {
  test("add", () => {
    expect(2 + 3).toBe(5);
  });

  test("substract", () => {
    expect(20 - 6).toBe(14);
  });
});

From the above examples,

  • it and test are interchangeable used, It contains the test case name, function contains logic to write test cases
  • Declaration and structure are the same for both functions.
  • Some developers use test for readability, other use it for simplicity

To summarize, it and test do the same thing in terms of functionality and usage, So you can choose based on your coding styles and uniform across all developers in your team.