727

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 difference between them?

describe('updateAll', () => {
  it('no force', () => {
    return updateAll(TableName, ["fileName"], {compandId: "test"})
        .then(updatedItems => {
          let undefinedCount = 0;
          for (let item of updatedItems) {
            undefinedCount += item === undefined ? 1 : 0;
          }
          // console.log("result", result);
          expect(undefinedCount).toBe(updatedItems.length);
        })
  });

  test('force update', () => {
    return updateAll(TableName, ["fileName"], {compandId: "test"}, true)
        .then(updatedItems => {
          let undefinedCount = 0;
          for (let item of updatedItems) {
            undefinedCount += item === undefined ? 1 : 0;
          }
          // console.log("result", result);
          expect(undefinedCount).toBe(0);
        })
  });
});

Update - Nov 2022:

It seems that test and it is interchangeable according to the official API of Jest. As @gwildu described here, you should choose one over the other for the sake of readability.

3
  • it might just be there for familiarity and migration from other frameworks.
    – Andrew Li
    Commented Aug 20, 2017 at 3:38
  • 70
    there is no difference. The documentation clearly states test is under the alias it.
    – Claies
    Commented Aug 20, 2017 at 4:00
  • Their logic is similar, but their semantics differ when it comes to reading Commented Nov 23, 2022 at 7:05

10 Answers 10

937

The Jest docs state it is an alias of test. So they are exactly the same from a functional point of view. They exist both to enable to make a readable English sentence from your test.

0
266

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.

4
  • 18
    some also prefer it('does this thing', () => {}) instead of it('should do this thing', () => {} as its shorter
    – gwildu
    Commented Dec 24, 2019 at 15:01
  • Alternatively, test('thing should do x') may be preferred over it('Should do X') as it is often vague. Commented Mar 6, 2020 at 16:31
  • 1
    @mikemaccana if you write test('thing should do x') you don't have a semantically correct sentence. The Idea behind those notation I think is really, that you can read a test in a sentence as you would speak. Same if you write test('Does this thing'). Of course you can do that, but the notation semantically would actually fit to the it notation
    – gwildu
    Commented Nov 10, 2020 at 7:32
  • 3
    test('thing should do x') is literally 'testing that thing should do X' - so the test reads as one would speak. test('thing does x') is also fine. Commented Nov 10, 2020 at 14:43
45

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:

12

As the jest documentation says, they are the same: it alias

test(name, fn, timeout)

Also under the alias: it(name, fn, timeout)

And describe is just for when you prefer your tests to be organized into groups: describe

describe(name, fn)

describe(name, fn) creates a block that groups together several related tests. For example, if you have a myBeverage object that is supposed to be delicious but not sour, you could test it with:

const myBeverage = {
  delicious: true,
  sour: false,
};

describe('my beverage', () => {
  test('is delicious', () => {
    expect(myBeverage.delicious).toBeTruthy();
  });

  test('is not sour', () => {
    expect(myBeverage.sour).toBeFalsy();
  });
});

This isn't required - you can write the test blocks directly at the top level. But this can be handy if you prefer your tests to be organized into groups.

9

You could replace it() with xit() to temporarily exclude a test from being executed; using it() and xit() is more eloquent than using test() and xit().

see Focusing and Excluding Tests

3
  • 4
    There is a "xtest" too, so both are pretty much alike.
    – yuriy636
    Commented Oct 19, 2021 at 6:47
  • xit() is also shorter
    – Adrian
    Commented Sep 6, 2022 at 16:53
  • And much less clear. Commented Feb 14 at 22:27
4

The following is an excerpt from the document:link

test(name, fn, timeout)

Also under the alias: it(name, fn, timeout)

All you need in a test file is the test method which runs a test. For example, let's say there's a function inchesOfRain() that should be zero. Your whole test could be: ......

-5

const request = require('supertest'); const app = require('../app') const {it, describe} = require('@jest/globals'); const { sequelize } = require('../models'); const {hash} = require('../helpers/bcrypt')

beforeAll(async ()=>{ await sequelize.queryInterface.bulkInsert('Customers', [{ username: "nikita", email:"[email protected]", password: hash("nikita"), createdAt: new Date(), updatedAt: new Date() }]) })

afterAll(async ()=>{ await sequelize.queryInterface.bulkDelete('Customers', null, { truncate: true, cascade: true, restartIdentity: true }) })

describe('POST /customers/register', () => { it('should response with status 201', async ()=> { let customer = { username: "hello" , email:"[email protected]", password:"hello", } let response = await request(app) .post('/customers/register').send(customer) expect(response.status).toBe(201) // console.log(response.body, 'ini ressss') expect(response.body).toEqual({ message: "Input data Customer succeed", id: expect.any(Number), email: expect.any(String) }) }); it('should response with status 400', async()=>{ let customer = { username: "hello", password:"hello" } let response = await request(app) .post('/customers/register').send(customer) expect(response.status).toBe(400) // console.log(response.body,"<<<"); expect(response.body[0].message).toBe('Please input email') })

1
  • 1
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Feb 7, 2023 at 22:44
-5
`const request = require('supertest');`
`const app =  require('../app')`
`const {it, describe} = require('@jest/globals');`
`const { sequelize, Category, User, News , Customer, Bookmark} = require('../models');`
`const {hash} = require('../helpers/bcrypt');`
`const news = require('../models/news');`
`const {queryInterface} = sequelize`


`beforeAll(async()=>{

    let userData = require("../data/user.json")
    userData.forEach(el => {
        el.password = hash(el.password)
        el.createdAt = new Date()
        el.updatedAt = new Date()
    });`

`afterAll(async ()=>{
    await Bookmark.destroy({
        truncate: true,
        cascade: true,
        restartIdentity: true
    })`

`describe('GET /customers/news', () => {
    it("should response with status 200 1", async () => {
        let response = await request(app)
        .get('/customers/news')
        // .set({access_token})
        // console.log(response.body, "<<<<NEWS NIhH");
        expect(response.status).toBe(200)
        expect(response.body).toBeInstanceOf(Array)
    })`

`it("should response with status 200 2", async()=>{
    let response = await request(app)
    .get('/customers/news?filter=1,2')
    expect(response.status).toBe(200)
    expect(response.body).toBeInstanceOf(Array)
})`
1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Feb 8, 2023 at 13:27
-20

Jest haven't mentioned why they have two versions for the exact same functionality.

My guess is, it's only for convention. test is for unit tests, and it is for integration tests.

0
-37

They are the same thing. I am using TypeScript as the programming language, and when I look into the definition file from the Jest package source code from /@types/jest/index.d.ts, I can see the following code.

Obviously, there are lots of different names of 'test', and you can use any of them.

declare var beforeAll: jest.Lifecycle;
declare var beforeEach: jest.Lifecycle;
declare var afterAll: jest.Lifecycle;
declare var afterEach: jest.Lifecycle;
declare var describe: jest.Describe;
declare var fdescribe: jest.Describe;
declare var xdescribe: jest.Describe;
declare var it: jest.It;
declare var fit: jest.It;
declare var xit: jest.It;
declare var test: jest.It;
declare var xtest: jest.It;

2
  • 41
    The code you show does not indicate that it and test are the same thing. It just means that their type is the same. I do not think that beforeAll and afterAll are the same thing even though their type is the same. Commented Apr 16, 2018 at 20:56
  • 6
    xit and xtest skips the tests, it, fit, test are to execute tests. Thanks for your answer.
    – Aakash
    Commented Nov 17, 2019 at 5:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.