Reputation: 183
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?
Upvotes: 4
Views: 1789
Reputation: 3026
It's possible that your tests are ending before the readFile
callback is executed. The test
method should accept a callback:
class Foobar
test: (path, callback) ->
fs.readFile path, (err, data) ->
console.log 'fs.readFile callback fired'
callback err, data
That way you can write your test to run asynchronously:
it 'calls callback', (done) ->
foobar = new Foobar()
foobar.test 'foobar.txt', done
Upvotes: 4