Skip to content

Instantly share code, notes, and snippets.

@ANDREHORMAN1994
Last active March 27, 2024 21:49
Show Gist options
  • Save ANDREHORMAN1994/c286426835af0c17d3315c01fc0ba65c to your computer and use it in GitHub Desktop.
Save ANDREHORMAN1994/c286426835af0c17d3315c01fc0ba65c to your computer and use it in GitHub Desktop.
JEST

Criando testes com JEST

DOCS

Iniciando um teste

  • Crie um arquivo de test exemplo.test.js ou exemplo.spec.js
  • Crie uma estrutura usando a função Describe
// describe(name, fn) cria um bloco que agrupa vários testes relacionados.

describe('Testing my application', () => {
 ...
});
  • Crie um teste utilizando o verbo test ou it
// describe(name, fn) cria um bloco que agrupa vários testes relacionados.

describe('Testing my application', () => {

  test('if it does this thing', () => {
    ...
  });

  it('should do the other thing', () => {
    ...
  });

});

Exemplo com Numbers

test('two plus two', () => {
  const value = 2 + 2;

  expect(value).toBeGreaterThan(3);
  expect(value).toBeGreaterThanOrEqual(3.5);
  expect(value).toBeLessThan(5);
  expect(value).toBeLessThanOrEqual(4.5);
  expect(value).toBe(4);
  expect(value).toEqual(4);
});

Exemplo com Strings

test('is the same or not', () => {
  const name = 'andre'

  expect(name).toMatch(/dre/);
  expect(name).not.toMatch(/I/);
  expect(name).toBe('andre');
  expect(name).toEqual('andre');
});

Exemplo com Iterables

test('object assignment', () => {
  const data = { one: 1 };
  data.two = 2;
  
  const expectedValue = { one: 1, two: 2 };
  expect(data).toEqual(expectedValue);
});

test('list of games', () => {
  const games = ['Mario', 'Sonic', 'Zelda'];

  expect(games).toContain('Zelda');
  expect(games).not.toContain('Resident Evil');
});

Exemplo com Exceptions

function errorMessage() {
  throw new Error('Something is not Ok!!');
}

test('throwing an error', () => {
  expect(() => compileAndroidCode()).toThrow();
  expect(() => compileAndroidCode()).toThrow(Error);

  // You can also use a string that must be contained in the error message or a regexp
  expect(() => compileAndroidCode()).toThrowError('Something is not Ok!!');
  expect(() => compileAndroidCode()).toThrowError(/Ok/);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment