请考虑我有以下 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 更改为此时(在末尾添加了两行):
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
了两次,正如预期的那样。
那么,为什么控制台在第一种情况下是空的?