Skip to content

Commit

Permalink
Merge pull request #14 from brunoh5/fix-npm-test
Browse files Browse the repository at this point in the history
makes `npm test` more robust with `async-retry` and `orchestrator.js`
  • Loading branch information
brunoh5 authored Dec 30, 2024
2 parents a5551c9 + 8cbc05c commit a1234b6
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 9 deletions.
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const createJestConfig = nextJest({

const jestConfig = createJestConfig({
moduleDirectories: ["node_modules", "<rootDir>"],
testTimeout: 60000,
});

module.exports = jestConfig;
147 changes: 147 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"services:down": "docker compose -f infra/compose.yaml down",
"lint:check": "prettier --check .",
"lint:fix": "prettier --write .",
"test": "jest --runInBand",
"test": " npm run services:up && concurrently -n next,jest --hide next -k -s command-jest \"next dev\" \"jest --runInBand --verbose\"",
"test:watch": "jest --watchAll --runInBand",
"migration:create": "node-pg-migrate -m infra/migrations create",
"migration:up": "node-pg-migrate -m infra/migrations --envPath .env.development up",
Expand All @@ -19,6 +19,7 @@
"author": "",
"license": "MIT",
"dependencies": {
"async-retry": "^1.3.3",
"dotenv": "^16.4.4",
"dotenv-expand": "^11.0.6",
"next": "^13.1.6",
Expand All @@ -28,6 +29,7 @@
"react-dom": "^18.2.0"
},
"devDependencies": {
"concurrently": "^8.2.2",
"jest": "^29.6.2",
"prettier": "^3.4.1"
}
Expand Down
8 changes: 4 additions & 4 deletions tests/integration/api/v1/migrations/get.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import database from "infra/database.js";
import orchestrator from "tests/orchestrator";

beforeAll(cleanDatabase);

async function cleanDatabase() {
beforeAll(async () => {
await orchestrator.waitForAllServices();
await database.query("drop schema public cascade; create schema public");
}
});

test("GET to /api/v1/migrations should return 200", async () => {
const response = await fetch("http://localhost:3000/api/v1/migrations");
Expand Down
8 changes: 4 additions & 4 deletions tests/integration/api/v1/migrations/post.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import database from "infra/database.js";
import orchestrator from "tests/orchestrator";

beforeAll(cleanDatabase);

async function cleanDatabase() {
beforeAll(async () => {
await orchestrator.waitForAllServices();
await database.query("drop schema public cascade; create schema public");
}
});

test("POST to /api/v1/migrations should return 200", async () => {
const response1 = await fetch("http://localhost:3000/api/v1/migrations", {
Expand Down
6 changes: 6 additions & 0 deletions tests/integration/api/v1/status/get.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import orchestrator from "tests/orchestrator";

beforeAll(async () => {
await orchestrator.waitForAllServices();
});

test("GET to /api/v1/status should return 200", async () => {
const response = await fetch("http://localhost:3000/api/v1/status");
expect(response.status).toBe(200);
Expand Down
24 changes: 24 additions & 0 deletions tests/orchestrator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import retry from "async-retry";

async function waitForAllServices() {
await waitForWebServer();

async function waitForWebServer() {
return retry(fetchStatusPage, {
retries: 100,
minTimeout: 100,
maxTimeout: 1000,
});

async function fetchStatusPage() {
const response = await fetch("http://localhost:3000/api/v1/status");
if (response.status !== 200) {
throw Error();
}
}
}
}

export default {
waitForAllServices,
};

1 comment on commit a1234b6

@vercel
Copy link

@vercel vercel bot commented on a1234b6 Dec 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.