Skip to content

Commit

Permalink
test(database): parallelize e2e tests with multiple databases (#582)
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinezanardi committed Oct 16, 2023
1 parent 2dd1630 commit d95b16a
Show file tree
Hide file tree
Showing 8 changed files with 6,215 additions and 5,640 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ Before testing, you must follow the **[installation steps](#installation)**.
Then, run one of the following commands :

```bash
# Assure you started test Docker containers
# Assure you started test Docker containers (4 databases are created to parallelize tests)
npm run docker:test:start

# Run unit tests with coverage
Expand Down
3 changes: 2 additions & 1 deletion config/cucumber/cucumber.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"junit:tests/acceptance/reports/junit.xml",
"json:tests/acceptance/reports/report.json",
"message:tests/acceptance/reports/message.json"
]
],
"parallel": 4
}
}
3 changes: 1 addition & 2 deletions config/jest/jest-e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ const JEST_E2E_CONFIG: Config = {
transform: { "^.+\\.(t|j)s$": "ts-jest" },
resetMocks: true,
restoreMocks: true,
maxWorkers: 4,
clearMocks: true,
maxConcurrency: 1,
maxWorkers: 1,
setupFilesAfterEnv: ["jest-extended/all"],
coverageReporters: ["clover", "json", "lcov", "text", "text-summary"],
collectCoverageFrom: [
Expand Down
3 changes: 1 addition & 2 deletions config/jest/jest-global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ const JEST_GLOBAL_CONFIG: Config = {
resetMocks: true,
restoreMocks: true,
clearMocks: true,
maxConcurrency: 1,
maxWorkers: 1,
maxWorkers: 4,
setupFiles: ["<rootDir>/tests/global-setup.ts"],
setupFilesAfterEnv: ["jest-extended/all"],
coverageReporters: ["clover", "json-summary", "lcov", "text", "text-summary"],
Expand Down
47 changes: 43 additions & 4 deletions docker/werewolves-assistant-test/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: "3.8"
services:
mongo:
container_name: "mongo-test"
mongo-1:
container_name: "mongo-test-1"
image: mongo:latest
environment:
MONGO_INITDB_ROOT_USERNAME: test
Expand All @@ -10,7 +10,46 @@ services:
ports:
- "27018:27017"
volumes:
- mongodb_data_container:/data/db
- mongodb_data_container_1:/data/db

mongo-2:
container_name: "mongo-test-2"
image: mongo:latest
environment:
MONGO_INITDB_ROOT_USERNAME: test
MONGO_INITDB_ROOT_PASSWORD: speed_burger_is_the_best
MONGO_INITDB_DATABASE: werewolves-assistant
ports:
- "27020:27017"
volumes:
- mongodb_data_container_2:/data/db

mongo-3:
container_name: "mongo-test-3"
image: mongo:latest
environment:
MONGO_INITDB_ROOT_USERNAME: test
MONGO_INITDB_ROOT_PASSWORD: speed_burger_is_the_best
MONGO_INITDB_DATABASE: werewolves-assistant
ports:
- "27022:27017"
volumes:
- mongodb_data_container_3:/data/db

mongo-4:
container_name: "mongo-test-4"
image: mongo:latest
environment:
MONGO_INITDB_ROOT_USERNAME: test
MONGO_INITDB_ROOT_PASSWORD: speed_burger_is_the_best
MONGO_INITDB_DATABASE: werewolves-assistant
ports:
- "27024:27017"
volumes:
- mongodb_data_container_4:/data/db

volumes:
mongodb_data_container:
mongodb_data_container_1:
mongodb_data_container_2:
mongodb_data_container_3:
mongodb_data_container_4:
25 changes: 23 additions & 2 deletions src/modules/config/database/helpers/database.helper.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
import type { ConfigService } from "@nestjs/config";
import type { MongooseModuleFactoryOptions } from "@nestjs/mongoose";

function getDatabasePort(configService: ConfigService): number | undefined {
const port = configService.get<string>("DATABASE_PORT");
if (port === undefined) {
return undefined;
}
if (process.env.JEST_WORKER_ID !== undefined) {
const portMultiplier = 2;
const portAdjuster = (parseInt(process.env.JEST_WORKER_ID) - 1) * portMultiplier;
return parseInt(port) + portAdjuster;
}
if (process.env.CUCUMBER_WORKER_ID !== undefined) {
const portMultiplier = 2;
const portAdjuster = parseInt(process.env.CUCUMBER_WORKER_ID) * portMultiplier;
return parseInt(port) + portAdjuster;
}
return parseInt(port);
}

function mongooseModuleFactory(configService: ConfigService): MongooseModuleFactoryOptions {
const connectionTimeoutMs = 3000;
const host = configService.getOrThrow<string>("DATABASE_HOST");
const port = configService.get<string>("DATABASE_PORT");
const port = getDatabasePort(configService);
const databaseName = configService.getOrThrow<string>("DATABASE_NAME");
const username = configService.getOrThrow<string>("DATABASE_USERNAME");
const password = configService.getOrThrow<string>("DATABASE_PASSWORD");
Expand All @@ -26,4 +44,7 @@ function mongooseModuleFactory(configService: ConfigService): MongooseModuleFact
};
}

export { mongooseModuleFactory };
export {
getDatabasePort,
mongooseModuleFactory,
};

0 comments on commit d95b16a

Please sign in to comment.