15.8 Jest에서 import/export를 사용하기

1. 설치

$ mkdir jest-using-import && cd jest-using-import
$ npm init -y
$ npm install -D jest @types/jest @babel/core @babel/preset-env

package.json

{
  "name": "jest-using-import",
  "version": "1.0.0",
  "scripts": {
    "test": "jest"
  },
  "devDependencies": {
    "@babel/core": "^7.18.5",
    "@babel/preset-env": "^7.18.2",
    "@types/jest": "^28.1.3",
    "jest": "^28.1.1"
  }
}

2. 설정

babel.config.json 생성

{
  "presets": ["@babel/preset-env"]
}

jest.config.json 생성

{
  "verbose": true,
  "collectCoverage": true
}

3. 테스트

add.js 생성

const add = (a, b) => a + b;

export default add;

add.test.js 생성

import add from './add';

test('adds 1 + 2 to equal 3', () => {
  expect(add(1, 2)).toBe(3);
});

테스트 실행

$ npm test

> jest-using-import@1.0.0 test
> jest

 PASS  ./sum.test.js
  ✓ adds 1 + 2 to equal 3 (1 ms)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files |     100 |      100 |     100 |     100 |
 add.js   |     100 |      100 |     100 |     100 |
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.521 s, estimated 1 s
Ran all test suites.
Back to top
Close