Jest

ํ…Œ์ŠคํŠธ ํ”„๋ ˆ์ž„์›Œํฌ Jest ๋ฉ”์†Œ๋“œ ์ •๋ฆฌ

Rainbow๐ŸŒˆCoder 2022. 10. 31. 16:20
728x90

 

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

728x90