apigeecs / apigee-deploy-grunt-plugin

The API Management Lifecycle Tool that makes your life easier
Apache License 2.0
25 stars 17 forks source link

Add Istanbul.js for code coverage #17

Open firefoxNX opened 9 years ago

firefoxNX commented 9 years ago

Would it be possible to add istanbul.js to generate code coverage for unit tests?

dzuluaga commented 9 years ago

I'd love too @firefoxNX. My colleagues @seymen and @sauliuz have done more extensive work on Istambul. Ozan wrote a nice article about it on the Community https://community.apigee.com/articles/4188/unit-testing-code-coverage.html. If you have an specific example that I can integrate with Apgee Grunt, please submit a pull request. Cheers!

firefoxNX commented 9 years ago

@dzuluaga I tried adding istanbul code coverage but I keep getting "No coverage information was collected"

Running "storeCoverage" task
[D] Task source: /apiProxyName/node_modules/grunt-istanbul/tasks/istanbul.js
Fatal error: No coverage information was collected

I added the following to gruntfile.js

instrument: {
            files: ['node/**/*.js'],
            options: {
                lazy: true,
                basePath: 'tests/coverage/instrument/'
            }
        },
        storeCoverage: {
            options: {
                dir: 'tests/coverage/reports'
            }
        },
        makeReport: {
            src: 'tests/coverage/reports/**/*.json',
            options: {
                type: 'lcov',
                dir: 'tests/coverage/reports',
                print: 'detail'
            }
        }

grunt.registerTask('coverage', ['jshint', 'eslint', 'clean', 'instrument', 'executeTests', 'storeCoverage', 'makeReport']);

I see the instrumented code in /tests/coverage/instrument/node folder but the tests are not using the instrumented code. Any idea on how to do that?

dzuluaga commented 9 years ago

Thanks Vineet! Good starting point. It looks like you need an app_test.js file, which will generate an instance of the actual app. I haven't tried yet. But looking at the tutorial for testing Node.js Apps with Istambul, that seems to be the case. Check out this line. https://github.com/gregjopa/express-app-testing-demo/blob/master/test/route/app_test.js#L3 also checkout how Grunt calls tests from app_test.js here. For the full tutorial, check this out. Please let me know your findings. Feel free to open this to the community, we can leverage the community for it. BTW, are you coming to I Love APIs? email me :-)

firefoxNX commented 9 years ago

I did the requireHelper so good to know I am on the right track. I think I have another issue in executing the tests because the tests we have are "integration tests" and they require node app running. The tests make HTTP requests to the API endpoints. So I have to either

  1. figure out a way to run the node express.js app with the instrumented code OR
  2. use nock to mock the HTTP requests as shown in photo_model_test.js#L48

I tried to do (1) by creating symbolic links to node_modules, swagger.json inside the instrumented sources folder. I also had to change the path to swagger-tools swaggerRouterOptions controllers: './controllers'. Even after doing that I am seeing

Running "storeCoverage" task
[D] Task source: /<path>/grunt-istanbul/tasks/istanbul.js
Fatal error: No coverage information was collected