taichi / grunt-istanbul

JavaScript codecoverage tool for Grunt
https://www.npmjs.com/package/grunt-istanbul
MIT License
107 stars 36 forks source link

Test files in different folder, not stored at common location #20

Open vkadam opened 10 years ago

vkadam commented 10 years ago

In our project we are not storing test files under one location, those are stored related to each source file. So each module will have its test folder. Also we are using mongoose client. So all our mongoose related model files gets loaded (required) in server.js/index.js. What this means that in test case we do not load model file like

var myModel = require('myModule')

but we load it like

var mongoose = require('mongoose');
var myModel = mongoose.model('ModelName');

So we can't use requireHandler for loading model as its getting loaded in server.js/index.js

So we needed a way to keep using require instead of requireHandler but still load instrumented file instead of source one, so we updated our require_handler.js to

if (process.env.APP_DIR_FOR_CODE_COVERAGE) {
    var extname = '.js',
        ext_super = require.extensions[extname],
        filePatternMatch = /\/app\/(?!.*\/test\/)(?!.*_spec\.).*\.(js$)/;
    require.extensions[extname] = function ext(module, filename) {
        filename = filename.match(filePatternMatch) ? filename.replace('/app/', process.env.APP_DIR_FOR_CODE_COVERAGE) : filename;
        return ext_super(module, filename);
    };
}

Basically hook while loading module using require

vkadam commented 10 years ago

I would be nice it this can be part of plugin it self. Load instrumented files from instrumented folder while running coverage.

gregjopa commented 10 years ago

I answered this question in: https://github.com/gregjopa/express-app-testing-demo/issues/1. I think this issue can be closed.