Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use of requirehelper #1

Closed
vkadam opened this issue Mar 18, 2014 · 1 comment
Closed

Use of requirehelper #1

vkadam opened this issue Mar 18, 2014 · 1 comment

Comments

@vkadam
Copy link

vkadam commented Mar 18, 2014

In our project we store all test file under test folder near each module instead of /root/test. Also we are using mongoose client for mongodb. So all mongoose models are loaded in server.js/index.js where server is started, so it doesn't make sense to user require_helper in production code.

So we needed a way to tell require to use instrumented file instead on source file without using requireHelper, So we used require.extension. Here is sample code for our requireHelper.js

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);
    };
}
@gregjopa
Copy link
Owner

I agree that using require_helper doesn't work for this use case. I have the same use case for an express web app thats organized by module and each module has it's own tests (more about this module design pattern with express.js here: http://vimeo.com/56166857).

Your solution with require.extensions is clever but I don't recommend using it since require.extensions is depreciated: http://nodejs.org/api/globals.html#globals_require_extensions.

Instead, I recommend copying your tests into your coverage folder and running them from there. In the gruntfile.js you can use a variable for the base path of your tests src directory using grunt.option("testBasePath") and set that variable to run your tests from their normal location or to run them from the code coverage folder. I added the branch "no-require-helper" to this project to demonstrate this behavior: https://github.com/gregjopa/express-app-testing-demo/tree/no-require-helper. Check out this solution and see if it will work for you.

cc: taichi/grunt-istanbul#20

@gregjopa gregjopa closed this as completed Apr 5, 2014
evanyasn added a commit to evanyasn/express-app-testing-demo that referenced this issue May 11, 2020
add npm run test to match it with exercise gregjopa#1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants