toBe
< fn.js >
const fn = {
add: (num1, num2) => num1 + num2,
}
module.exports = fn;
< fn.test.js >
const fn = require('./fn');
test('์ฒซ๋ฒ์งธ ํ
์คํธ์
๋๋ค!', () => {
expect(1).toBe(1);
});
test('์ด๊ฒ์ ํ์ดํ๋ค', () => {
expect('ํ์ดํ').toBe('ํ์ดํ');
});
//์ฑ๊ณต ์ผ์ด์ค
test('fn.add(2,3) = 5', () => {
expect(fn.add(2, 3)).toBe(5);
});
//์คํจ ์ผ์ด์ค
test('fn.add(2,3) =! 7', () => {
expect(fn.add(2, 3)).toBe(7);
});
toEqual๊ณผ toStrictEqual ์ฐจ์ด?
const fn = {
add: (num1, num2) => num1 + num2,
makeUser: (name, age) => ({ name, age }),
}
module.exports = fn;
const fn = require('./fn');
test('์ด๋ฆ๊ณผ ๋์ด๋ฅผ ์ ๋ฌ๋ฐ์์ ๊ฐ์ฒด ๋ฐํ', () => {
expect(fn.makeUser('Mike', 30)).toEqual({
name: 'Mike',
age: 30
});
});
test('์ด๋ฆ๊ณผ ๋์ด๋ฅผ ์ ๋ฌ๋ฐ์์ ๊ฐ์ฒด ๋ฐํ', () => {
expect(fn.makeUser('Mike', 30)).toStrictEqual({
name: 'Mike',
age: 30
});
});
โ ์ด๋ฆ๊ณผ ๋์ด๋ฅผ ์ ๋ฌ๋ฐ์์ ๊ฐ์ฒด ๋ฐํ (2 ms)
โ ์ด๋ฆ๊ณผ ๋์ด๋ฅผ ์ ๋ฌ๋ฐ์์ ๊ฐ์ฒด ๋ฐํ
์์ ๊ฐ์ ์ผ์ด์ค์์๋ ์ฐจ์ด๊ฐ ์์ด ๋ณด์ธ๋ค.
< ๊ทธ. ๋ฌ. ๋ >
const fn = {
add: (num1, num2) => num1 + num2,
makeUser: (name, age) => ({ name, age, gender: undefined }),
}
module.exports = fn;
gender๋ผ๋ ํค๋ฅผ ์ถ๊ฐํ๋ฉด ๊ฒฐ๊ณผ๋ ๋ฌ๋ผ์ง๋ค.
const fn = require('./fn');
test('์ด๋ฆ๊ณผ ๋์ด๋ฅผ ์ ๋ฌ๋ฐ์์ ๊ฐ์ฒด ๋ฐํ', () => {
expect(fn.makeUser('Mike', 30)).toEqual({
name: 'Mike',
age: 30
});
});
test('์ด๋ฆ๊ณผ ๋์ด๋ฅผ ์ ๋ฌ๋ฐ์์ ๊ฐ์ฒด ๋ฐํ', () => {
expect(fn.makeUser('Mike', 30)).toStrictEqual({
name: 'Mike',
age: 30
});
});
โ ์ด๋ฆ๊ณผ ๋์ด๋ฅผ ์ ๋ฌ๋ฐ์์ ๊ฐ์ฒด ๋ฐํ (2 ms)
โ ์ด๋ฆ๊ณผ ๋์ด๋ฅผ ์ ๋ฌ๋ฐ์์ ๊ฐ์ฒด ๋ฐํ (4 ms)
expect(received).toStrictEqual(expected) // deep equality
- Expected - 0
+ Received + 1
Object {
"age": 30,
+ "gender": undefined,
"name": "Mike",
}
10 |
11 | test('์ด๋ฆ๊ณผ ๋์ด๋ฅผ ์ ๋ฌ๋ฐ์์ ๊ฐ์ฒด ๋ฐํ', () => {
> 12 | expect(fn.makeUser('Mike', 30)).toStrictEqual({
| ^
13 | name: 'Mike',
14 | age: 30
15 | });
at Object.toStrictEqual (fn.test.js:12:34)
๊ฒฐ๊ณผ : toEqual ์ ํต๊ณผ toStrictEqual ๋ ํต๊ณผํ์ง ๋ชปํจ!
๋ฐ๋ผ์ ๋ณด๋ค ์๊ฒฉํ๊ฒ ๊ฒ์ฌํ๊ธฐ ์ํด์๋ toStrictEqual ๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข๋ค
// toBeNull // toBeUndefined // toBeDefined // toBeTruthy // toBeFalsy
const fn = require('./fn');
// toBeNull
// toBeUndefined
// toBeDefined
test("null์ null์
๋๊น?", () => {
expect(null).toBeNull();
});
// toBeTruthy
// toBeFalsy
test("-1 + 1 ์ ํ์ ๋ null์ธ์ง ์ฒดํฌ", () => {
expect(fn.add(1, -1)).toBeNull();
});
test("-1 + 1 ์ ํ์ ๋ false์ธ์ง ์ฒดํฌ", () => {
expect(fn.add(1, -1)).toBeFalsy();
});
test("๋จ์ ๋ฌธ์์ด(๋น์ด์์ง ์์ ๋ฌธ์์ด)์ ์
๋ ฅํ์ ๋, true์ธ์ง ์ฒดํฌ", () => {
expect(fn.add("hello", "world")).toBeTruthy();
});
test("๋จ์ ๋ฌธ์์ด(๋น์ด์์ง ์์ ๋ฌธ์์ด)์ ์
๋ ฅํ์ ๋, false์ธ์ง ์ฒดํฌ", () => {
expect(fn.add("hello", "world")).toBeFalsy();
});
// toBeGreaterThan ํฌ๋ค : ์ด๊ณผ // toBeGreaterThanOrEqual ํฌ๊ฑฐ๋ ๊ฐ๋ค : ์ด์ // toBeLessThan ์๋ค : ๋ฏธ๋ง // toBeLessThanOrEqual ์๊ฑฐ๋ ๊ฐ๋ค : ์ดํ
const fn = require('./fn');
// toBeGreaterThan ํฌ๋ค : ์ด๊ณผ
// toBeGreaterThanOrEqual ํฌ๊ฑฐ๋ ๊ฐ๋ค : ์ด์
// toBeLessThan ์๋ค : ๋ฏธ๋ง
// toBeLessThanOrEqual ์๊ฑฐ๋ ๊ฐ๋ค : ์ดํ
// toBe ์ ํํ๊ฒ ๊ทธ ๊ฐ (1) //๊ทธ๋ฌ๋ ๋ฌดํ์์์์ ๊ฑธ๋ฆผ
// toBeCloseTo
test("๋น๋ฐ๋ฒํธ 4์๋ฆฌ", () => {
const pw = '1234';
expect(pw.length).toBe(4);
});
test("0.1 ๋ํ๊ธฐ 0.2๋ 0.3", () => {
expect(fn.add(0.1, 0.2)).toBe(0.3);
});
test("0.1 ๋ํ๊ธฐ 0.2๋ 0.3", () => {
expect(fn.add(0.1, 0.2)).toBeCloseTo(0.3);
});
//๋ธ๋ํํฌ ์๋๋ก ๊ฐ์ง๊ณ ๋๊ธฐ^^
test("ID๋ 10์ ์ดํ์ฌ์ผ ํฉ๋๋ค.", () => {
const id = "BlackPink";
expect(id.length).toBeLessThanOrEqual(10);
});
test("ID๋ 10์ ์ด์์ด์ฌ์ผ ํฉ๋๋ค.", () => {
const id = "BlackPink";
expect(id.length).toBeGreaterThanOrEqual(10);
});
test("ID๋ 10์ ๋ฏธ๋ง์ด์ด์ผ ํฉ๋๋ค.", () => {
const id = "PurplePink";
expect(id.length).toBeLessThan(10);
});
test("ID๋ 10์ ์ด๊ณผ์ฌ์ผ ํฉ๋๋ค.", () => {
const id = "Black_Pink";
expect(id.length).toBeGreaterThan(10);
});
toMatch ๋ฌธ์์ด์ ํน์ ๋ฌธ์(์ ๊ท ํํ์ ํ์ฉ)๊ฐ ์๋์ง ์ฒดํฌ
const fn = require('./fn');
test("Hello World์ a๋ผ๋ ๊ธ์๊ฐ ์๋?", () => {
expect('Hello World').toMatch(/a/);
});
test("Hello World์ o ๋ผ๋ ๊ธ์๊ฐ ์๋?", () => {
expect('Hello World').toMatch(/o/);
});
toMatch์ ์ธ์๋ก ์ ๊ท ํํ์์ ๋ฃ์ด์ค๋ค.
๋์๋ฌธ์ ๊ตฌ๋ถ์ ์์ ์ฃผ๊ณ ์ถ๋ค๋ฉด, /O/i ์ด๋ ๊ฒ ๋ค์ i๋ฅผ ๋ถ์ฌ์ค๋ค
๊ฐ๋ น,
์ด๋ ๊ฒ ์์ฑํ๋ฉด ํ ์คํธ์ ํต๊ณผํ๋ค
const fn = require('./fn');
test("Hello World์ o ๋ผ๋ ๊ธ์๊ฐ ์๋?", () => {
expect('Hello World').toMatch(/O/i);
});
๊ทธ๋ฌ๋ ์ด๋ ๊ฒ ์์ฑํ๋ฉด ํ ์คํธ์ ํต๊ณผํ์ง ์๋๋ค
const fn = require('./fn');
test("Hello World์ o ๋ผ๋ ๊ธ์๊ฐ ์๋?", () => {
expect('Hello World').toMatch(/O/);
});
toContain ๋ฐฐ์ด์์ ํน์ ์์๊ฐ ์๋์ง ์ฒดํฌ
const fn = require('./fn');
//toContain
test("๊ณผ์ผ ๋ฆฌ์คํธ์ '์ฌ๊ณผ'๊ฐ ์๋?", () => {
const apple = '์ฌ๊ณผ';
const fruits = ['๋ฐฐ', '์ฌ๊ณผ', '๋งค์ค', '๋ฐ๋๋'];
expect(fruits).toContain(apple);
});