Skip to content

Commit

Permalink
add: resetting and seeding database for individual tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thelissimus-work committed Feb 24, 2025
1 parent 2dbac0c commit 73ffc6a
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 10 deletions.
50 changes: 41 additions & 9 deletions e2e/cypress.config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,45 @@
const util = require("node:util");
const cp = require("node:child_process");
const { defineConfig } = require("cypress");
const { Pool } = require("pg");

const exec = util.promisify(cp.exec);
const { env } = process;
const pool = new Pool({
connectionString: `postgres://${env.DB_USER}:${env.DB_PASSWORD}@${env.DB_HOST}:${env.DB_PORT}/${env.DB_NAME}`,
});

async function resetDatabase() {
const client = await pool.connect();
try {
const res = await client.query(
"SELECT tablename FROM pg_tables WHERE schemaname = 'public';",
);
for (const row of res.rows) {
await client.query(`TRUNCATE TABLE ${row.tablename} RESTART IDENTITY CASCADE;`);
}
} finally {
client.release();
}
return null;
}

async function seedDatabase() {
await exec("cd .. && go run cmd/seed/main.go");
return null;
}

module.exports = defineConfig({
e2e: {
defaultCommandTimeout: 15000,
requestTimeout: 20000,
responseTimeout: 20000,
pageLoadTimeout: 60000,
setupNodeEvents(on, config) {
// implement node event listeners here
},
},
e2e: {
defaultCommandTimeout: 15000,
requestTimeout: 20000,
responseTimeout: 20000,
pageLoadTimeout: 60000,
setupNodeEvents(on, config) {
on("task", {
resetDatabase,
seedDatabase,
});
},
},
});
5 changes: 5 additions & 0 deletions e2e/cypress/e2e/users/register.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ const login = (email, password) => {
};

describe("user auth and registration flow", () => {
before(() => {
cy.task("resetDatabase");
cy.task("seedDatabase");
});

afterEach(() => {
cy.visit("http://localhost:3200/logout");
});
Expand Down
3 changes: 2 additions & 1 deletion e2e/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"dependencies": {
"cypress": "^13.15.0"
"cypress": "^13.15.0",
"pg": "^8.13.3"
}
}
110 changes: 110 additions & 0 deletions e2e/pnpm-lock.yaml

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

0 comments on commit 73ffc6a

Please sign in to comment.