From 3da880fceb1342db23a94ef079ba0bab28f30b42 Mon Sep 17 00:00:00 2001 From: Akram El Assas Date: Tue, 7 Jan 2025 19:53:28 +0100 Subject: [PATCH 1/4] Update README.md --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 547dd133..5933bb82 100644 --- a/README.md +++ b/README.md @@ -112,4 +112,3 @@ You can also install the Android App by downloading the APK and installing it on ## License Movin' In is [MIT licensed](https://github.com/aelassas/movinin/blob/main/LICENSE). - From 567e4574f882de8091d100455dc8d379bda82120 Mon Sep 17 00:00:00 2001 From: aelassas Date: Tue, 7 Jan 2025 20:10:12 +0100 Subject: [PATCH 2/4] Fix: Jest did not exit one second after the test run has completed --- api/__tests__/agency.test.ts | 12 +++++------- api/__tests__/booking.test.ts | 27 ++++++++++++--------------- api/__tests__/country.test.ts | 7 ++----- api/__tests__/globalSetup.ts | 7 ++----- api/__tests__/index.test.ts | 5 +---- api/__tests__/location.test.ts | 7 ++----- api/__tests__/middleware.test.ts | 7 ++----- api/__tests__/miscellaneous.test.ts | 5 +---- api/__tests__/notification.test.ts | 15 ++++++--------- api/__tests__/property.test.ts | 19 ++++++++----------- api/__tests__/stripe.test.ts | 5 +---- api/__tests__/user.test.ts | 9 +++------ api/src/common/databaseHelper.ts | 29 ++++++++++++++--------------- 13 files changed, 59 insertions(+), 95 deletions(-) diff --git a/api/__tests__/agency.test.ts b/api/__tests__/agency.test.ts index 960e808c..2644ffc5 100644 --- a/api/__tests__/agency.test.ts +++ b/api/__tests__/agency.test.ts @@ -43,15 +43,13 @@ beforeAll(async () => { // Closing and cleaning the database connection after running the test suite // afterAll(async () => { - if (mongoose.connection.readyState) { - await testHelper.close() + await testHelper.close() - // delete agencies - await testHelper.deleteAgency(AGENCY1_ID) - await testHelper.deleteAgency(AGENCY2_ID) + // delete agencies + await testHelper.deleteAgency(AGENCY1_ID) + await testHelper.deleteAgency(AGENCY2_ID) - await databaseHelper.close() - } + await databaseHelper.close() }) // diff --git a/api/__tests__/booking.test.ts b/api/__tests__/booking.test.ts index f8835d31..06c6ac2a 100644 --- a/api/__tests__/booking.test.ts +++ b/api/__tests__/booking.test.ts @@ -1,7 +1,6 @@ import 'dotenv/config' import request from 'supertest' import { nanoid } from 'nanoid' -import mongoose from 'mongoose' import * as movininTypes from ':movinin-types' import app from '../src/app' import * as databaseHelper from '../src/common/databaseHelper' @@ -95,25 +94,23 @@ beforeAll(async () => { // Closing and cleaning the database connection after running the test suite // afterAll(async () => { - if (mongoose.connection.readyState) { - await testHelper.close() + await testHelper.close() - // delete the agency - await testHelper.deleteAgency(AGENCY_ID) + // delete the agency + await testHelper.deleteAgency(AGENCY_ID) - // delete the location - await testHelper.deleteLocation(LOCATION_ID) + // delete the location + await testHelper.deleteLocation(LOCATION_ID) - // delete the property - await Property.deleteMany({ _id: { $in: [PROPERTY1_ID, PROPERTY2_ID] } }) + // delete the property + await Property.deleteMany({ _id: { $in: [PROPERTY1_ID, PROPERTY2_ID] } }) - // delete renters - await User.deleteMany({ _id: { $in: [RENTER1_ID, RENTER2_ID] } }) - await Notification.deleteMany({ user: { $in: [RENTER1_ID, RENTER2_ID] } }) - await NotificationCounter.deleteMany({ user: { $in: [RENTER1_ID, RENTER2_ID] } }) + // delete renters + await User.deleteMany({ _id: { $in: [RENTER1_ID, RENTER2_ID] } }) + await Notification.deleteMany({ user: { $in: [RENTER1_ID, RENTER2_ID] } }) + await NotificationCounter.deleteMany({ user: { $in: [RENTER1_ID, RENTER2_ID] } }) - await databaseHelper.close() - } + await databaseHelper.close() }) // diff --git a/api/__tests__/country.test.ts b/api/__tests__/country.test.ts index 9241b9da..d6589bcd 100644 --- a/api/__tests__/country.test.ts +++ b/api/__tests__/country.test.ts @@ -1,7 +1,6 @@ import 'dotenv/config' import request from 'supertest' import { nanoid } from 'nanoid' -import mongoose from 'mongoose' import * as movininTypes from ':movinin-types' import app from '../src/app' import * as databaseHelper from '../src/common/databaseHelper' @@ -39,10 +38,8 @@ beforeAll(async () => { // Closing and cleaning the database connection after running the test suite // afterAll(async () => { - if (mongoose.connection.readyState) { - await testHelper.close() - await databaseHelper.close() - } + await testHelper.close() + await databaseHelper.close() }) // diff --git a/api/__tests__/globalSetup.ts b/api/__tests__/globalSetup.ts index 20e0cb03..f21da5b9 100644 --- a/api/__tests__/globalSetup.ts +++ b/api/__tests__/globalSetup.ts @@ -1,6 +1,5 @@ import 'dotenv/config' import bcrypt from 'bcrypt' -import mongoose from 'mongoose' import * as movininTypes from ':movinin-types' import * as env from '../src/config/env.config' import * as logger from '../src/common/logger' @@ -26,10 +25,8 @@ export default async function globalSetup() { await admin.save() logger.info('globalSetup: Admin user created:', admin.id) } - if (mongoose.connection.readyState) { - await databaseHelper.close() - logger.info('Database connection closed') - } + await databaseHelper.close() + logger.info('Database connection closed') } } catch (err) { logger.error('Error while running global setup', err) diff --git a/api/__tests__/index.test.ts b/api/__tests__/index.test.ts index df2cc47d..544404e5 100644 --- a/api/__tests__/index.test.ts +++ b/api/__tests__/index.test.ts @@ -1,5 +1,4 @@ import 'dotenv/config' -import mongoose from 'mongoose' import * as databaseHelper from '../src/common/databaseHelper' import * as testHelper from './testHelper' import * as env from '../src/config/env.config' @@ -21,9 +20,7 @@ beforeAll(async () => { // Closing and cleaning the database connection after running the test suite // afterAll(async () => { - if (mongoose.connection.readyState) { - await databaseHelper.close() - } + await databaseHelper.close() }) const createBookingIndex = async (expireAfterSeconds: number): Promise => { diff --git a/api/__tests__/location.test.ts b/api/__tests__/location.test.ts index 4ab9f39a..f4763a26 100644 --- a/api/__tests__/location.test.ts +++ b/api/__tests__/location.test.ts @@ -4,7 +4,6 @@ import url from 'url' import path from 'path' import fs from 'node:fs/promises' import { nanoid } from 'nanoid' -import mongoose from 'mongoose' import * as movininTypes from ':movinin-types' import app from '../src/app' import * as databaseHelper from '../src/common/databaseHelper' @@ -71,10 +70,8 @@ afterAll(async () => { await LocationValue.deleteMany({ _id: { $in: [countryValue1Id, countryValue2Id] } }) await Country.deleteOne({ _id: countryId }) - if (mongoose.connection.readyState) { - await testHelper.close() - await databaseHelper.close() - } + await testHelper.close() + await databaseHelper.close() }) // diff --git a/api/__tests__/middleware.test.ts b/api/__tests__/middleware.test.ts index 7ce2a7c9..093858d3 100644 --- a/api/__tests__/middleware.test.ts +++ b/api/__tests__/middleware.test.ts @@ -1,6 +1,5 @@ import 'dotenv/config' import request from 'supertest' -import mongoose from 'mongoose' import * as movininTypes from ':movinin-types' import * as databaseHelper from '../src/common/databaseHelper' import app from '../src/app' @@ -28,10 +27,8 @@ beforeAll(async () => { // Closing and cleaning the database connection after running the test suite // afterAll(async () => { - if (mongoose.connection.readyState) { - await testHelper.close() - await databaseHelper.close() - } + await testHelper.close() + await databaseHelper.close() }) describe('POST /api/sign-in/backend', () => { diff --git a/api/__tests__/miscellaneous.test.ts b/api/__tests__/miscellaneous.test.ts index 84c20fd3..0991c100 100644 --- a/api/__tests__/miscellaneous.test.ts +++ b/api/__tests__/miscellaneous.test.ts @@ -1,5 +1,4 @@ import 'dotenv/config' -import mongoose from 'mongoose' import * as movininTypes from ':movinin-types' import * as env from '../src/config/env.config' import * as databaseHelper from '../src/common/databaseHelper' @@ -22,9 +21,7 @@ beforeAll(async () => { // Closing and cleaning the database connection after running the test suite // afterAll(async () => { - if (mongoose.connection.readyState) { - await databaseHelper.close() - } + await databaseHelper.close() }) describe('Test User phone validation', () => { diff --git a/api/__tests__/notification.test.ts b/api/__tests__/notification.test.ts index d2917484..f396e840 100644 --- a/api/__tests__/notification.test.ts +++ b/api/__tests__/notification.test.ts @@ -1,6 +1,5 @@ import 'dotenv/config' import request from 'supertest' -import mongoose from 'mongoose' import * as databaseHelper from '../src/common/databaseHelper' import * as testHelper from './testHelper' import Notification from '../src/models/Notification' @@ -42,17 +41,15 @@ beforeAll(async () => { // Closing and cleaning the database connection after running the test suite // afterAll(async () => { - if (mongoose.connection.readyState) { - await testHelper.close() + await testHelper.close() - await testHelper.deleteAgency(AGENCY_ID) + await testHelper.deleteAgency(AGENCY_ID) - // clear admin user notifications and notification counter - await Notification.deleteMany({ user: ADMIN_USER_ID }) - await NotificationCounter.deleteOne({ user: ADMIN_USER_ID }) + // clear admin user notifications and notification counter + await Notification.deleteMany({ user: ADMIN_USER_ID }) + await NotificationCounter.deleteOne({ user: ADMIN_USER_ID }) - await databaseHelper.close() - } + await databaseHelper.close() }) // diff --git a/api/__tests__/property.test.ts b/api/__tests__/property.test.ts index 540b1139..e1670a46 100644 --- a/api/__tests__/property.test.ts +++ b/api/__tests__/property.test.ts @@ -4,7 +4,6 @@ import url from 'url' import path from 'path' import fs from 'node:fs/promises' import { nanoid } from 'nanoid' -import mongoose from 'mongoose' import * as movininTypes from ':movinin-types' import * as databaseHelper from '../src/common/databaseHelper' import app from '../src/app' @@ -61,19 +60,17 @@ beforeAll(async () => { // Closing and cleaning the database connection after running the test suite // afterAll(async () => { - if (mongoose.connection.readyState) { - await testHelper.close() + await testHelper.close() - // delete agencies - await testHelper.deleteAgency(AGENCY1_ID) - await testHelper.deleteAgency(AGENCY2_ID) + // delete agencies + await testHelper.deleteAgency(AGENCY1_ID) + await testHelper.deleteAgency(AGENCY2_ID) - // delete locations - await testHelper.deleteLocation(LOCATION1_ID) - await testHelper.deleteLocation(LOCATION2_ID) + // delete locations + await testHelper.deleteLocation(LOCATION1_ID) + await testHelper.deleteLocation(LOCATION2_ID) - await databaseHelper.close() - } + await databaseHelper.close() }) // diff --git a/api/__tests__/stripe.test.ts b/api/__tests__/stripe.test.ts index bc9d8c95..e4a3aff4 100644 --- a/api/__tests__/stripe.test.ts +++ b/api/__tests__/stripe.test.ts @@ -1,6 +1,5 @@ import 'dotenv/config' import request from 'supertest' -import mongoose from 'mongoose' import * as movininTypes from ':movinin-types' import app from '../src/app' import * as databaseHelper from '../src/common/databaseHelper' @@ -23,9 +22,7 @@ beforeAll(async () => { // Closing and cleaning the database connection after running the test suite // afterAll(async () => { - if (mongoose.connection.readyState) { - await databaseHelper.close() - } + await databaseHelper.close() }) describe('POST /api/create-checkout-session', () => { diff --git a/api/__tests__/user.test.ts b/api/__tests__/user.test.ts index 5627432e..ab7ca9fa 100644 --- a/api/__tests__/user.test.ts +++ b/api/__tests__/user.test.ts @@ -4,7 +4,6 @@ import url from 'url' import path from 'path' import fs from 'node:fs/promises' import { nanoid } from 'nanoid' -import mongoose from 'mongoose' import * as movininTypes from ':movinin-types' import app from '../src/app' import * as databaseHelper from '../src/common/databaseHelper' @@ -49,13 +48,11 @@ beforeAll(async () => { // Closing and cleaning the database connection after running the test suite // afterAll(async () => { - if (mongoose.connection.readyState) { - await testHelper.close() + await testHelper.close() - await Token.deleteMany({ user: { $in: [ADMIN_ID] } }) + await Token.deleteMany({ user: { $in: [ADMIN_ID] } }) - await databaseHelper.close() - } + await databaseHelper.close() }) // diff --git a/api/src/common/databaseHelper.ts b/api/src/common/databaseHelper.ts index 4ee69568..f50e433e 100644 --- a/api/src/common/databaseHelper.ts +++ b/api/src/common/databaseHelper.ts @@ -55,9 +55,10 @@ export const connect = async (uri: string, ssl: boolean, debug: boolean): Promis * @returns {Promise} */ export const close = async (force: boolean = false): Promise => { - if (mongoose.connection.readyState) { - await mongoose.connection.close(force) - } + // if (mongoose.connection.readyState) { + // await mongoose.connection.close(force) + // } + await mongoose.connection.close(force) } /** * Initialize locations. @@ -222,18 +223,16 @@ const createCollection = async(model: Model) => { */ export const initialize = async (): Promise => { try { - if (mongoose.connection.readyState) { - await createCollection(Booking) - await createCollection(LocationValue) - await createCollection(Country) - await createCollection(Location) - await createCollection(Notification) - await createCollection(NotificationCounter) - await createCollection(Property) - await createCollection(PushToken) - await createCollection(Token) - await createCollection(User) - } + await createCollection(Booking) + await createCollection(LocationValue) + await createCollection(Country) + await createCollection(Location) + await createCollection(Notification) + await createCollection(NotificationCounter) + await createCollection(Property) + await createCollection(PushToken) + await createCollection(Token) + await createCollection(User) // // Update Booking TTL index if configuration changes From 272e9c4c320fe7729c2bb9187b9afcb950bcd40b Mon Sep 17 00:00:00 2001 From: aelassas Date: Tue, 7 Jan 2025 20:11:28 +0100 Subject: [PATCH 3/4] Fix: agency.test.ts(7,1): error TS6133: 'mongoose' is declared but its value is never read --- api/__tests__/agency.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/api/__tests__/agency.test.ts b/api/__tests__/agency.test.ts index 2644ffc5..7a6dce32 100644 --- a/api/__tests__/agency.test.ts +++ b/api/__tests__/agency.test.ts @@ -4,7 +4,6 @@ import url from 'url' import path from 'path' import fs from 'node:fs/promises' import { nanoid } from 'nanoid' -import mongoose from 'mongoose' import * as movininTypes from ':movinin-types' import * as databaseHelper from '../src/common/databaseHelper' import * as testHelper from './testHelper' From e3a01474a148722dc396588ba030e677ccff4b95 Mon Sep 17 00:00:00 2001 From: aelassas Date: Tue, 7 Jan 2025 20:39:06 +0100 Subject: [PATCH 4/4] Update databaseHelper.ts --- api/src/common/databaseHelper.ts | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/api/src/common/databaseHelper.ts b/api/src/common/databaseHelper.ts index f50e433e..6094a4fa 100644 --- a/api/src/common/databaseHelper.ts +++ b/api/src/common/databaseHelper.ts @@ -223,16 +223,18 @@ const createCollection = async(model: Model) => { */ export const initialize = async (): Promise => { try { - await createCollection(Booking) - await createCollection(LocationValue) - await createCollection(Country) - await createCollection(Location) - await createCollection(Notification) - await createCollection(NotificationCounter) - await createCollection(Property) - await createCollection(PushToken) - await createCollection(Token) - await createCollection(User) + if (mongoose.connection.readyState) { + await createCollection(Booking) + await createCollection(LocationValue) + await createCollection(Country) + await createCollection(Location) + await createCollection(Notification) + await createCollection(NotificationCounter) + await createCollection(Property) + await createCollection(PushToken) + await createCollection(Token) + await createCollection(User) + } // // Update Booking TTL index if configuration changes