LoginSignup
14
11

More than 5 years have passed since last update.

mochaをCoffeeScriptで使おう

Posted at

mochaをCoffeeScriptで書いたプロジェクトで上手く動かなかったのでメモ

$ npm install --save mocha chai
$ mkdir test

mochaでは何も指定が無いときは./test/*.jsを片っ端から舐めていきます。
しかし、CoffeeScriptの案件ですと一々コンパイルの手間がかかってしまいます。
mochaコマンド実行時に引数としてパラメータを渡しても良いのですが、使いまわすために./test/mocha.optsファイルをコピペして作っておきましょう。

./test/mocha.opts
--compilers coffee:coffee-script/register
--reporter spec
--colors

--compilersは使用するCoffeeScriptのバージョンによって変わります。

CoffeeScript 1.7.x以上の場合

--compilers coffee:coffee-script/register

CoffeeScript 1.7.x未満の場合

--compilers coffee:coffee-script

他の紹介記事などが古かったために、ここでコケました。

その他のパラメータはいつものように設定して構いません。

実際に使ってみる

./TestClass.coffee
class TestClass
    getModifyText:(text)->
        return "text is " + text
module.exports = TestClass
./test/test.coffee
chai = require "chai"
assert = chai.assert

testClass = new (require "../TestClass")

describe 'Test TestClass.', ->
    it 'modify text', (done)->
        assert.equal testClass.getModifyText("hello"), "text is hello"
        done()
14
11
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
11