Jest, basitliğe odaklanan keyifli bir JavaScript Test Çerçevesidir. Babel, TypeScript, Node, React, Angular, Vue ve daha fazlasını kullanan projelerle çalışır!
Jest'i kurmak için şu komutu çalıştırın:
npm install --save-dev jest
package.json dosyanıza şu kısmı ekleyin:
{
  "scripts": {
    "test": "jest"
  }
}
Jest'te temel bir test şu şekilde görünür:
test('testin açıklaması', () => {
  // Test kodunuzu buraya yazın
  expect(someValue).toBe(expectedValue);
});
Ayrıca test yerine it de kullanabilirsiniz:
it('bir şey yapmalıdır', () => {
  // Test kodunuzu buraya yazın
});
Jest, değerleri farklı şekillerde test etmek için "matcher"lar kullanır. İşte bazı yaygın matcher'lar:
expect(2 + 2).toBe(4);
expect({name: 'John', age: 30}).toEqual({name: 'John', age: 30});
expect(null).toBeNull();
expect(undefined).toBeUndefined();
expect(true).toBeTruthy();
expect(false).toBeFalsy();
expect(value).toBeGreaterThan(3);
expect(value).toBeGreaterThanOrEqual(3.5);
expect(value).toBeLessThan(5);
expect(value).toBeLessThanOrEqual(4.5);
expect('team').not.toMatch(/I/);
expect('Christoph').toMatch(/stop/);
expect(['Apple', 'Banana', 'Orange']).toContain('Banana');
expect(() => {
  throw new Error('Yanlış JDK kullanıyorsunuz');
}).toThrow();
expect(() => {
  throw new Error('Yanlış JDK kullanıyorsunuz');
}).toThrow('Yanlış JDK kullanıyorsunuz');
expect(() => {
  throw new Error('Yanlış JDK kullanıyorsunuz');
}).toThrow(/JDK/);
test('veri peanut butter', () => {
  return fetchData().then(data => {
    expect(data).toBe('peanut butter');
  });
});
test('veri peanut butter', async () => {
  const data = await fetchData();
  expect(data).toBe('peanut butter');
});
test('veri peanut butter', done => {
  function callback(data) {
    try {
      expect(data).toBe('peanut butter');
      done();
    } catch (error) {
      done(error);
    }
  }
  fetchData(callback);
});
test('bir fonksiyonun mock implementasyonu', () => {
  const mock = jest.fn(() => 'bar');
  expect(mock('foo')).toBe('bar');
  expect(mock).toHaveBeenCalledWith('foo');
});
jest.mock('./myModule');
const myModule = require('./myModule');
test('bir şey yapmalıdır', () => {
  myModule.someFunction.mockReturnValue('mocked value');
  expect(myModule.someFunction()).toBe('mocked value');
});
jest.useFakeTimers();
test('oyunun bitmesi 1 saniye bekler', () => {
  const timerGame = require('../timerGame');
  timerGame();
  expect(setTimeout).toHaveBeenCalledTimes(1);
  expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 1000);
});
beforeAll(() => {
  // Tüm testlerden önce çalışır
});
afterAll(() => {
  // Tüm testlerden sonra çalışır
});
beforeEach(() => {
  // Her testten önce çalışır
});
afterEach(() => {
  // Her testten sonra çalışır
});
describe('şehirleri yiyeceklerle eşleştirme', () => {
  test('Viyana <3 sosis', () => {
    expect(isValidCityFoodPair('Viyana', 'Wiener Schnitzel')).toBe(true);
  });
  test('San Juan <3 plantain', () => {
    expect(isValidCityFoodPair('San Juan', 'Mofongo')).toBe(true);
  });
});
it('doğru şekilde render eder', () => {
  const tree = renderer
    .create(<Link page="http://www.facebook.com">Facebook</Link>)
    .toJSON();
  expect(tree).toMatchSnapshot();
});
package.json dosyanıza şu kısmı ekleyin:
{
  "jest": {
    "collectCoverage": true,
    "coverageReporters": ["json", "lcov", "text", "clover"]
  }
}
Veya Jest'i çalıştırırken --coverage bayrağını kullanın.
beforeEach ve afterEach kullanın.test.only Kullanın: Hata ayıklarken yalnızca bir testi çalıştırmak için test.only kullanın.2024 © Tüm hakları saklıdır - buraxta.com