Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

makes npm test more robust with async-retry and orchestrator.js #15

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}