Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocha beforeEach and afterEach during testing

I have been trying to test my test server using mocha. This is the following code that I use, almost the same as one found in another similar post.

beforeEach(function(done) {
    // Setup
    console.log('test before function');
    ws.on('open', function() {
        console.log('worked...');
        done();
    });
    ws.on('close', function() {
        console.log('disconnected...');
    });
});

afterEach(function(done) {
    // Cleanup
    if(readyState) {
        console.log('disconnecting...');
        ws.close();
    } else {
        // There will not be a connection unless you have done() in beforeEach, socket.on('connect'...)
        console.log('no connection to break...');
    }
    done();
});

describe('WebSocket test', function() {
    //assert.equal(response.result, null, 'Successful Authentification');
});

the problem is that when I execute this draft, none of the console.log that is expected to be seen is visible on the command prompt. Can you please explain to me what am I doing wrong?

like image 542
zamponotyropita Avatar asked Mar 31 '14 13:03

zamponotyropita


2 Answers

Georgi is correct that you need an it call to specify a test but you don't need to have a top level describe in your file if you don't want to. You could replace your single describe with a bunch of it calls:

it("first", function () {
    // Whatever test.
});

it("second", function () {
    // Whatever other test.
});

This works very well if your test suite is small and composed of only one file.

If your test suite is bigger or spread among multiple files, I would very strongly suggest you put your beforeEach and afterEach together with your it inside the describe, unless you are absolutely positive that every single test in the suite needs the work done by beforeEach or afterEach. (I've written multiple test suites with Mocha and I've never had a beforeEach or afterEach that I needed to run for every single test.) Something like:

describe('WebSocket test', function() {
    beforeEach(function(done) {
        // ...
    });

    afterEach(function(done) {
       // ...
    });

    it('response should be null', function() {
        assert.equal(response.result, null, 'Successful Authentification');
    });
});

If you do not put your beforeEach and afterEach inside describe like this, then let's say you have one file to test web sockets and another file to test some database operations. The tests in the file that contains the database operation tests will also have your beforeEach and afterEach executed before and after them. Putting the beforeEach and afterEach inside the describe like shown above will ensure that they are performed only for your web socket tests.

like image 57
Louis Avatar answered Sep 30 '22 19:09

Louis


You have no tests in your example. If there are no tests to be run, then before and after hooks won't be invoked. Try adding a test like:

describe('WebSocket test', function() {
    it('should run test and invoke hooks', function(done) {
        assert.equal(1,1);
        done(); 
    });
});
like image 26
Georgi Avatar answered Sep 30 '22 19:09

Georgi