ChatGPT解决这个技术问题 Extra ChatGPT

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

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);
        })
  });
});

It seems that test is in the official API of Jest, but it is not.

it might just be there for familiarity and migration from other frameworks.
there is no difference. The documentation clearly states test is under the alias it.

m
musicformellons

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.


I think this answer is slightly misleading - it and test are not exactly the same. As explained in @gwilde answer stackoverflow.com/a/56072272/449347 these aliases encourage the developer to write a test name as a readable English sentence - so randomly using it or test will mess that up.
@k1eran You're right! I edited the answer accordingly.
P
Peter Mortensen

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.


some also prefer it('does this thing', () => {}) instead of it('should do this thing', () => {} as its shorter
Alternatively, test('thing should do x') may be preferred over it('Should do X') as it is often vague.
@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
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.
P
Peter Mortensen

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:

https://jestjs.io/docs/en/api.html#describename-fn

https://jestjs.io/docs/en/api.html#testname-fn-timeout


S
SeyyedKhandon

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.


A
Adrian

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


There is a "xtest" too, so both are pretty much alike.
L
Luca Kiebel

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: ......


P
Peter Mortensen

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.


P
Peter Mortensen

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;


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.
xit and xtest skips the tests, it, fit, test are to execute tests. Thanks for your answer.