Using Mocha to run unit test for Node.js not working as expected

I am using mocha to run a unit test on my Node.js server code which inserts a user address, reads it and validates data and then deletes the address.

it('Create User Address', function(done) {
    var userId = 'testID';
    var address = '123 Test Cres';
    var city = 'Toronto';
    var region = 'Ontario';
    var country = 'Canada';
    var startDate = new Date().getTime() / 1000;
    var responseWrite = {};
    var responseRead = {};
    request('http://127.0.0.1:5003/cloud-medic/us-central1/app/createUserProfile' + '?userId=' + userId + '&address='
            + address + '&city=' + city + '&region=' + region + '&country=' + country + '&startDate=' + startDate, async function(error, response, body) {
        console.log("Getting response after write");
        responseWrite = await JSON.parse(body);
        console.log(responseWrite);
        expect(responseWrite.userProfile.userId).to.equal(userId);

        request('http://127.0.0.1:5003/cloud-medic/us-central1/app/getUserProfile' + '?id=' + responseWrite.userProfile.id, async function(error, response, body) {
                console.log("Getting response id after write!!!!!!!!!!!!!!!!!!!!!!!!");
                console.log(responseWrite);
                responseRead = await JSON.parse(body);
                console.log("Getting response id after READ!!!!!!!!!!!!!!!!!!!!!!!!");
                console.log(responseRead.userProfile);
                expect(responseRead.userProfile.userId).to.equal(userId);
                expect(responseRead.address).to.equal(address);
                expect(responseRead.city).to.equal(city);
                expect(responseRead.region).to.equal(region);
                expect(responseRead.country).to.equal(country);
                //console.log(responseRead.startDate);

            });
         done();
    });


    // tear down delete user profile created for test
    after(function () {
        request.delete({url:'http://127.0.0.1:5003/cloud-medic/us-central1/app/deleteUserProfile',
                 form: {id: responseWrite.userProfile.id}}, async function(err,httpResponse,body){
                 console.log("DELETEING PROFILE" + responseWrite.userProfile.id);
                 console.log(body);
                 responseDelete = await JSON.parse(body);
                 expect(responseDelete.userProfileStatus).to.equal(200);
                 //done();

         });
      });


});

This code works however it doesn’t seem to execute the expect assertations on the profile address (userId, address, city, region, country). So if I change the userId to “THROW ERROR” it throws an unexpected error.

expect(responseWrite.userProfile.userId).to.equal("THROW ERROR");

The error is based on a timeout which is thrown when I ran the unit tests:

 1) Create User Address:
     Error: Timeout of 10000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/Users/Projects/CloudMedic/test/test-user.js)

I run the tests using at 10000ms timeout.
npm test
Any idea on what is causing this weird behaviour? Thanks for your help.

3

I moved away from the callback using done. Instead of using the request library I am now using the node.js fetch command and promises. Here is the code that now works for me:

it('Create User Address', async function() {
    var userId = 'zcU7Q09GwsdvtKrLaR8M';
    var address = '123 Test';
    var city = 'Toronto';
    var region = 'Ontario';
    var country = 'Canada';
    var startDate = new Date().getTime();
    var responseWrite = {};
    var responseRead = {};
    await fetch('http://127.0.0.1:5003/cloud-medic/us-central1/app/createUserProfile' + '?userId=' + userId + '&address='
            + address + '&city=' + city + '&region=' + region + '&country=' + country + '&startDate=' + startDate, {
            method: "GET"
            }).then(response => response.json())
               .then(async response => {
                console.log("Getting response after write");
                responseWrite = response;
                console.log(responseWrite.userProfile.id);
                expect(responseWrite.userProfile.userId).to.equal(userId);

                await fetch('http://127.0.0.1:5003/cloud-medic/us-central1/app/getUserProfile' + '?id=' + responseWrite.userProfile.id, {
                        method: "GET"
                        }).then(response => response.json())
                           .then(response => {
                            responseRead = response;
                            console.log("Getting response id after READ!!!!!!!!!!!!!!!!!!!!!!!!");
                            console.log(responseRead.userProfile);
                            expect(responseRead.userProfile.userId).to.equal(userId);
                            expect(responseRead.userProfile.address).to.equal(address);
                            expect(responseRead.userProfile.city).to.equal(city);
                            expect(responseRead.userProfile.region).to.equal(region);
                            expect(responseRead.userProfile.country).to.equal(country);
                            expect(responseRead.userProfile.startDate._seconds).to.equal(Math.trunc(startDate/1000));
                    });
            });

    // tear down delete user profile created for test
   after(async function () {
        await fetch('http://127.0.0.1:5003/cloud-medic/us-central1/app/deleteUserProfile',
                { method: 'DELETE',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ id: responseWrite.userProfile.id})
             }).then(response => response.json())
               .then(response => {
                 responseDelete = response;
                 expect(responseDelete.userProfileStatus).to.equal(200);

            });
      });

 

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật