Skip to content

Commit f1351d6

Browse files
authored
Merge pull request #18 from epcgrs/fix/npm-test
orchestrators and testes more robusts
2 parents 533cebd + 6a79e72 commit f1351d6

File tree

8 files changed

+145
-8
lines changed

8 files changed

+145
-8
lines changed

jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const createJestConfig = nextJest({
1111
const jestConfig = createJestConfig({
1212
moduleDirectories: ['node_modules', '<rootDir>'],
1313
setupFiles: ['dotenv/config'],
14+
testTimeout: 30000
1415
});
1516

1617
module.exports = jestConfig;

package-lock.json

Lines changed: 94 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
@@ -3,7 +3,7 @@
33
"version": "1.0.0",
44
"main": "index.js",
55
"scripts": {
6-
"test": "jest --runInBand",
6+
"test": "npm run services:up && concurrently -n \"next,jest\" --hide next -k -s command-jest \"next dev\" \"jest --runInBand\"",
77
"test:watch": "jest --watchAll --runInBand",
88
"dev": "npm run services:up && npm run wait-for-postgres && npm run migration:up && next dev",
99
"services:up": "docker compose -f infra/compose.yaml up -d",
@@ -20,6 +20,7 @@
2020
"license": "MIT",
2121
"description": "",
2222
"dependencies": {
23+
"async-retry": "^1.3.3",
2324
"dotenv": "^17.2.1",
2425
"dotenv-expand": "^12.0.2",
2526
"next": "^13.1.6",
@@ -29,6 +30,7 @@
2930
"react-dom": "^18.2.0"
3031
},
3132
"devDependencies": {
33+
"concurrently": "^9.2.1",
3234
"jest": "^29.6.2",
3335
"prettier": "^3.5.3"
3436
}

pages/api/v1/migrations/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import database from 'infra/database.js';
33
export default async function migrations(request, response) {
44

55
if (request.method !== 'GET' && request.method !== 'POST') {
6-
response.setHeader("Content-Type", "application/json");
76
response.setHeader("Allow", ["GET", "POST"]);
8-
response.status(405).end(`Method ${request.method} Not Allowed`);
7+
return response.status(405).end(`Method ${request.method} Not Allowed`);
98
}
109

1110
if (request.method === 'GET') {
1211
return await migrationGet(request, response);
13-
} else if (request.method === 'POST') {
12+
}
13+
if (request.method === 'POST') {
1414
return await migrationPost(request, response);
1515
}
1616

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import database from "infra/database";
2+
import orchestrator from "tests/orchestrator";
3+
4+
beforeAll(async () => {
5+
await orchestrator.waitForAllServices();
6+
await cleanDatabase();
7+
});
28

39
async function cleanDatabase() {
410
await database.query("drop schema public cascade;");
511
await database.query("create schema public;");
612
}
713

8-
beforeAll(cleanDatabase);
9-
1014
test("GET to /api/v1/migrations should return 200", async () => {
1115

1216
const response = await fetch("http://localhost:3000/api/v1/migrations");

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import database from "infra/database";
2+
import orchestrator from "tests/orchestrator";
3+
4+
beforeAll(async () => {
5+
await orchestrator.waitForAllServices();
6+
await cleanDatabase();
7+
});
28

39
async function cleanDatabase() {
410
await database.query("drop schema public cascade;");
511
await database.query("create schema public;");
612
}
713

8-
beforeAll(cleanDatabase);
9-
1014
test("POST to /api/v1/migrations should return 200", async () => {
1115

1216
const response1 = await fetch("http://localhost:3000/api/v1/migrations", {

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

Lines changed: 6 additions & 0 deletions
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 should return 200", async () => {
28
const response = await fetch("http://localhost:3000/api/v1/status");
39
expect(response.status).toBe(200);

tests/orchestrator.js

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

0 commit comments

Comments
 (0)