"Right-BICEP"์ ํจ๊ณผ์ ์ธ ๋จ์ ํ ์คํธ๋ฅผ ์ํ ๊ธฐ๋ณธ ์์น๋ค์ ๋ํ๋ด๋ ์ฝ์ด์ด๋ฉฐ, ํ ์คํธ๊ฐ ์ค์ ์ ๋์ด์ผ ํ๋ ํต์ฌ ์์ญ๋ค์ ๊ฐ์กฐํ๋ค.
ํ ์คํธ๊ฐ ์ค์ ๋ก ์ค์ํ ์ฌํญ์ ๊ฒ์ฆํ๊ณ ์๋์ง ํ์ธํด์ผ ํ๋ค. ์๋ฅผ ๋ค์ด, ์ฌ์ฉ์์ ์ด๋ฉ์ผ ์ฃผ์ ํ์์ด ์ฌ๋ฐ๋ฅธ์ง ๊ฒ์ฆํ๋ ํ ์คํธ์ ๊ฒฝ์ฐ, ๋ค์ํ ํ์์ ์ด๋ฉ์ผ ์ฃผ์์ ๋ํ ํ ์คํธ๋ฅผ ์งํํ์ฌ, ์ค์ ๋ก ์ ํจํ ํ์์ ์ด๋ฉ์ผ ์ฃผ์๋ง์ ํ์ฉํ๋๋ก ํด์ผ ํ๋ค.
const { validateEmail } = require('./validation');
describe('Email Validation Tests', () => {
test('should validate email address correctly', () => {
expect(validateEmail('test@example.com')).toBeTruthy();
expect(validateEmail('invalid-email')).toBeFalsy();
});
});
// validation.js
function validateEmail(email) {
const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return re.test(email);
}
module.exports = { validateEmail };
validateEmail
ํจ์์ ๋ํ ํ
์คํธ๋ฅผ ๋ณด์ฌ์ค๋ค. ์ ํจํ ์ด๋ฉ์ผ ํ์ ('test@example.com')๊ณผ ์ ํจํ์ง ์์ ์ด๋ฉ์ผ ํ์ ('invalid-email')์ ๋ํ ๊ฒ์ฆ์ ์ํํจ์ผ๋ก์จ, ์ด๋ฉ์ผ ์ฃผ์์ ์ ํํ ํ์์ ์ฌ๋ฐ๋ฅด๊ฒ ๊ฒ์ฆํ๋์ง ํ์ธํ๋ค. ์ด์ฒ๋ผ ์ค์ํ ์ฌํญ์ ์ ํํ ๊ฒ์ฆํ๋ ํ
์คํธ๋ ์ํํธ์จ์ด์ ์ ๋ขฐ์ฑ์ ๋์ด๋ ๋ฐ ๊ธฐ์ฌํ๋ค.๊ฒฝ๊ณ ์กฐ๊ฑด ํ ์คํธ๋ ๋ฐฐ์ด, ๋ฆฌ์คํธ, ๋ฌธ์์ด ๋ฑ์ ๊ฒฝ๊ณ์์ ๋ฐ์ํ ์ ์๋ ํน๋ณํ ์ํฉ๋ค์ ๋ค๋ฃฌ๋ค. ์๋ฅผ ๋ค์ด, ๋ฐฐ์ด์ ์ฒซ ๋ฒ์งธ๋ ๋ง์ง๋ง ์์๋ฅผ ์ฒ๋ฆฌํ๋ ๋ก์ง, ๋น ๋ฐฐ์ด์ด๋ ๋ฌธ์์ด์ ๋ค๋ฃจ๋ ๋ก์ง ๋ฑ์ด ์ด์ ํด๋นํ๋ค. ์ด๋ฌํ ๊ฒฝ๊ณ ์กฐ๊ฑด์์๋ ์ฝ๋๊ฐ ์ฌ๋ฐ๋ฅด๊ฒ ์๋ํ๋์ง ํ์ธํ๋ ๊ฒ์ด ์ค์ํ๋ค.
const { getLastItem } = require('./arrayUtil');
describe('Array Boundary Conditions Tests', () => {
test('should handle empty array', () => {
expect(getLastItem([])).toBeNull();
});
test('should return last item for non-empty array', () => {
expect(getLastItem([1, 2, 3])).toBe(3);
});
});
// arrayUtil.js
function getLastItem(array) {
if (array.length === 0) return null;
return array[array.length - 1];
}
module.exports = { getLastItem };
getLastItem
ํจ์๋ ์ฃผ์ด์ง ๋ฐฐ์ด์ ๋ง์ง๋ง ์์๋ฅผ ๋ฐํํ๋๋ฐ, ๋น ๋ฐฐ์ด์ผ ๊ฒฝ์ฐ null
์ ๋ฐํํด์ผ ํ๋ค. Jest๋ฅผ ์ฌ์ฉํ์ฌ ๋น ๋ฐฐ์ด๊ณผ ๋น์ด ์์ง ์์ ๋ฐฐ์ด ๋ชจ๋๋ฅผ ํ
์คํธํจ์ผ๋ก์จ, ์ด ํจ์๊ฐ ๋ฐฐ์ด์ ๊ฒฝ๊ณ ์กฐ๊ฑด์์๋ ์ฌ๋ฐ๋ฅด๊ฒ ์๋ํ๋์ง ๊ฒ์ฆํ๋ค. ๊ฒฝ๊ณ ์กฐ๊ฑด์ ํ
์คํธํจ์ผ๋ก์จ ์ํํธ์จ์ด์ ๊ฒฌ๊ณ ํจ์ ๋์ผ ์ ์๋ค.์ญ ๊ด๊ณ ํ ์คํธ๋ ์ํํธ์จ์ด์ ๋ค๋ฅธ ๋ถ๋ถ์ด ๋ฐ๋ ๋๋ ์ํธ ๋ณด์์ ์ธ ๊ธฐ๋ฅ์ ์ํํ๋์ง ํ์ธํ๋ค. ์๋ฅผ ๋ค์ด, ์ถ๊ฐ(Add) ๊ธฐ๋ฅ์ ํ ์คํธํ ํ, ์ ๊ฑฐ(Remove) ๊ธฐ๋ฅ์ ํ ์คํธํ์ฌ ๋ ๊ธฐ๋ฅ์ด ์๋ก ์ฌ๋ฐ๋ฅด๊ฒ ๋์ํ๋์ง ํ์ธํ๋ ๊ฒฝ์ฐ๊ฐ ์ด์ ํด๋นํ๋ค.
const { addItem, removeItem, getItems } = require('./listManager');
describe('List Inverse Relationship Tests', () => {
test('adding and then removing an item should result in an empty list', () => {
addItem('apple');
removeItem('apple');
expect(getItems()).toEqual([]);
});
});
// listManager.js
let items = [];
function addItem(item) {
items.push(item);
}
function removeItem(item) {
const index = items.indexOf(item);
if (index > -1) {
items.splice(index, 1);
}
}
function getItems() {
return items;
}
module.exports = { addItem, removeItem, getItems };
addItem
์ผ๋ก ํญ๋ชฉ์ ์ถ๊ฐํ๊ณ , removeItem
์ผ๋ก ๊ฐ์ ํญ๋ชฉ์ ์ ๊ฑฐํ ํ, getItems
๋ฅผ ์ฌ์ฉํ์ฌ ์ต์ข
๋ชฉ๋ก์ด ๋น์ด ์๋์ง ๊ฒ์ฆํ๋ค. ์ด๋ฐ ์ญ ๊ด๊ณ ํ
์คํธ๋ ์ํํธ์จ์ด์ ์๋ก ๋ค๋ฅธ ๊ธฐ๋ฅ๋ค์ด ์ํธ ๋ณด์์ ์ผ๋ก ์ฌ๋ฐ๋ฅด๊ฒ ์๋ํ๋์ง ํ์ธํ๋ ๋ฐ ์ ์ฉํ๋ค.๊ต์ฐจ ๊ฒ์ฆ์ ์๋ก ๋ค๋ฅธ ๋ฐฉ์์ผ๋ก ๊ณ์ฐํ๊ฑฐ๋ ํ์ธํ๋ ๊ฒ์ ์๋ฏธํ๋ค. ์๋ฅผ ๋ค์ด, ๋ ๊ฐ์ง ๋ค๋ฅธ ์๊ณ ๋ฆฌ์ฆ์ผ๋ก ๋์ผํ ๊ฒฐ๊ณผ๋ฅผ ๊ณ์ฐํ์ฌ ๋ ๊ฒฐ๊ณผ๋ฅผ ๋น๊ตํ๋ ๊ฒ์ด๋ค. ์ด ๋ฐฉ์์ผ๋ก, ์๊ณ ๋ฆฌ์ฆ์ ์ ํ์ฑ์ ๋ณด๋ค ํ์คํ๊ฒ ๊ฒ์ฆํ ์ ์๋ค.
const { sumDirect, sumIterative } = require('./sumCalculators');
describe('Sum Calculation Cross-Check Tests', () => {
test('both sum calculation methods should return the same result', () => {
const numbers = [1, 2, 3, 4, 5];
expect(sumDirect(numbers)).toEqual(sumIterative(numbers));
});
});
// sumCalculators.js
function sumDirect(numbers) {
return numbers.reduce((a, b) => a + b, 0);
}
function sumIterative(numbers) {
let sum = 0;
for (let num of numbers) {
sum += num;
}
return sum;
}
module.exports = { sumDirect, sumIterative };
sumDirect
๋ฉ์๋๊ฐ reduce ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐฐ์ด์ ํฉ์ ๊ณ์ฐํ๊ณ , sumIterative
๋ฉ์๋๋ ๋ฐ๋ณต๋ฌธ์ ์ฌ์ฉํ์ฌ ๋์ผํ ์์
์ ์ํํ๋ค. ๋ ๋ฉ์๋๊ฐ ๋์ผํ ์
๋ ฅ์ ๋ํด ๋์ผํ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํ๋์ง Jest๋ก ํ
์คํธํ์ฌ ๊ต์ฐจ ๊ฒ์ฆํ๋ค.์ค๋ฅ ์กฐ๊ฑด ํ ์คํธ๋ ์ํํธ์จ์ด๊ฐ ์์ ๋ฐ ์์์น ๋ชปํ ์ค๋ฅ ์ํฉ์์ ์ฌ๋ฐ๋ฅด๊ฒ ๋์ํ๋์ง ํ์ธํ๋ค. ์๋ฅผ ๋ค์ด, ์๋ชป๋ ์ ๋ ฅ๊ฐ, ์์คํ ๋ฆฌ์์ค ๋ถ์กฑ, ์์กด ์๋น์ค์ ์คํจ ๋ฑ ๋ค์ํ ์ค๋ฅ ์ํฉ์ ์๋ฎฌ๋ ์ด์ ํ๊ณ ์ ์ ํ ์ค๋ฅ ์ฒ๋ฆฌ๊ฐ ์ด๋ฃจ์ด์ง๋์ง ๊ฒ์ฌํ๋ค.
const { processData } = require('./dataProcessor');
describe('Error Conditions Test', () => {
test('should handle invalid input gracefully', () => {
expect(() => processData(null)).toThrow('Invalid input');
expect(() => processData({})).toThrow('Invalid data structure');
});
test('should handle external service failure', async () => {
// ์ฌ๊ธฐ์๋ ์ธ๋ถ ์๋น์ค ์คํจ๋ฅผ ์๋ฎฌ๋ ์ด์
ํ๋ ๋ฐฉ๋ฒ์ด ํฌํจ๋ ์ ์์ต๋๋ค.
// ์๋ฅผ ๋ค์ด, Mock์ด๋ Spy๋ฅผ ์ฌ์ฉํ์ฌ ๋คํธ์ํฌ ์ค๋ฅ๋ฅผ ์๋ฎฌ๋ ์ด์
ํ ์ ์๋ค.
// ํด๋น ๋ถ๋ถ์ ํ๋ก์ ํธ์ ํ
์คํธ ํ๊ฒฝ์ ๋ฐ๋ผ ๋ค๋ฅด๊ฒ ๊ตฌํ๋ ์ ์๋ค.
});
});
// dataProcessor.js
function processData(data) {
if (!data) {
throw new Error('Invalid input');
}
// ... ๋ฐ์ดํฐ ์ฒ๋ฆฌ ๋ก์ง ...
}
module.exports = { processData };
processData
ํจ์๊ฐ ์ ํจํ์ง ์๊ฑฐ๋ ์์์น ๋ชปํ ์
๋ ฅ๊ฐ์ ๋ฐ์์ ๋ ์ ์ ํ ์ค๋ฅ ๋ฉ์์ง๋ฅผ ๋ฐ์์ํค๋์ง ํ
์คํธํ๋ค. ์ด๋ฌํ ์ข
๋ฅ์ ํ
์คํธ๋ ์์ธ ์ํฉ์์๋ ์์คํ
์ด ๊ฒฌ๊ณ ํ๊ฒ ๋์ํ๋์ง ํ์ธํ๋ ๋ฐ ์ค์ํ๋ค.์ฑ๋ฅ ํน์ฑ ํ ์คํธ๋ ์ํํธ์จ์ด์ ์ฒ๋ฆฌ ์๋, ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋, ๋์คํฌ ๊ณต๊ฐ ์ฌ์ฉ ๋ฑ์ ์ฑ๋ฅ ์งํ๋ฅผ ํ๊ฐํ๋ค. ์๋ฅผ ๋ค์ด, ์น ์ ํ๋ฆฌ์ผ์ด์ ์ API ์๋ต ์๊ฐ์ ์ธก์ ํ๊ฑฐ๋, ๋์ฉ๋ ๋ฐ์ดํฐ ์ฒ๋ฆฌ ์ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋์ ๋ถ์ํ ์ ์๋ค.
const { largeDataProcessor } = require('./processor');
describe('Performance Test', () => {
test('should process data within acceptable time', () => {
const largeData = generateLargeData(); // ๋์ฉ๋ ๋ฐ์ดํฐ ์์ฑ ํจ์
const startTime = performance.now();
largeDataProcessor(largeData);
const endTime = performance.now();
const processingTime = endTime - startTime;
expect(processingTime).toBeLessThan(1000); // 1000ms ์ด๋ด ์๋ฃ๋์ด์ผ ํจ
});
test('should not exceed memory limit', () => {
const largeData = generateLargeData();
const initialMemoryUsage = process.memoryUsage().heapUsed;
largeDataProcessor(largeData);
const finalMemoryUsage = process.memoryUsage().heapUsed;
expect(finalMemoryUsage - initialMemoryUsage).toBeLessThan(100000000); // 100MB ์ด๋ด๋ก ์ ํ
});
});
// processor.js
function largeDataProcessor(data) {
// ... ๋์ฉ๋ ๋ฐ์ดํฐ ์ฒ๋ฆฌ ๋ก์ง ...
}
module.exports = { largeDataProcessor };
largeDataProcessor
๊ฐ ์ ํด์ง ์๊ฐ ๋ด์ ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํ ์ ์์ผ๋ฉฐ, ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋์ด ํน์ ํ๊ณ๋ฅผ ๋์ง ์๋์ง ํ์ธํ๋ค. ์ด๋ฌํ ์ฑ๋ฅ ํ
์คํธ๋ ์์คํ
์ ํจ์จ์ฑ๊ณผ ํ์ฅ์ฑ์ ๊ฒ์ฆํ๋ ๋ฐ ์ค์ํ๋ค.