본문 바로가기
archive.tar

IntelliJ IDEA , Webstorm mocha 실행/디버깅

by 냉동만두 2020. 12. 22.

IntelliJ IDEA / Webstrorm 에서 mocha js 실행, 디버깅 사용 하기

intelli, nodejs, mocha

개요

나는 intellij idea ultimate 현질 했다. 제네시스로 개인택시를 운영하면 이런 느낌일 것 같다.

G80 개인택시. 남자들의 자동차 ‘이원우’

가끔 intellij를 사용 하다보면 편집기? 용도로만 사용 하는 느낌이 든다. 자바 프로젝트는 실행, 디버그 모드에서 별다른 설정 없이도 break point를 잡는데 문제가 없다.

node.js 프로젝트에서는 약간의 설정이 필요 하다. 비싼 돈 주고 인텔리제이 사놓고, 터미널에서 npm run test 하면...

intellij 에서 제공하는 mocha 지원 기능을 써본다.

누가 vscode 소리를 내었는가

준비물

- intellij  / websotrm

- nodejs + mocha 프로젝트

- 위 항목 2가지가 뭐하는건지 아는 사전 지식

 

테스트 프로젝트

테스트를 위해 소스파일 1개, 테스트 파일 2개가 있는 프로젝트를 준비 한다.

 

 

package.json

- test:all 전체 테스트 파일 실행 <- intellij 에서 설정 할 명령어

- test:unit 특정 테스트 파일 단독 실행 <- "$ npm run test:unit test/index2.test.js"

{
  "name": "mocha_test",
  "version": "1.0.0",
  "description": "",
  "main": "src/index.js",
  "scripts": {
    "test:all": "mocha test/*.test.js",
    "test:unit": "mocha"
  },
  "devDependencies": {
    "chai": "^4.2.0",
    "mocha": "^8.2.1"
  }
}

 

// src/index.js

/**
 * 쓸데없는 비동기 더하기 함수
 *
 * @param argA 첫번째 인자
 * @param argB 첫번째 인자
 * @return Promise<any>
 */
const addFunction = (argA, argB) => new Promise((resolve, reject) => {
    try {
        const distNum = argA + argB;
        resolve(distNum)
    } catch (e) {
        reject(e)
    }
});

module.exports = {addFunction};

 

// test/index1.test.js

const chai = require('chai');
const { addFunction } = require('../src/index')
const { expect } = chai;

describe('index test 1', () => {
  it('add args', async () => {
    const argA = 10;
    const argB = 2;

    const result = await addFunction(argA, argB);
    console.log({
        argA,
        argB,
        result,
    });

    expect(result).to.equal(argA + argB);
  });
});
// test/index2.test.js

const chai = require('chai');
const { addFunction } = require('../src/index')
const { expect } = chai;

describe('index test 1', () => {
  it('add args', async () => {
    const argA = 10;
    const argB = 2;

    const result = await addFunction(argA, argB);
    console.log({
        argA,
        argB,
        result,
    });

    expect(result).to.equal(argA + argB);
  });
});

 

IntelliJ IDEA  / Webstorm 설정

1. 실행 설정

실행/디버그 설정

2. 설정 편집

기본 interpreter / working dir 설정은 프로젝트에 맞게 변경 한다.

[Mocha ackage] : mocha 실행 파일 지정. 이미 설치 되어 있었다면 자동 완성 되어 있음

[Extra Mocka option] : mocha 실행 시 추가 옵션 명령어 입력. ex) --require @babel/register

[Test file patterns] : 특정 파일 이름 패턴을 입력하여 해당 하는 파일 모두 실행. 특정 파일 또는 다른 조건 가능

 

설정 편집
설정 후 mocha 실행 버튼

3. mocha 실행/디버깅

실행 하면 별도의 mocha 콘솔이 실행 되어, 테스트 케이스별로 콘솔을 분리 하여 보여준다

디버그 모드로 실행 후 break point도 인식 한다

intellij mocha ui
test case console

 

debug break point

 

마무리

비싼 돈주고 IDEA Ultimate 샀으니, 기능 잘 활용 하여 광명 찾자

2020 버전 세일 안하나,  2018 버전 -50% all package 샀는데

 

시-원