Skip to content

Commit

Permalink
Merge pull request #15 from vitoropereira/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
vitoropereira committed Jul 15, 2024
2 parents 371a03c + a2479ef commit 8c8968e
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 12 deletions.
2 changes: 2 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ const nextJest = require('next/jest');
const createJestConfig = nextJest({
dir: ".",
});

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.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@
"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",
"wait-for-postgres":"node infra/scripts/wait-for-postgres.js"
"wait-for-postgres": "node infra/scripts/wait-for-postgres.js"
},
"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": "^2.8.8"
}
Expand Down
10 changes: 5 additions & 5 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";

beforeAll(cleanDatabase);

async function cleanDatabase(){
await database.query("drop schema public cascade; create schema public");
}
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
10 changes: 5 additions & 5 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";

beforeAll(cleanDatabase);

async function cleanDatabase(){
await database.query("drop schema public cascade; create schema public");
}
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.test.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,
maxTimeout: 1000
})
}

async function fetchStatusPage(bail, tryNumber){
const response = await fetch("http://localhost:3000/api/v1/status");

if(response.status !== 200){
throw Error();
}
}
}

export default {
waitForAllServices
}

0 comments on commit 8c8968e

Please sign in to comment.