hbenl / vscode-mocha-test-adapter

Mocha Test Adapter for the VS Code Test Explorer
MIT License
91 stars 31 forks source link

Suggestion: better source location determination #130

Closed Spown closed 4 years ago

Spown commented 4 years ago

So I have this stupid ugly way of writing tests with an automation, where I have a registry of config, cleanup, prepare and tests directives and one iterator with a single it() in it, like so:

      var  _sbxs = {
            'should fail on faulty page file': {
                loading: false,
                sandbox: '_faulty_sandbox_page',
                config: _.assign({db: false}, config),
            },
           //...
            'should load from the second static location, when first is missing': {
                sandbox: '_two_statics',
                config: _.merge({
                    dev: true,
                    db: false,
                    paths: {
                        static: ['./static_1', './static_2']
                    }
                }, config),
                test : function (p, done) {
                    assert.lengthOf(p.paths.static, 2)
                    request('/static/x.txt', function (res, data) {
                        assert.equal(res.statusCode, 200, 'status code from response');
                        assert.equal(data, 'x2', 'data from response');
                        done()
                    }, null, p, done)
                }
            }
        };
    beforeEach('preparing', function(done){
        var _this = this, _sbx = _sbxs[_this.currentTest.title];
       // does some preparation accordingly
    });

    _.each(_sbxs, function (_sbx, _sbxKey) {
        it( //the only one "it" in this test group
            _sbxKey,
            _.isFunction(_sbx.test) ?
                function (done) {
                    var _thisTest = this;
                    _thisTest.timeout(devTimeIncr*stdWait);
                    platformPromise
                    .then(function(p) {
                        var testRet = _sbx.test.apply(_thisTest, [p, done, _sbx]);
                        if (Q.isPromiseAlike(testRet)) {
                            testRet.then(()=>{done()}).catch(done)
                        }
                    })
                    .catch(done);
                } :
                ((done)=>{platformPromise[_sbx.loading===false?'catch':'then'](err=>{ done(); })})
        )
    })

    afterEach(function () {
        var _this = this, _sbx = _sbxs[_this.currentTest.title];
       // does some cleaup accordingly
    })

And it does work swimmingly with Explorer UI: image

However, when it comes to Show source function - it always jumps to where the only one "it" is. So since explorer/adapter clearly parses and reads the test titles regardless, maybe it could perform the jump to the title string declaration location, instead of where the it() consumes it?

Thank you.

hbenl commented 4 years ago

Add "mochaExplorer.monkeyPatch": false to your settings, that should do exactly what you want. The option was originally meant as a fallback if the location detection (which uses a monkey patch for Mocha - hence the option's name) creates problems for some reason. When the monkey patch is turned off, the adapter will look for the test's label in your source file instead and use that location if it finds one.