grunt-istanbul icon indicating copy to clipboard operation
grunt-istanbul copied to clipboard

Test files in different folder, not stored at common location

Open vkadam opened this issue 10 years ago • 2 comments

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 avatar Mar 18 '14 18:03 vkadam

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

vkadam avatar Mar 18 '14 18:03 vkadam

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

gregjopa avatar Jun 03 '14 22:06 gregjopa