## Summary and Question

I'm having an issue getting my gulp tasks to recognize a helper file for mocha. 

* `mocha` or `npm test` run it properly
* the gulp mocha task fails:
  - to load the global vars from the helper file under the gulp task
  - to find the root hooks if I keep them in the same helper file and import that file
  

**Workaround:** The only way I can get it to work is to:

1. declare the requirement in the specs for the helper file, and
2. split the hooks out into their own file.

Not the end of the world, but I was hoping for automagic.


**QUESTION**:  Mocha respects the `test/mocha.opts` and can also the common file with `mocha.opts` not being present. How can I get my gulp task to load the common helper file _**without**_ having to add a requirement to each test file?

-------------------------

## TL;DR:

### works under Mocha

I have a helper file `test/common.js` that contains:

+ a few global exports, such as `chai`, `chaiHttp`, and `should`
+ Mocha's [Root Level Hooks]() `before`, `after`, `beforeEach`, `afterEach` for some test setup and teardown.

I **do not**:

- have a `mocha.opts` as Mocha has picked up the `test/common.js` file automatically whenever running tests.
- need to use `require ('./common');` inside my test files

Without both of those, Mocha still recognizes the file, properly loads and fires the hooks, and properly loads my globals. I have verified this works properly under the following calls:

+ `mocha`
+ `mocha --async-only` 
+ `mocha --watch`
+ and all combined: `mocha --watch --async-only`

### cannot get it to work under gulp

When I fire off mocha under a gulp task *(which we have named `gulp api-test`)* it fails to load the helper file(s).

I have tried:

+ creating `test/mocha.opts` and adding `--require test/common`;
	- *note I tried incorrect syntax and got `Cannot find module...` errors; the above finds the file correctly*
+ passing the helper file in as a `--require argument`. It fails with `Reference Error: path is not defined` where `path` is one of my globals.
  - _**note** I get the same behavior no matter what I pass to the --require argument, so I don't think it's recognizing it; my guess is this could be either ignored or where my error is_
+ splitting the root hooks out into their own file `hooks.js` and then trying any of the above. Still `path` error.

Best I can tell, it seems that when the gulp task runs mocha, I cannot successfully get mocha to

- load the mocha.opts file
- or listen to the require argument passed to it by the gulp task.

### variations of the gulp task

Here are different flavors / options I tried that made no difference. Similar approaches were tried with `gulp-spawn-mocha` as well. All are based off the sample tasks shown on the appropriate npm pages:

    const mocha = require('gulp-mocha');

    // 1:
    gulp.task('test-api', function () {
        return gulp.src(['./test/*.spec.js'], {read: false})
            // gulp-mocha needs filepaths so you can't have any plugins before it
            .pipe(mocha({reporter: 'nyan'}));
    });
    
    // 2:
    gulp.task('test-api', function () {
        return gulp.src(['./test/*.spec.js'], {read: false})
            .pipe(mocha({reporter: 'nyan', require: 'test/common' }));
    });
    
    // 3: same as 2 but:
    .pipe(mocha({reporter: 'nyan', require: './test/common' }));
    
    // 4: other requires tried:
    ... require: './test/common.js'
    ... require: 'common'
    ... require: 'mocha.opts'
    ... require: 'test/mocha.opts'
    ... require: 'test/mocha'
    

### the only thing that's worked.

As mentioned above in the workaround, the only thing that works is to add the requirement to my test specs.

    `require ('./common');` 
    
I was hoping that I could instead wire it up through the gulp task and not have to do that. *Given the amount of time I've spent trying to get this to work and then posting here, I should probably just get over it...*
    
### directory structure

    workingdir/
      + src/
          + client/
          + server/
              + app.js
              + ...
              + routes/
                  + orders.routes.js    /* sut */
      + test/
          + mocha.opts      /* references common.js; makes no difference if present or not */
          + common.js       /* helper file with hooks & globals */
          + orders.spec.js  /* uses hooks & vars from common */
          + users.spec.js   /* uses hooks & vars from common */
          
