Mocha setup/teardown lifecycle problem?

91 views
Skip to first unread message

Julian May

unread,
Jan 15, 2016, 3:10:47 PM1/15/16
to ArangoDB
Moin!

I've been having problems with my first test-suite.
It seems the "beforeEach" of sub-scope descriptions s is executed before the parent root "it".
 
Vlad from the Mocha gg ( https://groups.google.com/forum/#!topic/mochajs/DFw9J9gAuMU ), hints that it might be because the collection instance executing asyncronously.

... But, as far as i understand, everything is syncronousl in foxx - right?

Reproducable on Windows 7,8 & 10 in version 2.7.2, 2.7.3 & 2.8.0.beta-3

/*global describe, it */
'use strict';
var expect = require('chai').expect;
var theCollection = applicationContext.collection('Whatever');

describe('top level describe', function () {
    beforeEach('top level beforeEach', function () {
        theCollection.truncate();
        theCollection.insert({wat:'inserted at top level'});
    });
    it('top level test', function () {
        var docs = theCollection.all().toArray();
        expect(docs.length).to.be.equal(1);
        expect(docs[0]).to.have.property('wat', 'inserted at top level');
    });

    describe('2nd level describe', function () {
        beforeEach('2nd level beforeEach', function () {
            theCollection.insert({ wat: 'inserted at 2nd level' });
        });

        it('2nd level test', function () {
            var docs = theCollection.all().toArray();
            expect(docs.length).to.be.equal(2);
            expect(docs[1]).to.have.property('wat', 'inserted at 2nd level');
        });
    });

    describe('other 2nd level describe', function () {
        beforeEach('other 2nd level beforeEach', function () {
            theCollection.truncate();
            theCollection.insert({ wat: 'inserted at other 2nd level' });
        });

        it('2nd level test', function () {
var docs = theCollection.all().toArray();
            expect(docs.length).to.be.equal(1);
            expect(docs[0]).to.have.property('wat', 'inserted at other 2nd level');
        });
    });
    
});


fails with the following:
Completed 3 tests in 17ms (0/3/0)
top level describe
  • top level test
    AssertionError: expected { Object (_id, _key, ...) } to have a property 'wat' of 'inserted at top level', but got 'inserted at other 2nd level'
        at Context. (C:\code\Arango\Foxx\_db\_system\pathtast\APP\test\example.js:14:33)
        at Function.global.DEFINE_MODULE.exports.nextTick (C:\ArangoDB\2.8.0-beta3\bin\../share/arangodb/js/common/bootstrap/modules/process.js:26:3)

Julian May

unread,
Jan 18, 2016, 3:11:41 AM1/18/16
to ArangoDB
Just confirmed that this is not isolated to windows - happens in docker-container as well (not sure if it helps)

/Julian

Alan Plum

unread,
Jan 19, 2016, 9:03:42 AM1/19/16
to ArangoDB
Hi Julian,

that sounds like a bug in our mocha integration. I'm surprised to see `nextTick` in the stack trace as the tests are indeed executed synchronously.

I'll look into it.


Cheers,

Alan

Julian May

unread,
Jan 21, 2016, 9:54:42 AM1/21/16
to ArangoDB
In the vein of problems with tests and life-cycle:
It seems that a reference to a repository will store (and return) documents even though the respective collection.truncate() has been called. 
If this is intentional, I think it merrits a by-line in the Foxx Testing cookbook at the least. Took me a (long) while refactoring my code over and over before identifying this.

Julian May

unread,
Jan 22, 2016, 3:57:08 AM1/22/16
to ArangoDB
.. Or maybe it's a transactional thing I'm having trouble with?
Not sure if it's even related or should be in another thread, but here goes:


I'm a javascript noob, so it's hard for me to filter what is strange/wrong and what is javascript (all the same at this point, one big confusion) - but I'm fairly certiain this is something related to transactional behavior in tests that i dont understand.

Example (code from extended repository function):

    var q = qb.let('n', qb.fn('DATE_DIFF')(qb.str(fromDate), qb.str(toDate), qb.str('d'), qb.bool(false)))
            .for('i').in(qb.expr('0..n-1'))
                .let('d', qb.fn('xpectra::availability::iso8601todatenumber')(qb.fn('DATE_ADD')(qb.str(fromDate), qb.ref('i'), qb.str('day'))))
                .upsert({ date: qb.ref('d'), roomType: qb.str(roomType)})
                    .insert({ date: qb.ref('d'), roomType: qb.str(roomType), modifier: qb.num(-1)})
                    .update({ modifier: qb.expr('OLD.modifier -1')})
                .in(this.collection);
                
    if(debug){
         var all = db._query(qb.for('x').in(this.collection).return('x')).toArray();
         throw new Error("RoomTypeModifications at this point (after decrementing):            " + JSON.stringify(all) + "        used this query:     " + q.toAQL());     
    }

gives us this:

Error: RoomTypeModifications at this point (after decrementing):            []        used this query:     LET n = DATE_DIFF("2016-01-27", "2016-01-29", "d", false) FOR i IN 0..n-1 LET d = xpectra::availability::iso8601todatenumber(DATE_ADD("2016-01-27", i, "day")) UPSERT {date: d, roomType: "TST"} INSERT {date: d, roomType: "TST", modifier: -1} UPDATE {modifier: OLD.modifier -1} IN availability_RoomTypeAvailabilitytModifier


BUT if I copy the AQL, paste it into AQL Editor and executes it here (with a truncated 'availability_RoomTypeAvailabilityModifier' collection), i get this:


SO there's a difference somewhere that i do not understand. 


Whatever it is, I need to understand it and compensate accordingly. Any help or pointers is (as always) much appreciated :)


Thanks


/Julian

Julian May

unread,
Jan 24, 2016, 10:54:00 AM1/24/16
to ArangoDB
Kindly disregard my last couple of posts - figured it out :]

Alan Plum

unread,
Feb 2, 2016, 6:39:53 AM2/2/16
to ArangoDB
Hi Julian,

could you clarify what you found out?

Cheers
Reply all
Reply to author
Forward
0 new messages