1. chai
mocha ๊ณตํ์์ chai ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ถ์ฒํ๋ฏ๋ก, ํด๋น ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ ์ฉํด์
mocha + chai assert style ์์ ๋ฅผ ์์ฑํด๋ณด๊ฒ ๋ค.
2. chai ์ค์น
Installation - Chai
Installation Chai is available for both node.js and the browser using any test framework you like. There are also a number of other tools that include Chai. Node.js Package is available through npm: Recommend adding it to package.json devDependencies using
www.chaijs.com
์๋ ๋ช ๋ น์ด๋ฅผ ํฐ๋ฏธ๋์ ์ณ์ฃผ๊ณ , npm install ์ณ์ฃผ๋ฉด
(1)ํฌ์คํ ๊ณผ ์ด์ด์ง๋ฏ๋ก mocha๋ ์ด๋ฏธ ๊น๋ฆฐ ์ํ
npm install --save-dev chai
package.json์ด ๋ค์๊ณผ ๊ฐ์ด ๋ณ๊ฒฝ๋๋ค
{
"name": "mochatestingpj",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"chai": "^4.3.6",
"mocha": "^10.1.0"
}
}
3. chai assert style
Assertion Styles - Chai
Assertion Styles This section of the guide introduces you to the three different assertion styles that you may use in your testing environment. Once you have made your selection, it is recommended that you look at the API Documentation for your selected st
www.chaijs.com
chai ๊ณตํ ์์ ๋ฅผ ๋ณด๊ธฐ๋ก ํ๋ค !
์ฐธ๊ณ ๋ก, chai assert ์คํ์ผ์ ์ฌ์ฉํ๋ฉด ๋ช ๋ น๋ฌธ์ ๋ง์ง๋ง ๋งค๊ฐ๋ณ์๋ก ์ ํ์ ๋ฉ์์ง๋ฅผ ํฌํจํ ์ ์๋๋ฐ, ์ด๋ ์ด์ค์ ์ด ํต๊ณผํ์ง ๋ชปํ ๊ฒฝ์ฐ ์ค๋ฅ ๋ฉ์์ง๋ก ๋ณด์ฌ์ค ์ ์๋ค.
var assert = require('chai').assert
, foo = 'bar'
, beverages = { tea: ['chai', 'matcha', 'oolong'] };
assert.typeOf(foo, 'string'); // without optional message
assert.typeOf(foo, 'string', 'foo is a string'); // with optional message
assert.equal(foo, 'bar', 'foo equal `bar`');
assert.lengthOf(foo, 3, 'foo`s value has a length of 3');
assert.lengthOf(beverages.tea, 3, 'beverages has 3 types of tea');
(1) typeOf
![](https://blog.kakaocdn.net/dn/IA6v3/btrP3Codl5x/Di4CbEQyDkSLXwyGRFGlXK/img.png)
mocha ์ chia assertion style ์ค ํ๋์ธ typeOf์ ์จ๋ณธ ์์
-1- ๋๊ฐ๋ pass ํ๋๋ fail ํ๋ฉด์ ์๋ฌ๋ฉ์์ง ๋์ค๊ฒ ์์ฑํ ์์
var assert = require('chai').assert;
var foo = '์ด๋ฌ๋ผ์ฐธ๊นจ'
describe('chai assert', function () {
describe('#assertion typeOf', function () {
it('6์ number', function () {
assert.typeOf(6, 'number');
});
it('์ด๋ ค๋ผ ์ฐธ๊นจ๋ string', function () {
assert.typeOf('์ด๋ ค๋ผ ์ฐธ๊นจ', 'string');
});
it('์ด๋ ค๋ผ ์ฐธ๊นจ๋ string', function () {
assert.typeOf('์ด๋ ค๋ผ ์ฐธ๊นจ', 'number', '์ด๋ ค๋ผ ์ฐธ๊นจ๋ string');
});
});
});
![](https://blog.kakaocdn.net/dn/XrrBu/btrP33eDP3J/CGXWmksKtTVS6bG1x0N7IK/img.png)
(2) equal
mocha ์ chia assertion style ์ค ํ๋์ธ equal์ ์จ๋ณธ ์์
-1- ๋ ๋ค fail ๋์ค๊ฒ ์์ฑ
var assert = require('chai').assert;
var foo = '์ด๋ฌ๋ผ์ฐธ๊บ '
describe('chai assert', function () {
describe('#equal', function () {
it('์ฃผ๋ฌธ์ ์ด๋ ค๋ผ ์ฐธ๊นจ', function () {
assert.equal(foo, '์ด๋ฌ๋ผ๋๋๋ฌ๊ธฐ', 'foo equal `bar`');
});
it('์ฐพ๋ ๊ฐ์ด ์์ผ๋ฉด ์ธ๋ฑ์ค๊ฐ์ -1', function () {
assert.equal([1, 2, 3].indexOf(4), 1);
});
});
});
-2- ๋ ๋ค pass ๋์ค๊ฒ ์์ฑ
var assert = require('chai').assert;
var foo = '์ด๋ฌ๋ผ์ฐธ๊นจ'
describe('chai assert', function () {
describe('#equal', function () {
it('์ฃผ๋ฌธ์ ์ด๋ ค๋ผ ์ฐธ๊นจ', function () {
assert.equal(foo, '์ด๋ฌ๋ผ์ฐธ๊นจ', 'foo equal `bar`');
});
it('์ฐพ๋ ๊ฐ์ด ์์ผ๋ฉด ์ธ๋ฑ์ค๊ฐ์ -1', function () {
assert.equal([1, 2, 3].indexOf(4), -1);
});
});
});
(3) lengthOf
var assert = require('chai').assert;
var foo = '์ด๋ฌ๋ผ์ฐธ๊นจ'
var beverages = { tea: ['chai', 'matcha', 'oolong'] };
describe('chai assert', function () {
describe('#lengthOf', function () {
it('์ด๋ฌ๋ผ์ฐธ๊นจ๋ 5๊ธ์', function () {
assert.lengthOf(foo, 5, 'foo`s value has a length of 5');
});
it('์ฐจ์ ์ข
๋ฅ๋ 3๊ฐ์ง', function () {
assert.lengthOf(beverages.tea, 3, 'beverages has 3 types of tea');
});
});
});
chai ์ expect์ should๋ ๋ค์ ํฌ์คํ ์์ ๊ณ์..
'mocha' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ ์คํธ ์๋ํ ํ๋ ์์ํฌ Mocha ๊ณต์ ๋ฉ๋ด์ผ ๋ฐ๋ผํ๊ธฐ (1) (0) | 2022.10.31 |
---|