vendredi 27 mai 2016

Gulp: Avoiding login require paths when running tests (mocha)

I recently inherited some node code and tests (written using mocha, simon etc but could potentially be any mixture). The source and tests are in completely different directory structures, I suppose they follow the maven structure if you are used to that in java. so you have the source code buried deep under /main/..... and the tests are buried deep under /test/.....

I wanted to have these tests run by mocha using gulp. Although it appears I need to run some code before actually running the tests and would love some insight to what I need to do.

Looking around if I want to avoid the constant use of ../../... in my tests when i need to require the object on which to run the tests like

    var x = require('../../../../main/mycomponent')

I need to run some code before running the tests, this is not code that should run before each test but more of setting the context for the running of tests, I believe I shouldn't have to put these in any executing tests as this just smells like code smell.

After trying to figure out what I need to do, it appears that the following is important

  require('module').Module._initPaths();

and maybe using in conjunction with setting the node paths?

Can anyone help ?

I suppose what I am trying to achieve is going from this, that needs to be scattered around my test files

    var x = require('../../../../main/mycomponent')

to something like this

    var x = require('main/mycomponent') // cleaner

or

    var x = require('my component') // even cleaner

My choice of tools is gulp but I just can't figure out I how need to execute the code before running the tests.

Thanks in advance

Aucun commentaire:

Enregistrer un commentaire