Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- test('two plus two is four', () => {
- expect(2 + 2).toBe(4)
- })
- test('object assignment', () => {
- const data = { one: 1 }
- data['two'] = 2
- expect(data).toEqual({ one: 1, two: 2 })
- })
- test('adding positive numbers is not zero', () => {
- for (let a = 1; a < 10; a++) {
- for (let b = 1; b < 10; b++) {
- expect(a + b).not.toBe(0)
- }
- }
- })
- test('null', () => {
- const n = null
- expect(n).toBeNull()
- expect(n).toBeDefined()
- expect(n).not.toBeUndefined()
- expect(n).not.toBeTruthy()
- expect(n).toBeFalsy()
- })
- test('zero', () => {
- const z = 0
- expect(z).not.toBeNull()
- expect(z).toBeDefined()
- expect(z).not.toBeUndefined()
- expect(z).not.toBeTruthy()
- expect(z).toBeFalsy()
- expect(z).toBe(0)
- })
- test('two plus two', () => {
- const value = 2 + 2
- expect(value).toBeGreaterThan(3)
- expect(value).toBeGreaterThanOrEqual(3)
- expect(value).toBeLessThan(5)
- expect(value).toBeLessThanOrEqual(4.5)
- expect(value).toBe(4)
- expect(value).toEqual(4)
- })
- test('adding floating point number', () => {
- const value = 0.1 + 0.2
- expect(value).toBeCloseTo(0.3)
- })
- test('there is no I in team', () => {
- expect('team').not.toMatch(/I/)
- })
- test('but there is a "stop" in Christoph', () => {
- expect('Christoph').toMatch(/stop/)
- })
- test('the shopping list has beer on it', () => {
- const shoppingList = [
- 'diapers',
- 'kleenex',
- 'trash bags',
- 'paper towels',
- 'beer',
- ];
- expect(shoppingList).toContain('beer')
- expect(new Set(shoppingList)).toContain('beer')
- })
- class ConfigError extends Error {}
- function compileAndroidCode() {
- throw new ConfigError('you are using the wrong JDK')
- }
- test('compling android goes as expected', () => {
- expect(compileAndroidCode).toThrow()
- expect(compileAndroidCode).toThrow(ConfigError)
- expect(compileAndroidCode).toThrow('you are using the wrong JDK')
- expect(compileAndroidCode).toThrow(/JDK/)
- })
- // ===================== Callbacks ============================
- test('the data is peanut butter', done => {
- function fetchData(callback) {
- callback('peanut butter')
- }
- function callback(data) {
- setTimeout(() => {
- expect(data).toBe('peanut butter')
- done()
- }, 1000)
- }
- fetchData(callback)
- })
- // ==================== Promise ================================
- function promiseFetchData(success) {
- return new Promise((resolve, reject) => {
- setTimeout(() => {
- if (!success) return reject('error')
- return resolve('peanut butter')
- }, 100)
- })
- }
- it('the data is peanut butter', () => {
- const resp = 'qqq'
- const mockFunction = jest.fn(promiseFetchData)
- mockFunction.mockResolvedValue(resp)
- return mockFunction().then(data => {
- expect(data).toBe(resp)
- })
- })
- test('the fetch fails with an error', () => {
- return promiseFetchData(false).catch(error => {
- expect.assertions(1)
- expect(error).toMatch('error')
- })
- })
- test('promise: the data is peanut butter', () => {
- return expect(promiseFetchData(true)).resolves.toBe('peanut butter')
- })
- test('promise: the data is peanut butter', () => {
- return expect(promiseFetchData(false)).rejects.toBe('error')
- })
- // ==================== Async/Await ================================
- async function asyncFetchData(success) {
- return new Promise((resolve, reject) => {
- setTimeout(() => {
- if (!success) return reject('error')
- return resolve('peanut butter')
- }, 100)
- })
- }
- test('the data is peanut butter', async () => {
- expect.assertions(1)
- const data = await asyncFetchData(true)
- expect(data).toBe('peanut butter')
- })
- test('the fetch data fails with an error', async () => {
- expect.assertions(1)
- try {
- await asyncFetchData(false)
- } catch(e) {
- expect(e).toMatch('error')
- }
- })
- test('the data is peanut butter', async () => {
- await expect(asyncFetchData(true)).resolves.toBe('peanut butter')
- })
- test('the fetch fails with an error', async () => {
- await expect(asyncFetchData(false)).rejects.toMatch('error')
- })
- // ======================= Mock Functions ===============================
- function forEach(items, callback) {
- for (let index = 0; index < items.length; index++) {
- callback(items[index], 99)
- }
- }
- const mockCallback = jest.fn(x => 42 + x)
- test('forEach', () => {
- forEach([0, 1], mockCallback)
- expect(mockCallback.mock.calls.length).toBe(2)
- expect(mockCallback.mock.calls[0][0]).toBe(0)
- expect(mockCallback.mock.calls[1][0]).toBe(1)
- expect(mockCallback.mock.results[0].value).toBe(42)
- expect(mockCallback.mock.results[1].value).toBe(43)
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement