Home Async Jasmine test doesn't invoke beforeEach() before operating
Reply: 0

Async Jasmine test doesn't invoke beforeEach() before operating

user1332
1#
user1332 Published in July 16, 2018, 11:58 pm

I'm trying to code a test to my client-server modules. I need to run the server before the client is sending its requests, so I'm trying to use beforeEach, but the test fails before the server even started running.

My test:

'use strict';
const cp    = require('child_process');
const ip    = require('my-local-ip');
const utils = require('../util/utils');

describe('Server and client connectivity:', () => {

    let originalTimeout;
    let server;

    beforeEach(function() {
        server = cp.fork('Server/Server');
        originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
        jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
    });

    it('should transfer index.html file to client', (done) => {
        const client = cp.fork('Client/request', ['-t', ip(), '-p', 3300]);
        expect(utils.isFileExistsInDirectory('Client/', 'index.html')).toBe(true);
        done();
    });

    afterEach(function() {
        jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
    });

});

When I'm running manually first the server, and then the client with these commands, it works perfectly.

In the test sometimes the client request is sent before the server is listening.
How could it happen?
What am I doing wrong?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.308868 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO