lundi 2 novembre 2015

Mocha unit tests and assertions in `for`loop

I'm working on Gulp plugin allowing to write JavaScript code using reserved words written in language different than English - its main doing is translating, yet it is not what I'm about.

Writing unit tests with Mocha and Should.js I noticed I can't use for loop like that:

 describe('Proper translating JavaScript PL into JavaScript', function () {
    var asserts = [
        {
            passed: '(tak || nie) && (prawda || fałsz)',
            expected: '(true || false) && (true || false)'
        },
        {
            passed: 'jeśli (tak) { jeżeli (prawda) {}}',
            expected: 'if (true) { if (true) {}}'
        },
        {
            passed: 'przez (zm i = 1; i <= 3; console.log(i++));',
            expected: 'for (var i = 1; i <= 3; console.log(i++));'
        },
        {
            passed: 'zm sum = funkcja (a, b) { zwróć a + b; };',
            expected: 'var sum = function (a, b) { return a + b; };'
        },
        {
            passed: 'generator snowball() { zm a = 1; dostarcz a++; }',
            expected: 'function* snowball() { var a = 1; yield a++; }'
        },
        {
            passed: 'lol',
            expected: 'zap'
        }
    ];

    for (var i = 0, l = asserts.length; i < l; i++) {
        var assert = asserts[i];
        var desc = 'should turn `' + assert.passed + '` into `' + assert.expected + '`';
        it(desc, function (done) {
            tester(assert.passed, assert.expected, done);
        });
    }
});

When I do something like above, one error is stated as failing as much times as number of assertions.

enter image description here

As you see we got 6 failings though there are right 2. Moreover, only one of them is mentioned here.

I don't want to double describe statements like below:

describe('Proper translating JavaScript PL into JavaScript', function () {
    var asserts = [
        {
            passed: '(tak || nie) && (prawda || fałsz)',
            expected: '(true || false) && (true || false)'
        },
        {
            passed: 'jeśli (tak) { jeżeli (prawda) {}}',
            expected: 'if (true) { if (true) {}}'
        },
        {
            passed: 'przez (zm i = 1; i <= 3; console.log(i++));',
            expected: 'for (var i = 1; i <= 3; console.log(i++));'
        },
        {
            passed: 'zm sum = funkcja (a, b) { zwróć a + b; };',
            expected: 'var sum = function (a, b) { return a + b; };'
        },
        {
            passed: 'generator snowball() { zm a = 1; dostarcz a++; }',
            expected: 'function* snowball() { var a = 1; yield a++; }'
        },
        {
            passed: 'lol',
            expected: 'zap'
        }
    ];

    it('should be kicking 1', function (done) {
        tester(asserts[0].passed, asserts[0].expected, done);
    });
    it('should be kicking 2', function (done) {
        tester(asserts[1].passed, asserts[1].expected, done);
    });
    it('should be kicking 3', function (done) {
        tester(asserts[2].passed, asserts[2].expected, done);
    });
    it('should be kicking 4', function (done) {
        tester(asserts[3].passed, asserts[3].expected, done);
    });
    it('should be kicking 5', function (done) {
        tester(asserts[4].passed, asserts[4].expected, done);
    });
    it('should be kicking 6', function (done) {
        tester(asserts[5].passed, asserts[5].expected, done);
    });
});

These should be kicking N don't matter - I've been to lazy to write stuff like:

it('should turn `(tak || nie) && (prawda || fałsz)` into `(true || false) && (true || false)`', function (done) {
    tester('(tak || nie) && (prawda || fałsz)', '(true || false) && (true || false)', done);
});

Anyway, doing standalone assertions tests go ok.

enter image description here

First 5 tests succeed in both cases, we don't look at them at all, here. Tests we focus on are these 6 put in asserts.

Doing tests in separated it() things go right. Doing tests in for loop, one fail makes asserts.length fails, additionally one fail makes other not seen.

Aucun commentaire:

Enregistrer un commentaire