alex-seville / blanket

blanket.js is a simple code coverage library for javascript. Designed to be easy to install and use, for both browser and nodejs.
http://blanketjs.org
Other
1.41k stars 179 forks source link

Please restore require.extensions['.js'] #392

Open pmendelski opened 10 years ago

pmendelski commented 10 years ago

Hi,

I'm using blanket.js for test coverage. It's a great tool - thanks!

Although I found some minor bug.

Scenario: I want to run instrumnted tests. If they fail I would like to rerun the tests but without instrumentalization. I'm using grunt with grunt-mocha-test plugin like this:

    mochaTest: {
        options: {
            reporter: 'spec'
        },
        functional: {
            src: [paths.test + '/functional/**/*test.js']
        },
        integration: {
            src: [paths.test + '/integration/**/*test.js']
        },
        unit: {
            src: [paths.test + '/unit/**/*test.js']
        },
        testcov: {
            options: {
                require: paths.test + '/blanket',
                clearRequireCache: true
            },
            src: [paths.test + '/**/*test.js']
        },
        coverage: {
            options: {
                reporter: 'html-cov',
                quiet: true,
                captureFile: paths.tmp + '/coverage.html'
            },
            src: [paths.test + '/**/*test.js']
        }
    },

I created a grunt task that executes this scenario and in case of testcov error runs not instrumented tests. Although because of line 128 in blanket/src/index.js (blanket version 1.1.6) once instumented tests stayed instrumented for all other tasks.

    require.extensions['.js'] = function(localModule, filename) { ... } // Please restore it after tests execution!

Fix I had to restore require.extensions['.js'] function manually to fix it. I think you could do the same in blanket to prevent this strange situation.

Cheers!

bsodzik commented 10 years ago

Good point, I have spent quite some time to find out that clearing require.cache is not enough. It would be great to describe how blanket works under the hood (just few sentences in documentation).

BTW. I also noticed that require.extensions is deprecated. Do you have any idea on how to replace it with different mechanism?

And of course many thanks for creating and supporting blanket. It is really great!

dynnamitt commented 10 years ago

related to our prob here? : https://github.com/thlorenz/proxyquire/issues/33

danvk commented 9 years ago

@bsodzik can you say more about why clearing require.cache was insufficient to solve your problems? Clearing it in my require.extensions handler fixed a similar problem that I was having.

bsodzik commented 9 years ago

@danvk, besides clearing require.cache we also had to restore require.extensions['.js'] after blanket had finished its work (as @mendlik mentioned in his post). Now I see that blanket provides method restoreNormalLoader which is doing exactly what we implemented in our code. Unfortunately, back in February 2014 there was neither a documentation for that method nor a good soul to tell us about its existence.

What is a little bit worrying is that there is still no documentation for restoreNormalLoader. Maybe that method is not needed any more, but it still exists in blanket code..