Jest 설정하기

1 minute read

Jest 설정하기

이번 글에서는 jest 설정에 대해 알아보겠습니다 :P

Jest

Jest는 facebook에서 만든 test framework 모듈이며, Test 및 Coverage report를 쉽게 생성하고 확인 할 수 있습니다.

먼저 jest framework를 설치합니다.

$ npm install -D jest

Test를 돌리기 위해 package.json에 아래와 같은 run-script를 추가 합니다.

{
  ...
  "scripts": {
    "test": "./node_modules/.bin/jest src",
  }
  ...
}

Business code 및 Test code를 작성합니다.

// src/calc.js
export const sum = (a, b) => (a + b);

Test code는 Businesss code들과 구분하기 위하여 확장자를 *.test.js로 붙여줍니다.

// src/__test__/calc.test.js
import { sum } from '../calc';

describe('calc', () => {
  it('sum', () => {
    expect(sum(1, 2)).toBe(3);
  });
});

이제 Business code 및 Test code를 작성하였으니 Test를 돌려보도록 합시다.

$ npm test

➜  setup-es8 ./node_modules/.bin/jest src
 FAIL  src/__test__/calc.test.js
  ● Test suite failed to run

    Requires Babel "^7.0.0-0", but was loaded with "6.26.3". If you are sure you have a compatible version of @babel/core, it is likely that something in your build process is loadingthe wrong version. Inspect the stack trace of this error to look for the first entry that doesn't mention "@babel/core" or "babel-core" to see what is calling Babel. (While processingpreset: "/Users/juno/Documents/nodejs/setup-es8/node_modules/@babel/preset-env/lib/index.js")

      at throwVersionError (node_modules/@babel/helper-plugin-utils/lib/index.js:65:11)
      at Object.assertVersion (node_modules/@babel/helper-plugin-utils/lib/index.js:13:11)
      at _default (node_modules/@babel/preset-env/lib/index.js:154:7)
      at node_modules/@babel/helper-plugin-utils/lib/index.js:19:12
      at node_modules/babel-core/lib/transformation/file/options/option-manager.js:317:46
          at Array.map (<anonymous>)
      at OptionManager.resolvePresets (node_modules/babel-core/lib/transformation/file/options/option-manager.js:275:20)
      at OptionManager.mergePresets (node_modules/babel-core/lib/transformation/file/options/option-manager.js:264:10)
      at OptionManager.mergeOptions (node_modules/babel-core/lib/transformation/file/options/option-manager.js:249:14)
      at OptionManager.init (node_modules/babel-core/lib/transformation/file/options/option-manager.js:368:12)
      at File.initOptions (node_modules/babel-core/lib/transformation/file/index.js:212:65)
      at new File (node_modules/babel-core/lib/transformation/file/index.js:135:24)
      at Pipeline.transform (node_modules/babel-core/lib/transformation/pipeline.js:46:16)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.886s
Ran all test suites matching /src/i.

에러가 터져나옵니다.. :P Jest는 우리가 설치한 @babel/core 모듈을 참조하지 않고 babel-core를 참조하기 때문에 버전이 맞지 않다고 나옵니다.

Babel은 7버전 부터 @babel 이라는 네임스페이스 밑에서 모듈이 배포되고 있습니다.

이를 해결하기 위하여 구버전인 babel-core에 7.0 bridge 버전을 설치하여야 합니다. 설치해 줍시다.

➜  setup-es8 npm install -D babel-core@7.0.0-bridge.0
npm WARN setup-es8@1.0.0 No repository field.

+ babel-core@7.0.0-bridge.0
added 6 packages from 6 contributors, updated 1 package, moved 2 packages and audited 21101 packages in 4.195s
found 0 vulnerabilities

다시 실행 해 줍니다.

➜  setup-es8 npm test

> setup-es8@1.0.0 test /Users/juno/Documents/nodejs/setup-es8
> jest src

 PASS  src/__test__/calc.test.js
  calc
    ✓ sum (3ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.699s, estimated 1s
Ran all test suites matching /src/i.

성공적으로 테스트 되었습니다 :)

다음 글은 eslint 설정에 대해 알아보겠습니다.

Categories: ,

Updated: