Skip to content

Instantly share code, notes, and snippets.

@nandofalcao
Last active June 12, 2022 00:35
Show Gist options
  • Save nandofalcao/6e192ae1534d39a75442fdbb4de60bf5 to your computer and use it in GitHub Desktop.
Save nandofalcao/6e192ae1534d39a75442fdbb4de60bf5 to your computer and use it in GitHub Desktop.
Jest Best Practices - What is the difference between 'it' and 'test' in Jest?

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

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

RSpec style

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

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

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

xUnit style

function sum(a, b) {
  return a + b;
}

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

tip for test structure sequence

given - dado que

const post = {title: "Title Name"}

when - quando acontecer

const response = axios({url: 'localhost', method: 'post', body: post})

then - então

expect(response.body.id).toBe(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment