Please consider I have the following CoffeeScript code:
class Foobar
test: (path) ->
fs = require 'fs'
fs.readFile path, (err, data) ->
console.log 'fs.readFile callback fired'
root = exports ? window
root.Foobar = Foobar
And the following test file for 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'
I run the test:
mocha --compilers coffee:coffee-script -R spec
Strangely for me console logs nothing. When I change my Coffee to this (added two lines at the end):
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'
I run the test, and now console logs fs.readFile callback fired twice, as expected.
So, why console was empty in the first case?
It’s possible that your tests are ending before the
readFilecallback is executed. Thetestmethod should accept a callback:That way you can write your test to run asynchronously: