Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic testing #1

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
tsconfigRootDir : __dirname,
tsconfigRootDir : __dirname,
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
Expand All @@ -20,5 +20,6 @@ module.exports = {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-unused-vars': 'off' // because it does not work with jest.beforeEach() and shows warnings anyway
},
};
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,5 @@ dist
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

.idea/
10 changes: 5 additions & 5 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
presets: [
['@babel/preset-env', { targets: { node: 'current' } }],
'@babel/preset-typescript',
],
};
presets: [
['@babel/preset-env', { targets: { node: 'current' } }],
'@babel/preset-typescript',
],
};
34 changes: 25 additions & 9 deletions src/01-simple-tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,48 @@
// Uncomment the code below and write your tests
// import { simpleCalculator, Action } from './index';

import { Action, simpleCalculator } from './index';

describe('simpleCalculator tests', () => {
test('should add two numbers', () => {
// Write your test here
const action = Action.Add;
expect(simpleCalculator({ a: 48, b: -3, action: action })).toEqual(45);
});

test('should subtract two numbers', () => {
// Write your test here
const action = Action.Subtract;
expect(simpleCalculator({ a: 48, b: -3, action: action })).toEqual(51);
});

test('should multiply two numbers', () => {
// Write your test here
const action = Action.Multiply;
expect(simpleCalculator({ a: 48, b: -3, action: action })).toEqual(-144);
});

test('should divide two numbers', () => {
// Write your test here
const action = Action.Divide;
expect(simpleCalculator({ a: 48, b: -3, action: action })).toEqual(-16);
});

test('should exponentiate two numbers', () => {
// Write your test here
const action = Action.Exponentiate;
expect(simpleCalculator({ a: 48, b: 3, action: action })).toEqual(110592);
});

test('should return null for invalid action', () => {
// Write your test here
expect(simpleCalculator({ a: 48, b: 3, action: 'NOT_EXISTING' })).toEqual(
null,
);
});

test('should return null for invalid arguments', () => {
// Write your test here
});
test.each([Object.values(Action)])(
'should return null for invalid arguments',
(action) => {
expect(simpleCalculator({ a: '5', b: '6', action: action })).toEqual(
null,
);
expect(simpleCalculator({ a: 2, b: '7', action: action })).toEqual(null);
expect(simpleCalculator({ a: 'a', b: 5, action: action })).toEqual(null);
},
);
});
47 changes: 36 additions & 11 deletions src/02-table-tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,42 @@
// 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: 0, b: 2, action: Action.Subtract, expected: -2 },
{ a: 3, b: 2, action: Action.Multiply, expected: 6 },
{ a: 3, b: 3, action: Action.Multiply, expected: 9 },
{ a: 3, b: -2, action: Action.Multiply, expected: -6 },
{ a: 3, b: -2, action: Action.Divide, expected: -1.5 },
{ a: 0, b: 3, action: Action.Divide, expected: 0 },
{ a: 10000, b: -20, action: Action.Divide, expected: -500 },
{ a: 10, b: 2, action: Action.Exponentiate, expected: 100 },
{ a: 10, b: 0, action: Action.Exponentiate, expected: 1 },
{ a: 1, b: '2', action: Action.Add, expected: null },
{ a: '2', b: 2, action: Action.Add, expected: null },
{ a: 3, b: '2', action: Action.Add, expected: null },
{ a: 3, b: undefined, action: Action.Subtract, expected: null },
{ a: 0, b: {}, action: Action.Subtract, expected: null },
{ a: 3, b: null, action: Action.Multiply, expected: null },
{ a: 3, b: '3', action: Action.Multiply, expected: null },
{ a: 3, b: '-2', action: Action.Multiply, expected: null },
{ a: 3, b: {}, action: Action.Divide, expected: null },
{ a: 0, b: [3], action: Action.Divide, expected: null },
{ a: 10000, b: [-20], action: Action.Divide, expected: null },
{ a: 10, b: [2], action: Action.Exponentiate, expected: null },
{ a: [10], b: 0, action: Action.Exponentiate, 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 count result properly', (testCase) => {
expect(
simpleCalculator({
a: testCase.a,
b: testCase.b,
action: testCase.action,
}),
).toBe(testCase.expected);
});
// Consider to use Jest table tests API to test all cases above
});
30 changes: 23 additions & 7 deletions src/03-error-handling-async/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,46 @@
// 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 = 42;
const result = await resolveValue(value);
expect(result).toBe(value);
});
});

