Tuesday, 4 April 2017

Jest: Connect to the mongoose before testing begins

I start the server as a separate process, after which I run the tests on the Jest like this:

beforeAll(function (done) {
    server = fork('server/start_server.js');
    exec('node ../utility/new_user.js User Pass testing');
    setTimeout(() => {
        done();
    }, 2000);
});

afterAll(function () {
    fork('server/clean_db.js');
    server.kill();
});

test('Login', () => {
    login().then((data) => {
        expect(data.logged).toBeTruthy();
    })
});

The "start_server.js" file starts the http server and connects to the database (MongoDB through Mongoose). But the problem is that when the "Login" test send the request an error is returned - Error: connect ECONNREFUSED 127.0.0.1:8080. I assume that this is because the application does not have time to connect to the database. How to fix it without carrying out the database connection in the test file?



via Vladimir37

No comments:

Post a Comment