Skip to content

Commit e2ec6f8

Browse files
authored
feat: concurrently and orchestrator.js
1 parent 6a03144 commit e2ec6f8

File tree

7 files changed

+191
-10
lines changed

7 files changed

+191
-10
lines changed

jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const createJestConfig = nextJest({
1010
});
1111
const jestConfig = createJestConfig({
1212
moduleDirectories: ["node_modules", "<rootDir>"],
13+
testTimeout: 60000,
1314
});
1415

1516
module.exports = jestConfig;

package-lock.json

Lines changed: 147 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"services:stop": "docker compose -f infra/compose.yaml stop",
1111
"lint:check": "prettier --check .",
1212
"lint:fix": "prettier --write .",
13-
"test": "jest --runInBand",
13+
"test": "npm run services:up && concurrently -n next,jest --hide next --kill-others --success command-jest \"next dev\" \"jest --runInBand\"",
1414
"test:watch": "jest --watchAll --runInBand",
1515
"migration:create": "node-pg-migrate --migrations-dir infra/migrations create",
1616
"migration:up": "node-pg-migrate --migrations-dir infra/migrations --envPath .env.development up",
@@ -19,6 +19,7 @@
1919
"author": "",
2020
"license": "MIT",
2121
"dependencies": {
22+
"async-retry": "^1.3.3",
2223
"dotenv": "^16.4.4",
2324
"next": "^13.1.6",
2425
"node-pg-migrate": "^6.2.2",
@@ -27,6 +28,7 @@
2728
"react-dom": "^18.2.0"
2829
},
2930
"devDependencies": {
31+
"concurrently": "^8.2.2",
3032
"jest": "^29.6.2",
3133
"prettier": "^3.3.3"
3234
}

tests/orchestrator.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import retry from "async-retry";
2+
3+
async function waitForAllServices() {
4+
await waitForWebServer();
5+
6+
async function waitForWebServer() {
7+
return retry(fetchStatusPage, {
8+
retries: 100,
9+
minTimeout: 100,
10+
maxTimeout: 1000,
11+
});
12+
13+
async function fetchStatusPage() {
14+
const response = await fetch("http://localhost:3000/api/v1/status");
15+
16+
if (response.status !== 200) {
17+
throw new Error("Status page not ready yet");
18+
}
19+
}
20+
}
21+
}
22+
23+
export default {
24+
waitForAllServices,
25+
};

tests/unit/integration/api/v1/migrations/get.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import database from "infra/database.js";
2+
import orchestrator from "tests/orchestrator";
23

3-
beforeAll(cleanDatabase);
4-
5-
async function cleanDatabase() {
4+
beforeAll(async () => {
5+
await orchestrator.waitForAllServices();
66
await database.query("drop schema public cascade; create schema public;");
7-
}
7+
});
88

99
test("GET to /api/v1/migrations should return 200", async () => {
1010
const response = await fetch("http://localhost:3000/api/v1/migrations");

tests/unit/integration/api/v1/migrations/post.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import database from "infra/database.js";
2+
import orchestrator from "tests/orchestrator";
23

3-
beforeAll(cleanDatabase);
4-
5-
async function cleanDatabase() {
4+
beforeAll(async () => {
5+
await orchestrator.waitForAllServices();
66
await database.query("drop schema public cascade; create schema public;");
7-
}
7+
});
88

99
test("POST to /api/v1/migrations should return 200", async () => {
1010
const response1 = await fetch("http://localhost:3000/api/v1/migrations", {

tests/unit/integration/api/v1/status/get.test.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
import orchestrator from "tests/orchestrator";
2+
3+
beforeAll(async () => {
4+
await orchestrator.waitForAllServices();
5+
});
6+
17
test("GET to /api/v1/status returns 200", async () => {
28
const response = await fetch("http://localhost:3000/api/v1/status");
39
expect(response.status).toBe(200);
@@ -11,4 +17,4 @@ test("GET to /api/v1/status returns 200", async () => {
1117
expect(responseBody.dependecies.database.version).toBe("16.0");
1218
expect(responseBody.dependecies.database.max_connections).toEqual(100);
1319
expect(responseBody.dependecies.database.opened_connections).toEqual(1);
14-
});
20+
});

0 commit comments

Comments
 (0)