๐Ÿ–ฅFrontEnd

jest ๊ธฐ๋ณธ ๊ณต๋ถ€

hellohailie 2023. 4. 28. 18:51

jest๋Š” ํŽ˜์ด์Šค๋ถ์—์„œ ๋งŒ๋“  ํ…Œ์ŠคํŠธํŒ… ๋„๊ตฌ์ด๋‹ค. 

zero-config ์ฒ ํ•™์„ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์„œ ๋ณ„๋„์˜ ์„ค์ •์—†์ด ๋น ๋ฅด๊ฒŒ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด ์žฅ์ ์ด๋‹ค. 

 

jest ์„ค์น˜ํ•˜๊ธฐ

npm install jest --save-dev

 

๊ฐœ๋ฐœ ํ• ๋•Œ๋งŒ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋’ค์— --save-dev๋ฅผ ๋ถ™์ธ๋‹ค. 

 

package.json์—์„œ ์ˆ˜์ •ํ•œ๋‹ค. 

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },

๐Ÿ‘‡

  "scripts": {
    "test": "jest"
  },

 

 

~.test.js๋กœ ๋๋‚˜๊ฑฐ๋‚˜ __test__ํด๋”์— ๋‹ด๊ฒจ์žˆ์œผ๋ฉด ์ž๋™์œผ๋กœ ํ…Œ์ŠคํŠธ ํŒŒ์ผ๋กœ ์ธ์‹ํ•œ๋‹ค!

ํŠน์ • ํŒŒ์ผ๋งŒ ํ…Œ์ŠคํŒ…ํ•˜๊ณ  ์‹ถ์œผ๋ฉด npm test ๋’ค์— ํŒŒ์ผ๋ช…์ด๋‚˜ ํŠน์ • ๊ฒฝ๋กœ๋ฅผ ๋ถ™์ด๋ฉด ๋œ๋‹ค. 

 

 

//fn.js

const fn = {
  add: (num1, num2) => {
    num1 + num2;
  },
};

module.exports = fn;
// package file์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก export ํ•ด์ค€๋‹ค.
//fn.test.js

const fn = require("./fn");

test("1์€ 1์ด์•ผ", () => {
  expect(1).toBe(1);
});

expect ๋’ค์— ๊ฒ€์ฆํ•  ๋Œ€์ƒ์„ ๋„ฃ์–ด์ฃผ๊ณ  toBe์— ๊ธฐ๋Œ€๋˜๋Š” ๊ฐ’์„ ๋„ฃ๋Š”๋‹ค. 

 

 

๋ถ€์ •์— ๋Œ€ํ•œ ํ…Œ์ŠคํŠธ

test("3 ๋”ํ•˜๊ธฐ 3์€ 5๊ฐ€ ์•„๋‹ˆ์•ผ!", () => {
  expect(fn.add(3, 3)).not.toBe(5);
});

 

 

toBe์—์„œ ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ Matcher๋ผ๊ณ  ํ•œ๋‹ค. 

 

Matcher์—๋Š” toEqual๋„ ์žˆ๋‹ค.

 

๊ฐ์ฒด๋‚˜ ๋ฐฐ์—ด์€ ์žฌ๊ท€์ ์œผ๋กœ ๋Œ๋ฉด์„œ ๊ฐ’์„ ํ™•์ธํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— 'toEqual'์„ ์‚ฌ์šฉํ•ด์ค˜์•ผํ•œ๋‹ค.

 

Matcher ์ข…๋ฅ˜

- toBeNull()

- toBeTruthy()

- toBeFalsy()

- toBeUmdefined

- toBeDefined

 

- toBeGreaterThan ํฌ๋‹ค

- toBeGreaterThanEqual ํฌ๊ฑฐ๋‚˜ ๊ฐ™๋‹ค

- toBeLessThan ์ž‘๋‹ค

- toBeLessThanOrEqual ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค

- toBeCloseTo() ๊ทผ์‚ฌ๊ฐ’์ธ์ง€ ํŒ๋ณ„

 

- toMatch(์ •๊ทœํ‘œํ˜„์‹) ํŠน์ • ๋ฌธ์ž์—ด์ด ์žˆ๋Š”์ง€ ํŒ๋ณ„

 

- toContain()

๋ฐฐ์—ด์—์„œ ํŠน์ • ์š”์†Œ๊ฐ€ ์žˆ๋Š”์ง€ ํŒ๋ณ„

 

