次の CoffeeScript コードがあると考えてください。
class Foobar
test: (path) ->
fs = require 'fs'
fs.readFile path, (err, data) ->
console.log 'fs.readFile callback fired'
root = exports ? window
root.Foobar = Foobar
Mocha の次のテスト ファイル:
chai = require 'chai'
expect = chai.expect
chai.should()
{Foobar} = require '../source/foobar'
describe 'Foobar', ->
foobar = null
it 'does nothing', ->
foobar = new Foobar
foobar.test 'foobar.txt'
私はテストを実行します:
mocha --compilers coffee:coffee-script -R spec
奇妙なことに、コンソールは何もログに記録しません。Coffee をこれに変更すると (最後に 2 行追加):
class Foobar
test: (path) ->
fs = require 'fs'
fs.readFile path, (err, data) ->
console.log 'fs.readFile callback fired'
root = exports ? window
root.Foobar = Foobar
foobar = new Foobar
foobar.test 'foobar.txt'
テストを実行すると、予想どおり、コンソール ログfs.readFile callback fired
が 2 回記録されます。
では、なぜ最初のケースでコンソールが空だったのでしょうか?