Friday, 19 July 2019

Setting up jest for express

I am trying to setup Jest with express application.

Here is my jest.config.js file.

module.exports = {
    verbose: true,
    globalSetup: "<rootDir>/test/setup.js",
    globalTeardown: "<rootDir>/test/teardown.js",
    collectCoverage: true,
};

Now when I try to start server in setup.js I need to call a function startServer.

which is like:

exports.startServer = function(){
.
.
   yield httpLib.startHttpServer(envProperties.port);
.
.
}

But when I try to run jest, it throws me errors like config is not defiend etc, and server is never started. I want a basic setup to process with jest where running jest, would start the server and stop it after executing all the tests.

EDIT: My second issue is running testcases in the order they are:

ex. I am trying like:

describe('tests', () => {

    it('api1, async () => {
        const data = await request(server)
            .post('/api2')
            .send({
                name: 'xyz',
                age : 22
            })
        if (data.text) {
            data.text = JSON.parse(data.text);
        }
        expect(data.text.data).toEqual({
            xyz : 34
        });
    })

    it('api2, async () => {
        const data = await request(server)
            .post('/api1')
            .send({
                name: 'xyz',
                age : 22
            })
        if (data.text) {
            data.text = JSON.parse(data.text);
        }
        expect(data.text.data).toEqual({
            xyz : 34
        });
    })
    it('api3, async () => {
        const data = await request(server)
            .post('/api3')
            .send({
                name: 'xyz',
                age : 22
            })
        if (data.text) {
            data.text = JSON.parse(data.text);
        }
        expect(data.text.data).toEqual({
            xyz : 34
        });
    })
})

The order of execution should be api1 => api2 => api3. As api2 might have dependency on. api1 . I tried --runInBand which dont seem to work here,and testcases are run parallely.



from Setting up jest for express

No comments:

Post a Comment