npm init --yes
npm i jest --global
jest --init
npm i --save-dev jest
npm i @types/jest
jest --coverage : ์ปค๋ฒ๋ฆฌ์ง ํ์ธ
"scripts": {
"test": "jest --watchAll"
},
git init
.gitignore์ 'node_modules/*' ์ถ๊ฐ
git add .
git commit
"scripts": {
"test": "jest --watch" //
},
- ์์ ) calculator.js
class Calculator { constructor() { this.value = 0; } set(num) { this.value = num; } clear() { this.value = 0; } add(num) { const sum = this.value + num; if (sum > 100) { throw new Error('Value can not be greater than 100'); } this.value = sum; } subtract(num) { this.value = this.value - num; } multiply(num) { this.value = this.value * num; } divide(num) { this.value = this.value / num; } } module.exports = Calculator;
describe('Calculator', () => {
let cal;
// ๊ฐ๊ฐ์ ํ
์คํธ๊ฐ ์คํ๋๊ธฐ ์ ์ ์๋ก์ด object๋ฅผ ์์ฑ
beforeEach(() => {
cal = new Calculator();
});
it('inits with 0', () => {
expect(cal.value).toBe(0);
});
it('subtract', () => {
cal.set(1);
cal.subtract(2);
expect(cal.value).toBe(-1);
});
describe('divides', () => {
it('0 divide 0', () => {
cal.divide(0);
expect(cal.value).toBe(NaN);
});
it('1 divide 0', () => {
cal.set(1);
cal.divide(0);
expect(cal.value).toBe(Infinity);
});
});
});
it('add should throw an error if value is greater than 100', () => {
expect(() => {
cal.add(101);
}).toThrow('Value can not be greater than 100');
});
const fetchProduct = require('../async.js');
describe('Asnyc', () => {
/* done ๋ฐฉ์ */
it('aysnc-done', (done) => {
fetchProduct().then((item) => {
expect(item).toEqual({ item: 'Mike', price: 200 });
done(); //=> jest๊ฐ ๋๋๋ ์์ ์ง์ (5์ด ์ ๋ ๊ธฐ๋ค๋ฆฌ๊ธฐ ๋๋ฌธ์ ํ
์คํธ ์ํ์ด ๋๋ฆผ)
});
});
/* async return ๋ฐฉ์ */
it('aysnc-return', () => {
return fetchProduct().then((item) => {
expect(item).toEqual({ item: 'Mike', price: 200 });
});
});
it('aysnc-return-error', () => {
return fetchProduct().catch((item) => {
expect(item).toEqual('network error');
});
});
/* async await ๋ฐฉ์ */
it('aysnc-await', async () => {
const product = await fetchProduct();
expect(product).toEqual({ item: 'Mike', price: 200 });
});
it('aysnc-await-error', async () => {
try {
await fetchProduct('error');
} catch (error) {
expect(error).toBe('network error');
}
});
/* async resolves, rejects ๋ฐฉ์ */
it('aysnc-resolves', () => {
return expect(fetchProduct()).resolves.toEqual({
item: 'Mike',
price: 200,
});
});
it('aysnc-rejects', () => {
return expect(fetchProduct('error')).rejects.toBe('network error');
});
});
mock ํจ์๋ฅผ ํตํด ์ค์ ํจ์๋ฅผ ๊ตฌํํ์ง ์๊ณ , ๊ฐ๋จํ๊ฒ test ์งํ ๊ฐ๋ฅํ๋ค.
function check(predicate, onSuccess, onFail) {
if (predicate()) {
onSuccess('yes');
} else {
onFail('no');
}
}
module.exports = check;
const check = require('../check.js');
describe('check', () => {
let onSuccess;
let onFail;
beforeEach(() => {
onSuccess = jest.fn(); //-> ๊ฐ์ง ํจ์ ์์ฑ
onFail = jest.fn(); //-> ๊ฐ์ง ํจ์ ์์ฑ
});
it('should call onSuccess when predicate is true', () => {
check(() => true, onSuccess, onFail);
// expect(onSuccess.mock.calls.length).toBe(1);
// expect(onSuccess.mock.calls[0][0]).toBe(1);
// expect(onFail.mock.calls.length).toBe(0);
expect(onSuccess).toHaveBeenCalledTimes(1);
expect(onSuccess).toHaveBeenCalledWith('yes');
expect(onFail).toHaveBeenCalledTimes(0);
});
it('should call onFail when predicate is false', () => {
check(() => false, onSuccess, onFail);
expect(onFail).toHaveBeenCalledTimes(1);
expect(onFail).toHaveBeenCalledWith('no');
expect(onSuccess).toHaveBeenCalledTimes(0);
});
});
class ProductClient {
fetchItems() {
return fetch('http://example.com/login/id+password').then((res) =>
res.json()
);
}
}
module.exports = ProductClient;
const ProductClient = require('./product_client');
class ProductService {
constructor() {
this.ProductClient = new ProductClient();
}
fetchAvailableItems() {
return this.ProductClient.fetchItems().then((items) =>
items.filter((item) => item.available)
);
}
}
module.exports = ProductService;
const ProductService = require('../product_service_no_di.js');
const ProductClient = require('../product_client.js');
// product_client์ mock์ผ๋ก ์ฌ์ฉํ๋ค๊ณ ๋ช
์
jest.mock('../product_client');
describe('product service', () => {
// fetchItems์ ๋ํด์ mock ํจ์ ์์ฑ
const fetchItems = jest.fn(async () => [
{ item: '๐ฅ', available: true },
{ item: '๐', available: false },
]);
//product_client์ fetchItems ํจ์ ์ฐ๊ฒฐ
ProductClient.mockImplementation(() => {
return {
fetchItems,
};
});
let productService;
beforeEach(() => {
productService = new ProductService();
//=> network ์ํ์ ์์กดํ๋ ์ฝ๋๋ ์ข์ง ์๋ค.
// ์์กด์ฑ์ ์ ๊ฑฐํ๊ธฐ ์ํด product_client์ mockํ๋ค.
});
it('should filter out only available items', async () => {
const items = await productService.fetchAvailableItems();
expect(items.length).toBe(1);
expect(items).toEqual([{ item: '๐ฅ', available: true }]);
});
});