Solid Rhino
Solid Rhino

Reputation: 1895

Can't get a file required before every test

I try to setup mocha in combination with coffeescript and chai. Before every file containing the tests, I want to include the following file:

test/_helper.coffee

path = require 'path'

AppDir = path.resolve("#{__dirname}/../src/app")

chai = require('chai')
should = chai.should()
factories = require('chai-factories')
chai.use(factories)

This is so that I have access to the AppDir variabel. So that I don't have to specify the complete path to the app directory when I want to require a file.

test/app/setup-test.coffee

describe 'Setup instance', ->
    it 'should be a object', ->
        setup = require "#{AppDir}/setup"
        setup.should.be.a('object')

I tried the following setups:

Added _hellper.coffee to mocha commandline options like so:

./node_modules/.bin/mocha --require coffee-script --require test/_helper.coffee --compilers coffee:coffee-script --recursive --reporter spec test

and so:

./node_modules/.bin/mocha --require coffee-script --compilers coffee:coffee-script --recursive --reporter spec test/_helper.coffee test

And I tried a normal require in setup-test.coffee:

require '../_helper.coffee'

What ever method I use, I get the following error if I run the test:

Setup instance
    1) should be a object


  ✖ 1 of 1 test failed:

  1) Setup instance should be a object:
     ReferenceError: AppDir is not defined
      at Context.<anonymous> (/Users/ivotrompert/Dropbox/projects/nodejs/getWallpapers/test/app/setup-test.coffee:8:28)
      at Test.Runnable.run (/Users/ivotrompert/Dropbox/projects/nodejs/getWallpapers/node_modules/mocha/lib/runnable.js:184:32)
      at Runner.runTest (/Users/ivotrompert/Dropbox/projects/nodejs/getWallpapers/node_modules/mocha/lib/runner.js:300:10)
      at Runner.runTests.next (/Users/ivotrompert/Dropbox/projects/nodejs/getWallpapers/node_modules/mocha/lib/runner.js:346:12)
      at next (/Users/ivotrompert/Dropbox/projects/nodejs/getWallpapers/node_modules/mocha/lib/runner.js:228:14)
      at Runner.hooks (/Users/ivotrompert/Dropbox/projects/nodejs/getWallpapers/node_modules/mocha/lib/runner.js:237:7)
      at next (/Users/ivotrompert/Dropbox/projects/nodejs/getWallpapers/node_modules/mocha/lib/runner.js:185:23)
      at Runner.hook (/Users/ivotrompert/Dropbox/projects/nodejs/getWallpapers/node_modules/mocha/lib/runner.js:205:5)
      at process.startup.processNextTick.process._tickCallback (node.js:244:9)

And here is my MAKE file if someone want to know how I run the tests:

Makefile

REPORTER = spec
DEFAULT_COMMAND = ./node_modules/.bin/mocha --require coffee-script --ui bdd --compilers coffee:coffee-script --recursive --growl --reporter
check: test

test:
    @NODE_ENV=test $(DEFAULT_COMMAND) $(REPORTER)
test-watch:
    @clear
    @NODE_ENV=test $(DEFAULT_COMMAND) $(REPORTER) --watch
coverage:
    @jscoverage --no-highlight src src-cov
    @SRC_COV=1 $(DEFAULT_COMMAND) html-cov > coverage.html
    @rm -rf src-cov
ifeq ($(shell uname), Darwin)
    @open coverage.html
else
    @xdg-open coverage.html &> /dev/null &
endif

.PHONY: test

Can someone please help me, thank you.

Upvotes: 2

Views: 380

Answers (1)

vik
vik

Reputation: 772

Here is one Solution.

_helper.coffee

path = require 'path'

exports.AppDir = path.resolve("#{__dirname}/../src/app")

chai = require('chai')
should = chai.should()
factories = require('chai-factories')
chai.use(factories)

your test coffee

{AppDir} = require "../helpers"

describe 'Setup instance', ->
    it 'should be a object', ->
        setup = require "#{AppDir}/setup"
        setup.should.be.a('object')

What I have done is use exports and require returns multiple objects you can separate them by comma if you have more exports in your _helper.coffee. The Order is relevant.

Side note: But why would your require a file in your test? I wouldn't like to have some hidden setup in a external file for one test. That doesn't make it easy to understand the test later or for others.

Upvotes: 2

Related Questions