Mocha - the fun, simple, flexible JavaScript test framework
Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple and fun. Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct te
mochajs.org
ํ์ฌ์์ ํ ์คํธ ํ๋ ์์ํฌ ์ค jest์ mocha ์ค mocha๋ฅผ ์ฑํํ๋ ์ชฝ์ผ๋ก ๊ธฐ์ธ์ฌ์ง๊ณ ์๋ ๊ฒ ๊ฐ์์ mocha ๊ณต์ ํํ์ด์ง์ ์์ ๋ฅผ ๋ฐ๋ผํด๋ณด๋ ํฌ์คํ (์กฐ๊ธ ๋ ๋์๊ฒ ํธํ๊ฒ ๊ณผ์ ์ ๋ฐ๊พธ์๋ค)
์๋ํฐ๋ ๋น์ฅฌ์ผ ์คํ๋์ค ์ฝ๋๋ฅผ ์ฌ์ฉํ๋ค.
1. ์๋ก์ด ํ๋ก์ ํธ๋ฅผ ๋ง๋ ๋ค
2. npm init -y
2. ์๋ ๋ช ๋ น์ด๋ฅผ ํฐ๋ฏธ๋์ ์ ๋ ฅ!
npm install --save-dev mocha
๊ทธ๋ฌ๋ฉด package.json ์ด ์ด๋ ๊ฒ ๋ณ๊ฒฝ๋๋ค.
{
"name": "mochatestingpj",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"mocha": "^10.1.0"
}
}
์ฌ๊ธฐ์ ๋๋ ์คํฌ๋ฆฝํธ ๋ถ๋ถ์ ๋ค์๊ณผ ๊ฐ์ด ๋ณ๊ฒฝํ๊ณ ๋ค์ ํฐ๋ฏธ๋์ npm install ์์ฑ
"scripts": {
"test": "mocha"
},
์ด๋ ๊ฒ ํ๋ฉด ํ ์คํ ํ ๋ npx mocha ๋์ ์ npm test๋ฅผ ์์ฑํ์ฌ๋ ๋๊ฐ์ด ๋์๊ฐ๊ฒ ๋๋ค.
You can add a package.json file to your package to make it easy for others to manage and install. Packages published to the registry must contain a package.json file.
- lists the packages your project depends on
- specifies versions of a package that your project can use using semantic versioning rules
- makes your build reproducible, and therefore easier to share with other developers
๊ธฐ๋ณธ์ ์ผ๋ก๋ package.json์ ๋ฌธ์๋ค.
๊ฐ๋ฐ์๊ฐ ๋ฐฐํฌํ ํจํค์ง์ ๋ํด, ๋ค๋ฅธ ์ฌ๋๋ค์ด ๊ด๋ฆฌํ๊ณ ์ค์นํ๊ธฐ ์ฝ๊ฒ ํ๊ธฐ ์ํ ๋ฌธ์.
npm์ ํจํค์ง๋ฅผ ๋ฐฐํฌํ๊ณ npm registry์ ์ฌ๋ฆฌ๊ธฐ ์ํด์ ๋ฐ๋์ ํ์ํ ๋ฌธ์ํ์ผ์ด๋ค.
- ์์ ์ ํ๋ก์ ํธ๊ฐ ์์กดํ๋ ํจํค์ง์ ๋ฆฌ์คํธ
- ์์ ์ ํ๋ก์ ํธ์ ๋ฒ์ ์ ๋ช ์
- ๋ค๋ฅธ ํ๊ฒฝ์์๋ ๋น๋๋ฅผ ์ฌ์ ๊ฐ๋ฅํ๊ฒ ๋ง๋ค์ด, ๋ค๋ฅธ ๊ฐ๋ฐ์๊ฐ ์ฝ๊ฒ ์ฌ์ฉํ ์ ์๋๋ก ํ๋ค.
์ฆ, npm์ด๋ผ๋ ์คํ์์ค ํจํค์ง ์ํ๊ณ๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํ ๋ช ์ธ์ด์, ํ๋ก์ ํธ์ ์์กด์ฑ ๊ด๋ฆฌ๋ฅผ ์ํ ๋ช ์ธ, ๋ ์ด ์ํ๊ณ๋ก์ ๋ฐฐํฌ๋ฅผ ์ํ ๋ช ์ธ๋ผ๊ณ ๋ณผ ์ ์๋ค.
ํ๋ก์ ํธ์ ๊ฐ๋ฐ๋ง์ ์ํ ๋ชฉ์ ์ธ์ง, ํจํค์ง๋ฅผ ๋ฐฐํฌํ๊ธฐ ์ํ ๋ชฉ์ ์ธ์ง์ ๋ฐ๋ผ ์ฉ๋๊ฐ ๊ฐ๋ฆด ์ ์๊ฒ ๋ค๋ผ๊ณ ์๊ฐํ๋ค.
์ผ๋ฐ์ ์ผ๋ก ํจํค์ง๋ฅผ ๋ถ๋ฌ์์ ํ๋ก์ ํธ๋ฅผ ๊ฐ๋ฐํ๋๊ฒ ๋ชฉ์ ์ด๋ผ๋ฉด, ์์กด์ฑ ๊ด๋ฆฌ์ ๋ํ ๋ช ์ธ๊ฐ ๋ ์ค์ํ ๊ฒ์ด๋ค.
์ถ์ฒ : https://hoya-kim.github.io/2021/09/14/package-json/
3. ํ๋ก์ ํธ์ test ๋ผ๋ ํด๋๋ฅผ ๋ง๋ ๋ค.(๋ฐ๋์ test ๋ก ์์ฑ!)
4. ๊ทธ ์์ js ํ์ผ์ ๋ง๋ ๋ค(์ด๋ฆ ์์ )
5. js ํ์ผ์ mocha ๊ณตํ์ ์๋ ๋ค์๊ณผ ๊ฐ์ ์์ ๋ฅผ ๋ถ์ฌ๋ฃ๊ณ , npx mocha ๋ฅผ ์ ๋ ฅ!
var assert = require('assert');
describe('Array', function () {
describe('#indexOf()', function () {
it('should return -1 when the value is not present', function () {
assert.equal([1, 2, 3].indexOf(4), -1);
});
});
});
ํด์ :
[1, 2, 3].indexOf(4)
๋ฐฐ์ด์์ 4๋ฅผ ์ฐพ๋๋ค(์์ผ๋ฉด ์ธ๋ฑ์ค๋ฅผ -1๋ก ์ถ๋ ฅ๋จ)
assert.equal([1, 2, 3].indexOf(4), -1);
assert.equal ์ฒซ๋ฒ์งธ ์ธ์์ ๊ฒฐ๊ด๊ฐ์ผ๋ก ๊ธฐ๋๋๋ ๊ฐ์ ๋๋ฒ์งธ ์ธ์์ ๋ฃ๊ณ ,
์ฒซ๋ฒ์งธ ์ธ์ ๊ฒฐ๊ด๊ฐ๊ณผ, ๊ธฐ๋๊ฐ์ผ๋ก ์์ฑํ ๋๋ฒ์งธ ์ธ์๊ฐ ๊ฐ์ผ๋ฉด pass, ์๋๋ฉด fail์ด ๋ฌ๋ค.
๊ฐ์ ์กฐ๊ธ ๋ฐ๊พธ๊ฒ ์ ์ด๋ฃ์ด๋ ๊ฒฐ๊ณผ๋ ๊ฐ๋ค
var assert = require('assert');
describe('Array', function () {
describe('#indexOf()', function () {
it('should return 2', function () {
assert.equal([1, 2, 3].indexOf(3), 2);
});
});
});
์ผ๋ถ๋ก ํ๋ฆฐ ๊ธฐ๋๊ฐ์ ์์ฑํด์ ๋ฃ์ผ๋ฉด fail์ด ๋ฌ๋ค.
var assert = require('assert');
describe('Array', function () {
describe('#indexOf()', function () {
it('should return -1', function () {
assert.equal([1, 2, 3].indexOf(4), -1 + 1);
});
});
});
์ฌ๊ธฐ๊น์ง๊ฐ ๊ธฐ๋ณธ!
~ํ ์คํธ ์๋ํ ํ๋ ์์ํฌ Mocha ๊ณต์ ๋ฉ๋ด์ผ ๋ฐ๋ผํ๊ธฐ ์๋ฆฌ์ฆ๋ ์ด์ด์ ์์ฑ๋๋ค.~
'mocha' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ ์คํธ ์๋ํ ํ๋ ์์ํฌ Mocha ๊ณต์ ๋ฉ๋ด์ผ ๋ฐ๋ผํ๊ธฐ (2) (0) | 2022.10.31 |
---|