Skip to content

Commit

Permalink
Fix: Jest did not exit one second after the test run has completed ; …
Browse files Browse the repository at this point in the history
…update mobile dependencies
  • Loading branch information
aelassas committed Jan 8, 2025
1 parent acd9bd8 commit 5fa5990
Show file tree
Hide file tree
Showing 15 changed files with 116 additions and 152 deletions.
52 changes: 25 additions & 27 deletions api/__tests__/booking.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,36 +135,34 @@ beforeAll(async () => {
// Closing and cleaning the database connection after running the test suite
//
afterAll(async () => {
if (mongoose.connection.readyState) {
await testHelper.close()

// delete the supplier
await testHelper.deleteSupplier(SUPPLIER_ID)

// delete the location
await testHelper.deleteLocation(LOCATION_ID)

// delete the car
await Car.deleteMany({ _id: { $in: [CAR1_ID, CAR2_ID] } })

// delete drivers
// await User.deleteMany({ _id: { $in: [DRIVER1_ID, DRIVER2_ID] } })
const drivers = await User.find({ _id: { $in: [DRIVER1_ID, DRIVER2_ID] } })
expect(drivers.length).toBe(2)
for (const driver of drivers) {
if (driver.license) {
const license = path.join(env.CDN_LICENSES, driver.license)
if (await helper.exists(license)) {
await fs.unlink(license)
}
await testHelper.close()

// delete the supplier
await testHelper.deleteSupplier(SUPPLIER_ID)

// delete the location
await testHelper.deleteLocation(LOCATION_ID)

// delete the car
await Car.deleteMany({ _id: { $in: [CAR1_ID, CAR2_ID] } })

// delete drivers
// await User.deleteMany({ _id: { $in: [DRIVER1_ID, DRIVER2_ID] } })
const drivers = await User.find({ _id: { $in: [DRIVER1_ID, DRIVER2_ID] } })
expect(drivers.length).toBe(2)
for (const driver of drivers) {
if (driver.license) {
const license = path.join(env.CDN_LICENSES, driver.license)
if (await helper.exists(license)) {
await fs.unlink(license)
}
await driver.deleteOne()
}
await Notification.deleteMany({ user: { $in: [DRIVER1_ID, DRIVER2_ID] } })
await NotificationCounter.deleteMany({ user: { $in: [DRIVER1_ID, DRIVER2_ID] } })

await databaseHelper.close()
await driver.deleteOne()
}
await Notification.deleteMany({ user: { $in: [DRIVER1_ID, DRIVER2_ID] } })
await NotificationCounter.deleteMany({ user: { $in: [DRIVER1_ID, DRIVER2_ID] } })

await databaseHelper.close()
})

//
Expand Down
19 changes: 8 additions & 11 deletions api/__tests__/car.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 bookcarsTypes from ':bookcars-types'
import * as databaseHelper from '../src/common/databaseHelper'
import app from '../src/app'
Expand Down Expand Up @@ -54,19 +53,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 suppliers
await testHelper.deleteSupplier(SUPPLIER1_ID)
await testHelper.deleteSupplier(SUPPLIER2_ID)
// delete suppliers
await testHelper.deleteSupplier(SUPPLIER1_ID)
await testHelper.deleteSupplier(SUPPLIER2_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()
})

//
Expand Down
7 changes: 2 additions & 5 deletions api/__tests__/country.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'dotenv/config'
import request from 'supertest'
import { nanoid } from 'nanoid'
import mongoose from 'mongoose'
import * as bookcarsTypes from ':bookcars-types'
import app from '../src/app'
import * as databaseHelper from '../src/common/databaseHelper'
Expand Down Expand Up @@ -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()
})

//
Expand Down
7 changes: 2 additions & 5 deletions api/__tests__/globalSetup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dotenv/config'
import bcrypt from 'bcrypt'
import mongoose from 'mongoose'
import * as bookcarsTypes from ':bookcars-types'
import * as env from '../src/config/env.config'
import * as logger from '../src/common/logger'
Expand Down Expand Up @@ -28,10 +27,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)
Expand Down
5 changes: 1 addition & 4 deletions api/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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<void> => {
Expand Down
8 changes: 3 additions & 5 deletions api/__tests__/location.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import url from 'url'
import path from 'path'
import fs from 'node:fs/promises'
import { nanoid } from 'nanoid'
import mongoose, { FlattenMaps } from 'mongoose'
import { FlattenMaps } from 'mongoose'
import * as bookcarsTypes from ':bookcars-types'
import app from '../src/app'
import * as databaseHelper from '../src/common/databaseHelper'
Expand Down Expand Up @@ -71,10 +71,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()
})

//
Expand Down
7 changes: 2 additions & 5 deletions api/__tests__/middleware.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dotenv/config'
import request from 'supertest'
import mongoose from 'mongoose'
import * as bookcarsTypes from ':bookcars-types'
import * as databaseHelper from '../src/common/databaseHelper'
import app from '../src/app'
Expand Down Expand Up @@ -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', () => {
Expand Down
5 changes: 1 addition & 4 deletions api/__tests__/miscellaneous.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'dotenv/config'
import mongoose from 'mongoose'
import * as bookcarsTypes from ':bookcars-types'
import * as env from '../src/config/env.config'
import * as databaseHelper from '../src/common/databaseHelper'
Expand All @@ -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()
})

const ADDITIONAL_DRIVER: bookcarsTypes.AdditionalDriver = {
Expand Down
15 changes: 6 additions & 9 deletions api/__tests__/notification.test.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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.deleteSupplier(SUPPLIER_ID)
await testHelper.deleteSupplier(SUPPLIER_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()
})

//
Expand Down
5 changes: 1 addition & 4 deletions api/__tests__/stripe.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dotenv/config'
import request from 'supertest'
import mongoose from 'mongoose'
import * as bookcarsTypes from ':bookcars-types'
import app from '../src/app'
import * as databaseHelper from '../src/common/databaseHelper'
Expand All @@ -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', () => {
Expand Down
17 changes: 7 additions & 10 deletions api/__tests__/supplier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 bookcarsTypes from ':bookcars-types'
import * as databaseHelper from '../src/common/databaseHelper'
import * as testHelper from './testHelper'
Expand Down Expand Up @@ -116,18 +115,16 @@ 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 suppliers
await testHelper.deleteSupplier(SUPPLIER1_ID)
await testHelper.deleteSupplier(SUPPLIER2_ID)
// delete suppliers
await testHelper.deleteSupplier(SUPPLIER1_ID)
await testHelper.deleteSupplier(SUPPLIER2_ID)

// delete cars
await Car.deleteMany({ _id: { $in: [CAR1_ID, CAR2_ID] } })
// delete cars
await Car.deleteMany({ _id: { $in: [CAR1_ID, CAR2_ID] } })

await databaseHelper.close()
}
await databaseHelper.close()
})

//
Expand Down
9 changes: 3 additions & 6 deletions api/__tests__/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 bookcarsTypes from ':bookcars-types'
import app from '../src/app'
import * as databaseHelper from '../src/common/databaseHelper'
Expand Down Expand Up @@ -58,13 +57,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()
})

//
Expand Down
4 changes: 1 addition & 3 deletions api/src/common/databaseHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ export const connect = async (uri: string, ssl: boolean, debug: boolean): Promis
* @returns {Promise<void>}
*/
export const close = async (force: boolean = false): Promise<void> => {
if (mongoose.connection.readyState) {
await mongoose.connection.close(force)
}
await mongoose.connection.close(force)
}

/**
Expand Down
Loading

0 comments on commit 5fa5990

Please sign in to comment.