Skip to content

Commit

Permalink
makes npm test more robust with async-retry and orchestrator.js
Browse files Browse the repository at this point in the history
  • Loading branch information
liviomendonca authored Sep 16, 2024
1 parent 6d4836a commit ddb4b20
Show file tree
Hide file tree
Showing 7 changed files with 178 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 @@ -10,6 +10,7 @@ const createJestConfig = nextJest({

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

module.exports = jestConfig;
133 changes: 133 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\"",
"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.3.3"
}
Expand Down
8 changes: 4 additions & 4 deletions tests/integration/api/v1/migrations/get.test.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.js";

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

beforeAll(cleanDatabase);
});

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.test.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.js";

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

beforeAll(cleanDatabase);
});

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.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import orchestrator from "tests/orchestrator.js";

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
27 changes: 27 additions & 0 deletions tests/orchestrator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// comentario do commit
// makes `npm test` more robust with `async-retry` and `orchestrator.js`

import retry from "async-retry";

async function waitForAllServices() {
await waitForWebServer();

async function waitForWebServer() {
return retry(fetchStatusPage, {
retries: 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,
};

0 comments on commit ddb4b20

Please sign in to comment.