Anonymous
Anonymous

Reputation: 6251

How to log correctly with Mocha/Velocity (Meteor testing)?

What's the correct way to go about logging out information about tests using the velocity framework with Meteor?

I have some mocha tests that I'd like to output some values from, I guess it'd be good if the output could end up in the logs section of the velocity window... but there doesn't seem to be any documentation anywhere?

Upvotes: 7

Views: 464

Answers (2)

neo post modern
neo post modern

Reputation: 3424

This is a terrible hack. It will expose an unprotected method that writes to your DB.

But it works.

I was really annoyed to lack this feature so I digged into the Velocity code to find out that they have a VelocityLogs collection that is globally accessible. But you need to access it from your production, not testing, instance to see it in the web reporter.

So it then took me a good while to get Meteor CORS enabled, but I finally managed - even for Firefox - to create a new route within IronRouter to POST log messages to. (CORS could be nicer with this suggestion - but you really shouldn't expose this anyway.)

You'll need to meteor add http for this.

Place outside of /tests:

if Meteor.isServer
  Router.route 'log', ->
    if @request.method is 'OPTIONS'
      @response.setHeader 'Access-Control-Allow-Origin', '*'
      @response.setHeader 'Access-Control-Allow-Methods', 'POST, OPTIONS'
      @response.setHeader 'Access-Control-Max-Age', 1000
      @response.setHeader 'Access-Control-Allow-Headers', 'origin, x-csrftoken, content-type, accept'
      @response.end()

      return

    if @request.method is 'POST'
      logEntry = @request.body

      logEntry.level ?= 'unspecified'
      logEntry.framework ?= 'log hack'
      logEntry.timestamp ?= moment().format("HH:mm:ss.SSS")

      _id = VelocityLogs.insert(logEntry)

      @response.setHeader 'Access-Control-Allow-Origin', '*'
      @response.end(_id)

      return

  , where: 'server'

Within tests/mocha/lib or similar, as a utility function:

@log = (message, framework, level) ->
  HTTP.post "http://localhost:3000/log",
    data: { message: message, framework: framework, level: level}
    (error) -> console.dir error

For coffee haters: coffeescript.org > TRY NOW > Paste the code to convert > Get your good old JavaScript.

Upvotes: 0

Tomas Romero
Tomas Romero

Reputation: 8698

I haven't seen it documented either.

I don't know how to log messages into the Velocity window, though I don't like the idea of logging into the UI.

What I've done is created a simple Logger object that wraps all of my console.{{method}} calls and prevents logging if process.env.IS_MIRROR. That will only output test framework messages on the terminal. If I need to debug an specific test, I activate logging output for a while on Logger.

Upvotes: 1

Related Questions