LoginSignup
20
14

More than 5 years have passed since last update.

TypeScriptとJestとCircleCIとCodecovな環境

Last updated at Posted at 2017-08-29

TypeScript

まずは、本体をインストール。

npm i -D typescript
# yarn add -D typescript

tsconfig.jsonの作成。

npx tsc --init
# yarn run tsc --init

適当に変換するファイル作成。

index.ts
export function plus(x: number, y: number): number {
  return x + y;
}

以下で変換できればok。

npx tsc index.ts
# yarn tsc index.ts

push時に、色々含まれないようにする。

 cat > .gitignore <<A
> node_modules/
> index.js
> coverage/
> A

Jest

インストール。

npm i -D jest ts-jest @types/jest
# yarn add -D jest ts-jest @types/jest

通るテストを書く。

index.test.ts
import {plus} from './index.ts';

test('plus', () => {
  expect(plus(1, 1)).toBe(2);
});

package.jsonにこんな感じのjestプロパティを追加。

  "jest": {
    "transform": {
      "^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
    },
    "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(tsx?|jsx?)$",
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "json",
      "jsx"
    ]
  },

また、run-scriptstestで使えるようにする。package.jsonにこんなのを追記。

  "scripts": {
    "test": "jest index.test.ts"
  }

テスト。

npm test
# yarn test
#
# ----------------------
#  PASS  ./index.test.ts
#   ✓ plus (3ms)
#     ...

CircleCI

設定ファイルを作る。

mkdir .circleci && touch .circleci/config.yml

中身はこんな感じ。(Nodeのサンプルのyarnnpmに変えただけ)

.circleci/config.yml
version: 2
jobs:
  build:
    docker:
      - image: circleci/node:7.10
    working_directory: ~/repo
    steps:
      - checkout
      - restore_cache:
          keys:
          - v1-dependencies-{{ checksum "package.json" }}
          - v1-dependencies-
      - run: npm install
      - save_cache:
          paths:
            - node_modules
          key: v1-dependencies-{{ checksum "package.json" }}
      - run: npm test

Githubへプッシュ。

git init
git add --all
git commit -m 'Init'
git remote add origin git@github.com:...
git push origin master

CircleCIの設定。「Add Project]のからの、

Projects_-_CircleCI_🔊.png

適当なリポジトリをセットアップを押して、

Add_Projects_-_CircleCI_🔊.png

Add_Projects_-_CircleCI_🔊 3.png

「Start Building」をクリック。

Add_Projects_-_CircleCI_🔊 2.png

通った。

CircleCI_🔊.png

Codecov

まずはインストール。

npm i -D codecov
# yarn add -D codecov

package.jsonjestプロパティを編集して、coverage/に結果を出すようにする。こんな感じ。

  "jest": {
    "transform": {
      "^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
    },
    "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(tsx?|jsx?)$",
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "json",
      "jsx"
    ],
    "coverageDirectory": "./coverage/",
    "collectCoverage": true
  },

あとscriptstestcodecovコマンドを続けて実行するように。

  "scripts": {
    "test": "jest index.test.ts && codecov"
  }

codecovコマンドでデータを送るのにトークンが必要なので、その辺の設定をする。

Codecovの対象リポジトリのページを開く。この辺から、

Team_⋅_nju33_🔊.png

開くとトークンがあるので、コピー。

Dashboard_⋅_nju33_example-typescript-jest-circleci-codecov_🔊.png

CI上でCODECOV_TOKENという名前で見れるように、環境変数を登録する。

CircleCI_🔊.png

「Add Variable」から。

Project_settings_-_nju33_example-typescript-jest-circleci-codecov_-_CircleCI_🔊.png

(モザイクはトークン)

Project_settings_-_nju33_example-typescript-jest-circleci-codecov_-_CircleCI_🔊.png

先程の修正をプッシュして、無事CIが通ったらCodecovのページが更新。
こんな感じになってたらok!!

codecov_io-gh-nju33-example-typescript-jest-circleci-codecov.png

おわりに

実際のnpm testの中では[ "true" = "$CI" ] && ... || ...とか使ってローカルではcodecovしないとかすると良いと思います。

リポジトリ

nju33/example-typescript-jest-circleci-codecov

20
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
14