2014년 12월 16일 화요일

[mochajs] Is it possible to set up a beforeEach() iside of a .js file that is required from the command line with -r?

I'm pretty sure looking at it that there is NOT a means of specifying a file from the command line to be included in a run that defines a before(), beforeEach(), after() or afterEach() which would be ideal for one of my scenarios. 

Based on https://github.com/mochajs/mocha/blob/master/bin/_mocha#L301-L305 it looks like each requires is loaded from the command line with no ability for those files loaded to access the current instance of Mocha to manipulate it in any way.  

Would anyone be opposed to another command line argument that allowed me to do the moral equivalent of "require(...the required file(s)...)(mocha)" where I can pass the current instance of mocha to it?  maybe a command line argument line -e, --extends or similar that allows us to know that the required file will be passed the mocha instance?  I'm happy to do the work, test it and submit a PR - but didn't want to waste my time if there is another way of doing this or if there is a religious argument for not doing such a thing... so let me know if you think this is a bad idea.

Our scenario:  We have dozens of *-spec.js files and would like to be able to clean up the db between each and every test and some other similar utilities like this to ensure truly clean runs and ensuring tests don't depend on other tests for consistency... so it would be ideal to be able to specify from the command line a file that defines a beforeEach or similar rather than forcing every file to have a require('./helper.js') or similar (especially since the spec files are located in arbitrary depth folders and as such we'd have to have a variable number of "/../" in the relative paths and it feels... ugly.

Anyway - thanks in advance for paying any attention to this if you read and respond.  



It seems like if you run beforebeforeEachafter, or afterEach outside of adescribe() block, they will be run for every it() during that suite execution. For example:

~/tmp/mocha ɀ find test
test
test/one.js
test/reset.js
~/tmp/mocha ɀ cat test/reset.js
beforeEach(function() {
console.log('beforeEach');
});
 
afterEach(function() {
console.log('afterEach');
});
~/tmp/mocha ɀ cat test/one.js
describe('one', function() {
it('runs', function() {
});
 
describe('nested', function() {
it('runs too', function() {
});
});
});
~/tmp/mocha ɀ mocha
 
 
one
beforeEach
✓ runs
afterEach
nested
beforeEach
✓ runs too
afterEach
 
 
2 passing (8ms)
 
~/tmp/mocha ɀ

As you can see, the before-/afterEach defined in reset.js helper file are run for every it().



Absolutely - that is 100% the goal - to include from the command line some specific setup for each and every test with the ability on the command line to change the pointer to the file(s) from the command line so that each individual file does not require an include.  And it is not possible today to define a beforeEach, afterEach, before, or after in a file that is "-r" required from the command line.


댓글 없음:

댓글 쓰기