JavaScript - Chai, Mocha

LANA·2020년 4월 6일
1

JavaScript

목록 보기
13/21
post-thumbnail

chai - assert 참조

  • Chai는 테스트에 필요한 헬퍼 함수들이 담긴 라이브러리입니다.
  • Chai는 이전에 만들었던 assert 함수와 동일한 기능을 하는 assert 함수를 제공합니다. 참조
  • 영어 문법에 가까운 코드로 테스트를 작성할 수 있게 도와줍니다.
  • expect는 그 방법 중 하나입니다.(후에 should도 제작)

chai assert

describe("Visa", function() {
   let assert = chai.assert;

  it("has a prefix of 4 and a length of 13", function() {
    assert(detectNetwork("4123456789012") === "Visa");
  });
});

chai assert - expect 용법

describe("Discover", function() {
  let expect = chai.expect;

  it(FILL_ME_IN, function() {
    expect(detectNetwork("5112345678901234")).to.equal("MasterCard");
  });
}

chai assert - should 용법

describe("MasterCard", function() {
  let should = chai.should();
  it("has a prefix of 6011 and a length of 16", function(){
    detectNetwork("6011123456789012").should.equal("Discover");
  });
};

mocha test

  • 함수를 실행할 때 오류가 발생하면, 실패합니다.
  • 오류가 발생하지 않으면, 실패하지 않습니다.
describe("Introduction to Mocha Tests - READ ME FIRST", function() {
it("예상 동작이 실제 동작과 일치하지 않을 때 오류가 발생합니다.", function() {
    let even = function(num) {
      return num / 2 === 0; // 체크하려는 함수에 뭔가 문제가 있군요!
    };

    if (even(10) === true) {
      throw new Error("10은 짝수여야 합니다!");
    }
  });
});
profile
Let's code like chord !

0개의 댓글