From d6488456198dea28ca4a3ff689e5a7b8add17e45 Mon Sep 17 00:00:00 2001 From: romanusovich1 Date: Wed, 28 Jun 2023 20:46:54 +0300 Subject: [PATCH 01/13] feat: add simple tests --- src/01-simple-tests/index.test.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/01-simple-tests/index.test.ts b/src/01-simple-tests/index.test.ts index fbbea85de..5afabfc8e 100644 --- a/src/01-simple-tests/index.test.ts +++ b/src/01-simple-tests/index.test.ts @@ -1,32 +1,32 @@ // Uncomment the code below and write your tests -// import { simpleCalculator, Action } from './index'; +import { simpleCalculator, Action } from './index'; describe('simpleCalculator tests', () => { test('should add two numbers', () => { - // Write your test here + expect(simpleCalculator({ a: 1, b: 2, action: Action.Add })).toBe(3); }); test('should subtract two numbers', () => { - // Write your test here + expect(simpleCalculator({ a: 3, b: 2, action: Action.Subtract })).toBe(1); }); test('should multiply two numbers', () => { - // Write your test here + expect(simpleCalculator({ a: 2, b: 2, action: Action.Multiply })).toBe(4); }); test('should divide two numbers', () => { - // Write your test here + expect(simpleCalculator({ a: 4, b: 2, action: Action.Divide })).toBe(2); }); test('should exponentiate two numbers', () => { - // Write your test here + expect(simpleCalculator({ a: 3, b: 2, action: Action.Exponentiate })).toBe(9); }); test('should return null for invalid action', () => { - // Write your test here + expect(simpleCalculator({ a: 2, b: 0, action: Action })).toBeNull(); }); test('should return null for invalid arguments', () => { - // Write your test here + expect(simpleCalculator({ a: '100', b: '0', action: Action.Exponentiate })).toBeNull(); }); }); From adb4b954624150ee4128988b79f3623c9fdf3617 Mon Sep 17 00:00:00 2001 From: romanusovich1 Date: Wed, 28 Jun 2023 21:05:18 +0300 Subject: [PATCH 02/13] feat: add table-driven tests --- src/02-table-tests/index.test.ts | 33 ++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/src/02-table-tests/index.test.ts b/src/02-table-tests/index.test.ts index 4f36e892e..8d0a6f353 100644 --- a/src/02-table-tests/index.test.ts +++ b/src/02-table-tests/index.test.ts @@ -1,17 +1,34 @@ // Uncomment the code below and write your tests -/* import { simpleCalculator, Action } from './index'; +import { simpleCalculator, Action } from './index'; const testCases = [ - { a: 1, b: 2, action: Action.Add, expected: 3 }, - { a: 2, b: 2, action: Action.Add, expected: 4 }, - { a: 3, b: 2, action: Action.Add, expected: 5 }, - // continue cases for other actions -]; */ + { a: 1, b: 2, action: Action.Add, expected: 3 }, + { a: 2, b: 2, action: Action.Add, expected: 4 }, + { a: 3, b: 2, action: Action.Add, expected: 5 }, + { a: 3, b: 2, action: Action.Subtract, expected: 1 }, + { a: 4, b: 2, action: Action.Subtract, expected: 2 }, + { a: 3, b: 3, action: Action.Subtract, expected: 0 }, + { a: 3, b: 2, action: Action.Multiply, expected: 6 }, + { a: 3, b: 3, action: Action.Multiply, expected: 9 }, + { a: 4, b: 2, action: Action.Multiply, expected: 8 }, + { a: 4, b: 2, action: Action.Divide, expected: 2 }, + { a: 4, b: 4, action: Action.Divide, expected: 1 }, + { a: 6, b: 2, action: Action.Divide, expected: 3 }, + { a: 3, b: 2, action: Action.Exponentiate, expected: 9 }, + { a: 3, b: 0, action: Action.Exponentiate, expected: 1 }, + { a: 5, b: 2, action: Action.Exponentiate, expected: 25 }, + { a: 3, b: 1, action: Action, expected: null }, + { a: 2, b: 2, action: Action, expected: null }, + { a: 1, b: 3, action: Action, expected: null }, + { a: '3', b: '1', action: Action.Add, expected: null }, + { a: '2', b: '2', action: Action.Subtract, expected: null }, + { a: '1', b: '3', action: Action.Multiply, expected: null }, +]; describe('simpleCalculator', () => { // This test case is just to run this test suite, remove it when you write your own tests - test('should blah-blah', () => { - expect(true).toBe(true); + test.each(testCases)('should return $expected if $a $action $b', ({ a, b, action, expected }) => { + expect(simpleCalculator({ a, b, action })).toBe(expected); }); // Consider to use Jest table tests API to test all cases above }); From 200472a268e4b4b5b147769ee2dfe0b3f96aeb64 Mon Sep 17 00:00:00 2001 From: romanusovich1 Date: Thu, 29 Jun 2023 20:34:32 +0300 Subject: [PATCH 03/13] feat: add error handling async tests --- src/03-error-handling-async/index.test.ts | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/03-error-handling-async/index.test.ts b/src/03-error-handling-async/index.test.ts index 6e106a6d6..9da95be25 100644 --- a/src/03-error-handling-async/index.test.ts +++ b/src/03-error-handling-async/index.test.ts @@ -1,30 +1,38 @@ // Uncomment the code below and write your tests -// import { throwError, throwCustomError, resolveValue, MyAwesomeError, rejectCustomError } from './index'; +import { throwError, throwCustomError, resolveValue, MyAwesomeError, rejectCustomError } from './index'; describe('resolveValue', () => { test('should resolve provided value', async () => { - // Write your test here + const value = 'value'; + const result = await resolveValue(value); + expect(result).toBe(value); }); }); describe('throwError', () => { test('should throw error with provided message', () => { - // Write your test here + expect(() => { throwError('Error') }).toThrow('Error'); }); test('should throw error with default message if message is not provided', () => { - // Write your test here + expect(() => { throwError() }).toThrow('Oops!'); }); }); describe('throwCustomError', () => { test('should throw custom error', () => { - // Write your test here + expect(() => { throwCustomError() }).toThrow(MyAwesomeError); + expect(() => { throwCustomError() }).toThrow('This is my awesome custom error!'); }); }); describe('rejectCustomError', () => { test('should reject custom error', async () => { - // Write your test here + try { + await rejectCustomError(); + } catch (e: any) { + expect(e).toBeInstanceOf(MyAwesomeError); + expect(e.message).toContain('This is my awesome custom error!'); + } }); }); From 4f7c6c46227a6a5775d69e4cb95cc25b43b5c3ef Mon Sep 17 00:00:00 2001 From: romanusovich1 Date: Thu, 29 Jun 2023 21:50:07 +0300 Subject: [PATCH 04/13] feat: add class test --- src/04-test-class/index.test.ts | 56 ++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 11 deletions(-) diff --git a/src/04-test-class/index.test.ts b/src/04-test-class/index.test.ts index 937490d82..24bdd6074 100644 --- a/src/04-test-class/index.test.ts +++ b/src/04-test-class/index.test.ts @@ -1,44 +1,78 @@ // Uncomment the code below and write your tests -// import { getBankAccount } from '.'; +import { getBankAccount, BankAccount, InsufficientFundsError, TransferFailedError, SynchronizationFailedError } from '.'; + +import * as lodash from 'lodash'; +jest.mock('lodash'); describe('BankAccount', () => { + let balance: number; + let bankAcc: BankAccount; + let anotherBankAcc: BankAccount; + let fetchBalance: number; + let spyLodashRandom = jest.spyOn(lodash, 'random'); + + beforeEach(() => { + jest.clearAllMocks(); + + balance = 1000; + bankAcc = getBankAccount(balance); + anotherBankAcc = getBankAccount(balance); + + fetchBalance = 99; + spyLodashRandom.mockReturnValueOnce(fetchBalance); + }); + test('should create account with initial balance', () => { - // Write your test here + expect(bankAcc).toBeInstanceOf(BankAccount); + expect(bankAcc.getBalance()).toBe(balance); }); test('should throw InsufficientFundsError error when withdrawing more than balance', () => { - // Write your test here + expect(() => { bankAcc.withdraw(balance ** 2) }).toThrow(InsufficientFundsError); }); test('should throw error when transferring more than balance', () => { - // Write your test here + expect(() => { bankAcc.transfer(balance ** 2, anotherBankAcc) }).toThrow(InsufficientFundsError); }); test('should throw error when transferring to the same account', () => { - // Write your test here + expect(() => { bankAcc.transfer(balance, bankAcc) }).toThrow(TransferFailedError); }); test('should deposit money', () => { - // Write your test here + bankAcc.deposit(balance); + expect(bankAcc.getBalance()).toBe(balance + balance); }); test('should withdraw money', () => { - // Write your test here + bankAcc.withdraw(balance); + expect(bankAcc.getBalance()).toBe(balance - balance); }); test('should transfer money', () => { - // Write your test here + bankAcc.transfer(balance, anotherBankAcc); + expect(bankAcc.getBalance()).toBe(balance - balance); + expect(anotherBankAcc.getBalance()).toBe(balance + balance); }); test('fetchBalance should return number in case if request did not failed', async () => { - // Write your tests here + spyLodashRandom.mockReturnValueOnce(1); + const result = await bankAcc.fetchBalance() + expect(result).toBe(fetchBalance); }); test('should set new balance if fetchBalance returned number', async () => { - // Write your tests here + spyLodashRandom.mockReturnValueOnce(1); + await bankAcc.synchronizeBalance(); + expect(bankAcc.getBalance()).toBe(fetchBalance); }); test('should throw SynchronizationFailedError if fetchBalance returned null', async () => { - // Write your tests here + spyLodashRandom.mockReturnValueOnce(0); + try { + await bankAcc.synchronizeBalance(); + } catch (e) { + expect(e).toBeInstanceOf(SynchronizationFailedError); + } }); }); From fc64a5b5a17b8706b03dae95e3a14cfe7330e6b5 Mon Sep 17 00:00:00 2001 From: romanusovich1 Date: Fri, 30 Jun 2023 18:06:00 +0300 Subject: [PATCH 05/13] feat: add mocking node modules test --- src/06-mocking-node-api/index.test.ts | 66 +++++++++++++++++++++++---- 1 file changed, 57 insertions(+), 9 deletions(-) diff --git a/src/06-mocking-node-api/index.test.ts b/src/06-mocking-node-api/index.test.ts index 8dc3afd79..7e75b467f 100644 --- a/src/06-mocking-node-api/index.test.ts +++ b/src/06-mocking-node-api/index.test.ts @@ -1,9 +1,19 @@ // Uncomment the code below and write your tests -// import { readFileAsynchronously, doStuffByTimeout, doStuffByInterval } from '.'; +import { doStuffByTimeout, doStuffByInterval, readFileAsynchronously } from '.'; +import * as fs from 'fs'; // existsSync +import * as fsp from 'fs/promises'; // readFile +import * as path from 'path'; // join +jest.mock('fs'); +jest.mock('fs/promises'); +jest.mock('path'); describe('doStuffByTimeout', () => { + const callback = jest.fn(); + const timeout = 150; + beforeAll(() => { jest.useFakeTimers(); + // jest.spyOn(global, 'setTimeout'); // just doesnt work }); afterAll(() => { @@ -11,17 +21,29 @@ describe('doStuffByTimeout', () => { }); test('should set timeout with provided callback and timeout', () => { - // Write your test here + // expect(setTimeout).toHaveBeenLastCalledWith(callback, timeout); // just doesnt work + doStuffByTimeout(callback, timeout); + expect(callback).not.toBeCalled(); + jest.advanceTimersByTime(timeout); + expect(callback).toBeCalledTimes(1); }); test('should call callback only after timeout', () => { - // Write your test here + doStuffByTimeout(callback, timeout); + expect(callback).not.toBeCalled(); + jest.advanceTimersByTime(timeout); + expect(callback).toBeCalledTimes(1); }); }); describe('doStuffByInterval', () => { - beforeAll(() => { + let callback: any; + const timeout = 150; + + beforeEach(() => { jest.useFakeTimers(); + callback = jest.fn(); + // jest.spyOn(global, 'setInterval'); // just doesnt work }); afterAll(() => { @@ -29,24 +51,50 @@ describe('doStuffByInterval', () => { }); test('should set interval with provided callback and timeout', () => { - // Write your test here + // expect(setInterval).toHaveBeenLastCalledWith(callback, timeout); // just doesnt work + doStuffByInterval(callback, timeout); + expect(callback).not.toBeCalled(); + jest.advanceTimersByTime(timeout); + expect(callback).toBeCalledTimes(1); }); test('should call callback multiple times after multiple intervals', () => { - // Write your test here + doStuffByInterval(callback, timeout); + expect(callback).not.toBeCalled(); + jest.advanceTimersByTime(timeout); + expect(callback).toBeCalledTimes(1); + jest.advanceTimersByTime(timeout); + expect(callback).toBeCalledTimes(2); + jest.advanceTimersByTime(timeout); + expect(callback).toBeCalledTimes(3); }); }); describe('readFileAsynchronously', () => { + jest.spyOn(path, 'join'); + const existSpy = jest.spyOn(fs, 'existsSync'); + const readFileSpy = jest.spyOn(fsp, 'readFile'); + const filePath = 'index.ts'; + const text = 'some text'; + test('should call join with pathToFile', async () => { - // Write your test here + readFileAsynchronously(filePath); + expect(path.join).toBeCalledTimes(1); }); test('should return null if file does not exist', async () => { - // Write your test here + existSpy.mockReturnValueOnce(false); + const result = await readFileAsynchronously(filePath); + expect(result).toBeNull(); }); test('should return file content if file exists', async () => { - // Write your test here + existSpy.mockReturnValueOnce(true); + readFileSpy.mockReturnValueOnce(new Promise((resolve, reject) => { + resolve(text); + reject(text); + })); + const result = await readFileAsynchronously(filePath); + expect(result).toBe(text); }); }); From 8edd50bb6483c78f083fa64975f61536767fbcbd Mon Sep 17 00:00:00 2001 From: romanusovich1 Date: Mon, 3 Jul 2023 11:35:35 +0300 Subject: [PATCH 06/13] fix: edit mocking node tests --- src/06-mocking-node-api/index.test.ts | 32 +++++++++++++++------------ 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/06-mocking-node-api/index.test.ts b/src/06-mocking-node-api/index.test.ts index 7e75b467f..4a56da2ac 100644 --- a/src/06-mocking-node-api/index.test.ts +++ b/src/06-mocking-node-api/index.test.ts @@ -11,9 +11,12 @@ describe('doStuffByTimeout', () => { const callback = jest.fn(); const timeout = 150; + afterEach(() => { + jest.clearAllTimers(); + }); + beforeAll(() => { jest.useFakeTimers(); - // jest.spyOn(global, 'setTimeout'); // just doesnt work }); afterAll(() => { @@ -21,11 +24,9 @@ describe('doStuffByTimeout', () => { }); test('should set timeout with provided callback and timeout', () => { - // expect(setTimeout).toHaveBeenLastCalledWith(callback, timeout); // just doesnt work + const setTimeoutSpy = jest.spyOn(global, 'setTimeout'); doStuffByTimeout(callback, timeout); - expect(callback).not.toBeCalled(); - jest.advanceTimersByTime(timeout); - expect(callback).toBeCalledTimes(1); + expect(setTimeoutSpy).toBeCalledWith(callback, timeout); }); test('should call callback only after timeout', () => { @@ -40,22 +41,26 @@ describe('doStuffByInterval', () => { let callback: any; const timeout = 150; + afterEach(() => { + jest.clearAllTimers(); + }); + beforeEach(() => { - jest.useFakeTimers(); callback = jest.fn(); - // jest.spyOn(global, 'setInterval'); // just doesnt work }); + beforeAll(() => { + jest.useFakeTimers(); + }); + afterAll(() => { jest.useRealTimers(); }); test('should set interval with provided callback and timeout', () => { - // expect(setInterval).toHaveBeenLastCalledWith(callback, timeout); // just doesnt work + const setIntervalSpy = jest.spyOn(global, 'setInterval'); doStuffByInterval(callback, timeout); - expect(callback).not.toBeCalled(); - jest.advanceTimersByTime(timeout); - expect(callback).toBeCalledTimes(1); + expect(setIntervalSpy).toBeCalledWith(callback, timeout); }); test('should call callback multiple times after multiple intervals', () => { @@ -74,7 +79,7 @@ describe('readFileAsynchronously', () => { jest.spyOn(path, 'join'); const existSpy = jest.spyOn(fs, 'existsSync'); const readFileSpy = jest.spyOn(fsp, 'readFile'); - const filePath = 'index.ts'; + const filePath = 'asd.ts'; const text = 'some text'; test('should call join with pathToFile', async () => { @@ -90,9 +95,8 @@ describe('readFileAsynchronously', () => { test('should return file content if file exists', async () => { existSpy.mockReturnValueOnce(true); - readFileSpy.mockReturnValueOnce(new Promise((resolve, reject) => { + readFileSpy.mockReturnValueOnce(new Promise((resolve) => { resolve(text); - reject(text); })); const result = await readFileAsynchronously(filePath); expect(result).toBe(text); From 59ec1b9302f7510c04e52ee8356d282aad851a02 Mon Sep 17 00:00:00 2001 From: romanusovich1 Date: Mon, 3 Jul 2023 11:45:36 +0300 Subject: [PATCH 07/13] feat: add partial mocking tests --- src/05-partial-mocking/index.test.ts | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/05-partial-mocking/index.test.ts b/src/05-partial-mocking/index.test.ts index 9d8a66cbd..5f870befb 100644 --- a/src/05-partial-mocking/index.test.ts +++ b/src/05-partial-mocking/index.test.ts @@ -1,8 +1,16 @@ // Uncomment the code below and write your tests -// import { mockOne, mockTwo, mockThree, unmockedFunction } from './index'; +import { mockOne, mockTwo, mockThree, unmockedFunction } from './index'; jest.mock('./index', () => { - // const originalModule = jest.requireActual('./index'); + const originalModule = jest.requireActual('./index'); + + return { + __esModule: true, + ...originalModule, + mockOne: jest.fn(), + mockTwo: jest.fn(), + mockThree: jest.fn(), + }; }); describe('partial mocking', () => { @@ -11,10 +19,16 @@ describe('partial mocking', () => { }); test('mockOne, mockTwo, mockThree should not log into console', () => { - // Write your test here + const consoleSpy = jest.spyOn(console, 'log'); + mockOne(); + mockTwo(); + mockThree(); + expect(consoleSpy).not.toBeCalled(); }); test('unmockedFunction should log into console', () => { - // Write your test here + const consoleSpy = jest.spyOn(console, 'log'); + unmockedFunction(); + expect(consoleSpy).toBeCalled(); }); }); From d4a1da1e5d5d96e41e070e1e973992653f32f376 Mon Sep 17 00:00:00 2001 From: romanusovich1 Date: Mon, 3 Jul 2023 11:57:07 +0300 Subject: [PATCH 08/13] feat: add snapshot test --- .../__snapshots__/index.test.ts.snap | 17 +++++++++++++++ src/08-snapshot-testing/index.test.ts | 21 ++++++++++++++++--- 2 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 src/08-snapshot-testing/__snapshots__/index.test.ts.snap diff --git a/src/08-snapshot-testing/__snapshots__/index.test.ts.snap b/src/08-snapshot-testing/__snapshots__/index.test.ts.snap new file mode 100644 index 000000000..b06ad6e10 --- /dev/null +++ b/src/08-snapshot-testing/__snapshots__/index.test.ts.snap @@ -0,0 +1,17 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`generateLinkedList should generate linked list from values 2 1`] = ` +{ + "next": { + "next": { + "next": { + "next": null, + "value": null, + }, + "value": 3, + }, + "value": 2, + }, + "value": 1, +} +`; diff --git a/src/08-snapshot-testing/index.test.ts b/src/08-snapshot-testing/index.test.ts index 67c345706..b5347e3dc 100644 --- a/src/08-snapshot-testing/index.test.ts +++ b/src/08-snapshot-testing/index.test.ts @@ -1,14 +1,29 @@ // Uncomment the code below and write your tests -// import { generateLinkedList } from './index'; +import { generateLinkedList } from './index'; describe('generateLinkedList', () => { + const values = [1, 2, 3]; + const ll = generateLinkedList(values); + // Check match by expect(...).toStrictEqual(...) test('should generate linked list from values 1', () => { - // Write your test here + expect(ll).toStrictEqual({ + "next": { + "next": { + "next": { + "next": null, + "value": null, + }, + "value": values[2], + }, + "value": values[1], + }, + "value": values[0], + }); }); // Check match by comparison with snapshot test('should generate linked list from values 2', () => { - // Write your test here + expect(ll).toMatchSnapshot(); }); }); From bbd8396a86bc17de54ee739e1585b2a753df2d75 Mon Sep 17 00:00:00 2001 From: romanusovich1 Date: Mon, 3 Jul 2023 15:26:28 +0300 Subject: [PATCH 09/13] feat: add mocking lib tests --- src/07-mocking-lib-api/index.test.ts | 34 ++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/src/07-mocking-lib-api/index.test.ts b/src/07-mocking-lib-api/index.test.ts index e1dd001ef..4abfa8d7a 100644 --- a/src/07-mocking-lib-api/index.test.ts +++ b/src/07-mocking-lib-api/index.test.ts @@ -1,17 +1,41 @@ // Uncomment the code below and write your tests -/* import axios from 'axios'; -import { throttledGetDataFromApi } from './index'; */ +import axios from 'axios'; +import { throttledGetDataFromApi } from './index'; + +jest.mock('lodash', () => { + const originalModule = jest.requireActual('lodash'); + + return { + __esModule: true, + ...originalModule, + throttle: jest.fn((fn) => fn), + }; +}); describe('throttledGetDataFromApi', () => { + const relativePath = 'todos/1'; + test('should create instance with provided base url', async () => { - // Write your test here + const axiosCreateSpy = jest.spyOn(axios, 'create'); + await throttledGetDataFromApi(relativePath); + expect(axiosCreateSpy).toBeCalledWith({ + baseURL: 'https://jsonplaceholder.typicode.com', + }); }); test('should perform request to correct provided url', async () => { - // Write your test here + const axiosGetSpy = jest.spyOn(axios.Axios.prototype, 'get'); + await throttledGetDataFromApi(relativePath); + expect(axiosGetSpy).toHaveBeenCalledWith(relativePath); }); test('should return response data', async () => { - // Write your test here + const result = await throttledGetDataFromApi(relativePath); + expect(result).toStrictEqual({ + userId: 1, + id: 1, + title: "delectus aut autem", + completed: false + }); }); }); From d4d6d3357ae5f2830467cdd1ee131faf238d630e Mon Sep 17 00:00:00 2001 From: romanusovich1 Date: Mon, 3 Jul 2023 15:42:01 +0300 Subject: [PATCH 10/13] fix: solve eslint warnings --- src/01-simple-tests/index.test.ts | 8 +++++-- src/02-table-tests/index.test.ts | 9 +++++--- src/03-error-handling-async/index.test.ts | 27 +++++++++++++++++------ src/04-test-class/index.test.ts | 24 +++++++++++++++----- src/05-partial-mocking/index.test.ts | 3 ++- src/06-mocking-node-api/index.test.ts | 12 +++++----- src/07-mocking-lib-api/index.test.ts | 4 ++-- src/08-snapshot-testing/index.test.ts | 16 +++++++------- 8 files changed, 69 insertions(+), 34 deletions(-) diff --git a/src/01-simple-tests/index.test.ts b/src/01-simple-tests/index.test.ts index 5afabfc8e..8194a9fe4 100644 --- a/src/01-simple-tests/index.test.ts +++ b/src/01-simple-tests/index.test.ts @@ -19,7 +19,9 @@ describe('simpleCalculator tests', () => { }); test('should exponentiate two numbers', () => { - expect(simpleCalculator({ a: 3, b: 2, action: Action.Exponentiate })).toBe(9); + expect(simpleCalculator({ a: 3, b: 2, action: Action.Exponentiate })).toBe( + 9, + ); }); test('should return null for invalid action', () => { @@ -27,6 +29,8 @@ describe('simpleCalculator tests', () => { }); test('should return null for invalid arguments', () => { - expect(simpleCalculator({ a: '100', b: '0', action: Action.Exponentiate })).toBeNull(); + expect( + simpleCalculator({ a: '100', b: '0', action: Action.Exponentiate }), + ).toBeNull(); }); }); diff --git a/src/02-table-tests/index.test.ts b/src/02-table-tests/index.test.ts index 8d0a6f353..8fe9b9df9 100644 --- a/src/02-table-tests/index.test.ts +++ b/src/02-table-tests/index.test.ts @@ -27,8 +27,11 @@ const testCases = [ describe('simpleCalculator', () => { // This test case is just to run this test suite, remove it when you write your own tests - test.each(testCases)('should return $expected if $a $action $b', ({ a, b, action, expected }) => { - expect(simpleCalculator({ a, b, action })).toBe(expected); - }); + test.each(testCases)( + 'should return $expected if $a $action $b', + ({ a, b, action, expected }) => { + expect(simpleCalculator({ a, b, action })).toBe(expected); + }, + ); // Consider to use Jest table tests API to test all cases above }); diff --git a/src/03-error-handling-async/index.test.ts b/src/03-error-handling-async/index.test.ts index 9da95be25..4deea023d 100644 --- a/src/03-error-handling-async/index.test.ts +++ b/src/03-error-handling-async/index.test.ts @@ -1,5 +1,11 @@ // Uncomment the code below and write your tests -import { throwError, throwCustomError, resolveValue, MyAwesomeError, rejectCustomError } from './index'; +import { + throwError, + throwCustomError, + resolveValue, + MyAwesomeError, + rejectCustomError, +} from './index'; describe('resolveValue', () => { test('should resolve provided value', async () => { @@ -11,18 +17,26 @@ describe('resolveValue', () => { describe('throwError', () => { test('should throw error with provided message', () => { - expect(() => { throwError('Error') }).toThrow('Error'); + expect(() => { + throwError('Error'); + }).toThrow('Error'); }); test('should throw error with default message if message is not provided', () => { - expect(() => { throwError() }).toThrow('Oops!'); + expect(() => { + throwError(); + }).toThrow('Oops!'); }); }); describe('throwCustomError', () => { test('should throw custom error', () => { - expect(() => { throwCustomError() }).toThrow(MyAwesomeError); - expect(() => { throwCustomError() }).toThrow('This is my awesome custom error!'); + expect(() => { + throwCustomError(); + }).toThrow(MyAwesomeError); + expect(() => { + throwCustomError(); + }).toThrow('This is my awesome custom error!'); }); }); @@ -30,9 +44,8 @@ describe('rejectCustomError', () => { test('should reject custom error', async () => { try { await rejectCustomError(); - } catch (e: any) { + } catch (e) { expect(e).toBeInstanceOf(MyAwesomeError); - expect(e.message).toContain('This is my awesome custom error!'); } }); }); diff --git a/src/04-test-class/index.test.ts b/src/04-test-class/index.test.ts index 24bdd6074..d4f66e1e9 100644 --- a/src/04-test-class/index.test.ts +++ b/src/04-test-class/index.test.ts @@ -1,5 +1,11 @@ // Uncomment the code below and write your tests -import { getBankAccount, BankAccount, InsufficientFundsError, TransferFailedError, SynchronizationFailedError } from '.'; +import { + getBankAccount, + BankAccount, + InsufficientFundsError, + TransferFailedError, + SynchronizationFailedError, +} from '.'; import * as lodash from 'lodash'; jest.mock('lodash'); @@ -9,7 +15,7 @@ describe('BankAccount', () => { let bankAcc: BankAccount; let anotherBankAcc: BankAccount; let fetchBalance: number; - let spyLodashRandom = jest.spyOn(lodash, 'random'); + const spyLodashRandom = jest.spyOn(lodash, 'random'); beforeEach(() => { jest.clearAllMocks(); @@ -28,15 +34,21 @@ describe('BankAccount', () => { }); test('should throw InsufficientFundsError error when withdrawing more than balance', () => { - expect(() => { bankAcc.withdraw(balance ** 2) }).toThrow(InsufficientFundsError); + expect(() => { + bankAcc.withdraw(balance ** 2); + }).toThrow(InsufficientFundsError); }); test('should throw error when transferring more than balance', () => { - expect(() => { bankAcc.transfer(balance ** 2, anotherBankAcc) }).toThrow(InsufficientFundsError); + expect(() => { + bankAcc.transfer(balance ** 2, anotherBankAcc); + }).toThrow(InsufficientFundsError); }); test('should throw error when transferring to the same account', () => { - expect(() => { bankAcc.transfer(balance, bankAcc) }).toThrow(TransferFailedError); + expect(() => { + bankAcc.transfer(balance, bankAcc); + }).toThrow(TransferFailedError); }); test('should deposit money', () => { @@ -57,7 +69,7 @@ describe('BankAccount', () => { test('fetchBalance should return number in case if request did not failed', async () => { spyLodashRandom.mockReturnValueOnce(1); - const result = await bankAcc.fetchBalance() + const result = await bankAcc.fetchBalance(); expect(result).toBe(fetchBalance); }); diff --git a/src/05-partial-mocking/index.test.ts b/src/05-partial-mocking/index.test.ts index 5f870befb..993cd9a70 100644 --- a/src/05-partial-mocking/index.test.ts +++ b/src/05-partial-mocking/index.test.ts @@ -2,7 +2,8 @@ import { mockOne, mockTwo, mockThree, unmockedFunction } from './index'; jest.mock('./index', () => { - const originalModule = jest.requireActual('./index'); + const originalModule = + jest.requireActual('./index'); return { __esModule: true, diff --git a/src/06-mocking-node-api/index.test.ts b/src/06-mocking-node-api/index.test.ts index 4a56da2ac..b403ee16f 100644 --- a/src/06-mocking-node-api/index.test.ts +++ b/src/06-mocking-node-api/index.test.ts @@ -38,7 +38,7 @@ describe('doStuffByTimeout', () => { }); describe('doStuffByInterval', () => { - let callback: any; + let callback: jest.Mock; const timeout = 150; afterEach(() => { @@ -52,7 +52,7 @@ describe('doStuffByInterval', () => { beforeAll(() => { jest.useFakeTimers(); }); - + afterAll(() => { jest.useRealTimers(); }); @@ -95,9 +95,11 @@ describe('readFileAsynchronously', () => { test('should return file content if file exists', async () => { existSpy.mockReturnValueOnce(true); - readFileSpy.mockReturnValueOnce(new Promise((resolve) => { - resolve(text); - })); + readFileSpy.mockReturnValueOnce( + new Promise((resolve) => { + resolve(text); + }), + ); const result = await readFileAsynchronously(filePath); expect(result).toBe(text); }); diff --git a/src/07-mocking-lib-api/index.test.ts b/src/07-mocking-lib-api/index.test.ts index 4abfa8d7a..80a98000f 100644 --- a/src/07-mocking-lib-api/index.test.ts +++ b/src/07-mocking-lib-api/index.test.ts @@ -34,8 +34,8 @@ describe('throttledGetDataFromApi', () => { expect(result).toStrictEqual({ userId: 1, id: 1, - title: "delectus aut autem", - completed: false + title: 'delectus aut autem', + completed: false, }); }); }); diff --git a/src/08-snapshot-testing/index.test.ts b/src/08-snapshot-testing/index.test.ts index b5347e3dc..ae6c5d388 100644 --- a/src/08-snapshot-testing/index.test.ts +++ b/src/08-snapshot-testing/index.test.ts @@ -8,17 +8,17 @@ describe('generateLinkedList', () => { // Check match by expect(...).toStrictEqual(...) test('should generate linked list from values 1', () => { expect(ll).toStrictEqual({ - "next": { - "next": { - "next": { - "next": null, - "value": null, + next: { + next: { + next: { + next: null, + value: null, }, - "value": values[2], + value: values[2], }, - "value": values[1], + value: values[1], }, - "value": values[0], + value: values[0], }); }); From 1cbcc4c4ce9ac7ea38b0535243168497e20245ea Mon Sep 17 00:00:00 2001 From: romanusovich1 Date: Mon, 3 Jul 2023 16:03:51 +0300 Subject: [PATCH 11/13] fix: edit mocking lib test --- src/07-mocking-lib-api/index.test.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/07-mocking-lib-api/index.test.ts b/src/07-mocking-lib-api/index.test.ts index 80a98000f..724f09430 100644 --- a/src/07-mocking-lib-api/index.test.ts +++ b/src/07-mocking-lib-api/index.test.ts @@ -30,12 +30,14 @@ describe('throttledGetDataFromApi', () => { }); test('should return response data', async () => { + const data = 'data'; + jest.spyOn(axios.Axios.prototype, 'get').mockImplementation( + () => + new Promise((resolve) => { + resolve({ data }); + }), + ); const result = await throttledGetDataFromApi(relativePath); - expect(result).toStrictEqual({ - userId: 1, - id: 1, - title: 'delectus aut autem', - completed: false, - }); + expect(result).toBe(data); }); }); From fe76aef2acbb83836a654042d5dbbef60cf11543 Mon Sep 17 00:00:00 2001 From: romanusovich1 Date: Mon, 3 Jul 2023 16:04:07 +0300 Subject: [PATCH 12/13] refactor: edit snapshot test --- src/08-snapshot-testing/index.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/08-snapshot-testing/index.test.ts b/src/08-snapshot-testing/index.test.ts index ae6c5d388..3a7603397 100644 --- a/src/08-snapshot-testing/index.test.ts +++ b/src/08-snapshot-testing/index.test.ts @@ -14,11 +14,11 @@ describe('generateLinkedList', () => { next: null, value: null, }, - value: values[2], + value: 3, }, - value: values[1], + value: 2, }, - value: values[0], + value: 1, }); }); From 5564abee7df6fc7ea6dd1fbb5072fc8f9fe4fc4d Mon Sep 17 00:00:00 2001 From: romanusovich1 Date: Mon, 3 Jul 2023 16:22:17 +0300 Subject: [PATCH 13/13] fix: edit mocking lib test --- src/07-mocking-lib-api/index.test.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/07-mocking-lib-api/index.test.ts b/src/07-mocking-lib-api/index.test.ts index 724f09430..910a6b07f 100644 --- a/src/07-mocking-lib-api/index.test.ts +++ b/src/07-mocking-lib-api/index.test.ts @@ -14,6 +14,16 @@ jest.mock('lodash', () => { describe('throttledGetDataFromApi', () => { const relativePath = 'todos/1'; + const data = 'data'; + + beforeEach(() => { + jest.spyOn(axios.Axios.prototype, 'get').mockImplementation( + () => + new Promise((resolve) => { + resolve({ data }); + }), + ); + }); test('should create instance with provided base url', async () => { const axiosCreateSpy = jest.spyOn(axios, 'create'); @@ -30,13 +40,6 @@ describe('throttledGetDataFromApi', () => { }); test('should return response data', async () => { - const data = 'data'; - jest.spyOn(axios.Axios.prototype, 'get').mockImplementation( - () => - new Promise((resolve) => { - resolve({ data }); - }), - ); const result = await throttledGetDataFromApi(relativePath); expect(result).toBe(data); });