jcollum
jcollum

Reputation: 46589

how can I get my 'before' function to make other tests wait for its completion?

describe 'TheObject', ->
  before ->
    console.log 'loading text'
    fs.readFile('../data/data.json', 'utf8', (err, data) ->
      text = data
    )
  describe 'simple', ->
    it 'should pass a simple test', ->
      b = "1"
      b.should.equal "1" 

I'd like to make it so that none of my tests run until the file read in the before action is complete. But this is asynch world here, so I think it's behaving as expected. Can I put all my other tests into a callback somehow? Can I force the readFile to be blocking?

Upvotes: 0

Views: 72

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 311945

When async, your before function should accept a done callback parameter that it calls when it's completed its processing:

describe 'TheObject', ->
  before (done) ->
    console.log 'loading text'
    fs.readFile('../data/data.json', 'utf8', (err, data) ->
      text = data
      done()
    )

Upvotes: 1

Related Questions