Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoiding require(../../../..) relative paths with grunt mocha

When unit testing, I tend to have a directory called test at the top of my project structure, with the directory structure mimicking the source code that is to be tested. However, these directories can get quite deep, for example

app/src/js/models/User.js

with perhaps a test in

test/app/src/js/models/User.js.

Now, when I want to include the User.js module, I use require('../../../../../app/src/js/models/Users.js') which is very cumbersome.

Ideally, I would like to use require('/app/src/js/models/User.js') or perhaps even require('User.js').

Is this possible? I am using grunt-mocha-test, but I think the question is a more general one.

like image 384
Brendon Avatar asked Jan 03 '15 14:01

Brendon


1 Answers

There are multiple options you could use. Your best bet would probably be to use some npm module, for example this one. Search the npm registory for require, there are tons of options so choose whichever suits your needs.

Alternatively, you could write some helper function that does something similar.

If you were looking for some native way (built-in to Node.js) to achieve this, sadly there are none. You will have to use either a custom function or an npm module to do this in some nice, reusable way.

like image 110
Robert Rossmann Avatar answered Oct 19 '22 19:10

Robert Rossmann