Skip to content

Instantly share code, notes, and snippets.

@blackcoat
Last active August 31, 2017 09:22
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blackcoat/3927320 to your computer and use it in GitHub Desktop.
Save blackcoat/3927320 to your computer and use it in GitHub Desktop.
Sample Mocha test runner in CoffeeScript
# Required to run commands in the shell
# @see http://nodejs.org/api/child_process.html
{spawn} = require 'child_process'
# A basic command runner
run = (cmd, args) ->
console.log cmd, args.join ' '
spawn cmd, args, stdio: 'inherit'
# Set up default arguments for running our Mocha tests.
# The first argument passed to Mocha is the location of our
# test files. Meteor expects our specs to be located in the
# "tests" folder, which differs from Mocha's default test
# location (i.e. "test").
# @see http://docs.meteor.com/#structuringyourapp
# @see http://stackoverflow.com/questions/11785917/where-should-unit-tests-be-placed-in-meteor
mocha_args = [
'tests',
'--recursive',
'--compilers', 'coffee:coffee-script',
'--require', 'chai',
'--ui', 'bdd',
'--colors',
]
# Standard test runner with complete spec-style output
task 'test', 'runs the Mocha test suite', ->
run 'mocha', mocha_args.concat ['--reporter', 'spec']...
# Our watchful test runner to use during development and refactoring
task 'test:watch', 'runs the Mocha test suite whenever a file on the project is changed', ->
run 'mocha', mocha_args.concat [
'--reporter', 'min',
'--watch'
]...
@pipedreambomb
Copy link

I'm pretty new to Node and Meteor, I found this gist from your answer on StackOverflow and have saved this file, but I get the following when I do cake test:

/<myapp>/tests/mocha.js:5240 global = window; ^ ReferenceError: window is not defined

I can guess what that means, it's not running in a browser so the window global variable isn't available. I don't know enough about how any of this works to know how to fix it however! Just want to run unit tests with my meteor app, it seemed a good place to start without too much experience in Node and server-side JavaScript.

@blackcoat
Copy link
Author

Wow, how am I just seeing this!?!?

Yes, you are correct: NodeJS does not have any sense of the window object, causing issues when running tests against Meteor code that expects window to exist. If you are using window to store values globally, you can use global or even process within Node. If you need to emulate window within Node, you could try jsdom or start up your Meteor app within a headless browser like Zombie.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment