LoginSignup
14
16

More than 5 years have passed since last update.

mocha + enzyme + power-assertの環境構築メモ

Last updated at Posted at 2016-08-29

node modules

npm install -D babel babel-register babel-preset-latest babel-preset-react babel-plugin-espower mocha power-assert enzyme react-addons-test-utils jsdom jsdom-global
npm install -S react

設定

.babelrc

.babelrc
{
  "presets": [
    "latest",
    "react"
  ],
  "plugins": [
    "babel-plugin-espower"
  ]
}
  • test内でjsx syntaxを書くのでpresetsに"react"の指定が必要
  • .babelrcを使いたくない場合は、babel-plugin-espower#via-babel-require-hookのようなスクリプトを用意して、以下のようにmochaの"-r"オプションでbabel-registerの代わりに渡すとよい
  • presetなどはお好きに

実行

./node_modules/.bin/mocha -r babel-register -r jsdom-global/register path/to/test/**/*.js
  • babel-registerをrequireすることで、ES.next + Reactなテストコードをトランスパイルして実行できる
  • enzymeのrender, mountmethodを実行する為にwindow.documentなどのcontextが必要なので、jsdom-global/registerをrequireすることによって設定する

参考

14
16
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
14
16