gruntjsjshintgrunt-contrib-jshint

jshint exported directive not working


I've setup a mocha task in my gruntfile which loads a few test libraries before the tests start. Here is what it looks like:

    mochaTest: {
        unit: {
            options: {
                reporter: 'spec',
                require: function(){
                    var chai = require('chai');
                    var chaiAsPromised = require('chai-as-promised');
                    chai.use(chaiAsPromised);
                    /* exported assert, expect */
                    var assert = chai.assert;
                    var expect = chai.expect;
                    chai.should();
                }
            },
            src: ['backend/test/**/*.js']
        }
    },

so jshint complains that I'm not using assert and expect in the gruntfile. I've tried adding exported to the .jshintrc file, to the jshint grunt task and even the inline directive but jshint don't seem to care. I've just upgraded to the latest version to be on the safe side but still no luck.

I can use the // jshint ignore:line at the moment to get by but I'd like to get this working with the /*exported assert, expect */ directive if possible, as I think it is more understandable.


Solution

  • Unfortunately I don't have enough reputation to comment on an answer directly. However to add something to the answer from James and the subsequent comment from Reno. I believe that by adding the 'var' declaration to your gruntfile you make the variables local to that file and as such they cannot be accessed in tests. As I see it you have to configure JSHint to allow these globals if you want to use them as globals (which is what you are doing so that makes sense). You can't just workaround it by making them local

    As such I think the following would work the way you want (I haven't had chance to test it though)

    /* exported assert, expect */
    
    ...
    
        mochaTest: {
        unit: {
            options: {
                reporter: 'spec',
                require: function(){
                    var chai = require('chai');
                    var chaiAsPromised = require('chai-as-promised');
                    chai.use(chaiAsPromised);
                    assert = chai.assert;
                    expect = chai.expect;
                    chai.should();
                }
            },
            src: ['backend/test/**/*.js']
        }
    },
    

    The JSHint documentation mentions that the exported directive should be used with the unused option but i'm not sure what it means by that (maybe it is the reason you ran into issues with exported)