- ํŠน์ • ์—๋Ÿฌ์ธ์ง€ ํ™•์ธ

const fn = {
  add: (num1, num2) => num1 + num2,
  //๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๋Š” ํ•จ์ˆ˜
  makeUser: (name, age) => ({ name, age }),
  //ํŠน์ • ์—๋Ÿฌ์ธ์ง€ ํ™•์ธํ•˜๋Š” ํ•จ์ˆ˜
  throwErr: () => {
    throw new Error("xx");
  },
};


module.exports = fn;
// package file์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก export ํ•ด์ค€๋‹ค.



test("์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋‚˜์š”?", () => {
  expect(() => fn.throwErr()).toThrow("์—๋Ÿฌ๋ฐœ์ƒ!!!");
});

 

 

๋” ๋งŽ์€ Matcher๋Š” ๊ณต์‹ ํ™ˆํŽ˜์ด์ง€์—์„œ ํ™•์ธํ•˜๊ธฐ!

https://jestjs.io/docs/using-matchers

 

Using Matchers · Jest

Jest uses "matchers" to let you test values in different ways. This document will introduce some commonly used matchers. For the full list, see the expect API doc.

jestjs.io

 

 


๋น„๋™๊ธฐ jest 

 

const fn = {
  add: (num1, num2) => num1 + num2,
  //๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๋Š” ํ•จ์ˆ˜
  makeUser: (name, age) => ({ name, age }),
  getName: (callback) => {
    const name = "Hailie";
    setTimeout(() => {
      callback(name);
      // throw new Error("error");
    }, 3000);
  },
  getAge: () => {
    const age = 30;
    return new Promise((res, rej) => {
      setTimeout(() => {
        res(age);
        // rej("err");
      }, 3000);
    });
  },
};

module.exports = fn;
// package file์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก export ํ•ด์ค€๋‹ค.
test("3์ดˆ ํ›„์— ๋ฐ›๋Š” ์ด๋ฆ„์€ Hailie", (done) => {
  function callback(name) {
    expect(name).toBe("Hailie");
    done();
  }
  fn.getName(callback);
});

// ๋งŒ์•ฝ api์˜ ์—๋Ÿฌ๋ฅผ ๊ฐ์ง€ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด try catch๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.
test("3์ดˆ ํ›„์— ๋ฐ›๋Š” ์ด๋ฆ„์€ Hailie", (done) => {
  function callback(name) {
    try {
      expect(name).toBe("Hailie");
      done();
    } catch (error) {
      done();
    }
  }
  fn.getName(callback);
});

// promise๋Š” done์„ ์•ˆ๋„˜๊ฒจ์ค˜๋„ ๋œ๋‹ค. ๋Œ€์‹  return์„ ํ•ด์ค˜์•ผํ•œ๋‹ค.
// test("3์ดˆ ํ›„์— ๋ฐ›์€ ๋‚˜์ด๋Š” 30", () => {
//   return fn.getAge().then((age) => {
//     expect(age).toBe(30);
//   });
// });

//๊ฐ„๋‹จํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๊ธฐ resolves, rejects
test("3์ดˆ ํ›„์— ๋ฐ›์€ ๋‚˜์ด๋Š” 30", () => {
  return expect(fn.getAge()).resolves.toBe(30);
});
//๊ฐ„๋‹จํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๊ธฐ resolves, rejects
test("3์ดˆ ํ›„์— ๋ฐ›์€ ๋‚˜์ด๋Š” 30", async () => {
  // const age = await fn.getAge();
  // expect(age).toBe(30);
  await expect(fn.getAge()).resolves.toBe(30);
});

 

ํ…Œ์ŠคํŠธ ์ง์ „์— ์ƒ์ˆ˜ ์ดˆ๊ธฐํ™” ํ•˜๊ธฐ (๋ชจ๋“  ํ…Œ์ŠคํŠธ ์ง์ „์— ์‹คํ–‰๋จ)

beforeEach(() => {
  num = 0;
});

ํ…Œ์ŠคํŠธ ์งํ›„์— ์ƒ์ˆ˜ ์ดˆ๊ธฐํ™” ํ•˜๊ธฐ (๋ชจ๋“  ํ…Œ์ŠคํŠธ ์งํ›„์— ์‹คํ–‰๋จ)

afterEach(() => {
  num = 0;
});

'๐Ÿ–ฅFrontEnd' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

jest, beforeAll, afterAll  (0) 2023.05.01