describe('throwError', () => {
test('should throw error with provided message', () => {
// Write your test here
const messageText = 'Message';
expect(() => {
throwError(messageText);
}).toThrow(messageText);
});

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);
});
});

describe('rejectCustomError', () => {
test('should reject custom error', async () => {
// Write your test here
await expect(async () => {
await rejectCustomError();
}).rejects.toThrow(MyAwesomeError);
});
});
71 changes: 61 additions & 10 deletions src/04-test-class/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,95 @@
// Uncomment the code below and write your tests
// import { getBankAccount } from '.';

import { BankAccount, getBankAccount } from './index';

describe('BankAccount', () => {
let account: BankAccount;

test('should create account with initial balance', () => {
// Write your test here
const initial = 42;
account = getBankAccount(initial);
expect(account.getBalance()).toEqual(initial);
});

test('should throw InsufficientFundsError error when withdrawing more than balance', () => {
// Write your test here
expect(() => {
account.withdraw(account.getBalance() * 10);
}).toThrow();
});

test('should throw error when transferring more than balance', () => {
// Write your test here
const receiver = getBankAccount(0);
expect(() => {
account.transfer(account.getBalance() * 10, receiver);
}).toThrow();
});

test('should throw error when transferring to the same account', () => {
// Write your test here
expect(() => {
account.transfer(account.getBalance() * 10, account);
}).toThrow();
});

test('should deposit money', () => {
// Write your test here
const currentBalance = account.getBalance();
const deposit = 10;
account.deposit(deposit);
expect(account.getBalance()).toEqual(currentBalance + deposit);
});

test('should withdraw money', () => {
// Write your test here
const currentBalance = account.getBalance();
const sum = 10;
account.withdraw(sum);
expect(account.getBalance()).toEqual(currentBalance - sum);
});

test('should transfer money', () => {
// Write your test here
const receiverInitial = 0;
const receiver = getBankAccount(receiverInitial);
const currentBalance = account.getBalance();
const sum = 1;
account.transfer(sum, receiver);
expect(account.getBalance()).toEqual(currentBalance - sum);
expect(receiver.getBalance()).toEqual(receiverInitial + sum);
});

test('fetchBalance should return number in case if request did not failed', async () => {
// Write your tests here
const res = await account.fetchBalance();
const typeValid = typeof res === 'object' || typeof res === 'number';
expect(typeValid).toBeTruthy();
});

test('should set new balance if fetchBalance returned number', async () => {
// Write your tests here
const initial = -42;
const testAccount = getBankAccount(initial);
let errorFlag = false;
try {
await testAccount.synchronizeBalance();
} catch (error) {
errorFlag = true;
}

const result =
(errorFlag && testAccount.getBalance() === initial) ||
(!errorFlag && testAccount.getBalance() !== initial);
expect(result).toBeTruthy();
});

test('should throw SynchronizationFailedError if fetchBalance returned null', async () => {
// Write your tests here
const initial = -42;
const testAccount = getBankAccount(initial);
let errorFlag = false;
try {
await testAccount.synchronizeBalance();
} catch (error) {
errorFlag = true;
}

const result =
(errorFlag && testAccount.getBalance() === initial) ||
(!errorFlag && testAccount.getBalance() !== initial);
expect(result).toBeTruthy();
});
});
27 changes: 22 additions & 5 deletions src/05-partial-mocking/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
// Uncomment the code below and write your tests
// import { mockOne, mockTwo, mockThree, unmockedFunction } from './index';
import { mockOne, mockThree, mockTwo, unmockedFunction } from './index';

function nope(): void {
return undefined;
}

jest.mock('./index', () => {
// const originalModule = jest.requireActual<typeof import('./index')>('./index');
const originalModule =
jest.requireActual<typeof import('./index')>('./index');
return {
...originalModule,
mockOne: nope,
mockTwo: nope,
mockThree: nope,
};
});

describe('partial mocking', () => {
Expand All @@ -11,10 +21,17 @@ describe('partial mocking', () => {
});

test('mockOne, mockTwo, mockThree should not log into console', () => {
// Write your test here
const logSpy = jest.spyOn(global.console, 'log');
mockOne();
mockTwo();
mockThree();
expect(logSpy).toHaveBeenCalledTimes(0);
expect(logSpy.mock.calls).toHaveLength(0);
});

test('unmockedFunction should log into console', () => {
// Write your test here
const logSpy = jest.spyOn(global.console, 'log');
unmockedFunction();
expect(logSpy).toHaveBeenCalledTimes(1);
});
});
Loading