Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make Mocha load a helper.js file that defines global hooks or utilities?

I have a file named test/helper.js that I use to run Mocha tests on my Node.js apps. My tests structure looks like:

test/ test/helper.js    # global before/after test/api/sometest.spec.js test/models/somemodel.spec.js ... more here 

The file helper.js has to be loaded because it contains global hooks for my test suite. When I run Mocha to execute the whole test suite like this:

mocha --recursive test/ 

the helper.js file is loaded before my tests and my before hook gets executed as expected.

However, when I run just one specific test, helper.js is not loaded before the test. This is how I run it:

mocha test/api/sometest.spec.js 

No global before called, not even a console.log('I WAS HERE');.

So how can I get Mocha to always load my helper.js file?

like image 541
Zlatko Avatar asked Jan 28 '15 11:01

Zlatko


People also ask

What are hooks in mocha?

Mocha is a feature-rich JavaScript test framework for Node. js. Mocha provides several built-in hooks that can be used to set up preconditions and clean up after your tests. The four most commonly used hooks are: before() , after() , beforeEach() , and afterEach() .

How does Mocha JavaScript work?

Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple and fun. Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases. Hosted on GitHub.

Is Mocha a JavaScript library?

Mocha is an open source JavaScript testing framework that runs on Node. js and in the browser. It's designed for testing both synchronous and asynchronous code with a very simple interface.

What is Mocha and Chai js?

Mocha is a JavaScript test framework running on Node. js and in the browser. Mocha allows asynchronous testing, test coverage reports, and use of any assertion library. Chai is a BDD / TDD assertion library for NodeJS and the browser that can be delightfully paired with any javascript testing framework.


1 Answers

Mocha does not have any notion of a special file named helper.js that it would load before other files.

What you are trying to do works when you run mocha --recursive because of the order in which Mocha happens to load your files. Because helper.js is one level higher than the other files, it is loaded first. When you specify an individual file to Mocha, then Mocha just loads this file and, as you discovered, your helper.js file is not loaded at all.

So what you want to do is load a file such that it will set top level ("global") hooks (e.g. before, after, etc.). Options:

  1. You could use Mocha programmatically and feed it the files in the order you want.

  2. You could force yourself to always specify your helper file on the command line first before you list any other file. (I would not do this, but it is possible.)

  3. Another option would be to organize your suite like I've detailed in this answer. Basically, you have one "top level" file that loads the rest of the suite into it. With this method you'd lose the ability of running Mocha on individual files, but you could use --grep to select what is being run.

You cannot use the -r option. It loads a module before running the suite but, unfortunately, the loaded module does not have access to any of the testing interface that Mocha makes available to your tests so it cannot set hooks.

like image 96
Louis Avatar answered Sep 25 '22 01:09

Louis