Skip to content

Commit 7cf07dc

Browse files
authored
Merge pull request #215 from codeableorg/test-env-update
fix: update database URL in .env.test and add dotenv-cli to dependencies; refactor Playwright config to comment out unused browsers
2 parents 2d45d96 + ba22350 commit 7cf07dc

File tree

12 files changed

+155
-73
lines changed

12 files changed

+155
-73
lines changed

.env.test

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
DATABASE_URL="postgresql://diego@localhost:5432/fullstock?schema=public"
1+
DATABASE_URL="postgresql://diego@localhost:5432/fullstock_test?schema=public"
22

33
# Admin Database (for database creation/deletion)
4-
ADMIN_DB_NAME=postgres
5-
6-
# This was inserted by `prisma init`:
7-
[object Promise]
4+
ADMIN_DB_NAME=postgres

.github/workflows/tests.yaml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,44 @@ jobs:
5757

5858
- name: Run Unit Tests
5959
run: npm run test
60+
61+
e2e-test:
62+
runs-on: ubuntu-latest
63+
needs: [test]
64+
services:
65+
postgres:
66+
image: postgres:15
67+
ports:
68+
- 5432:5432
69+
env:
70+
POSTGRES_USER: diego
71+
POSTGRES_DB: fullstock_test
72+
POSTGRES_HOST_AUTH_METHOD: trust
73+
options: >-
74+
--health-cmd "pg_isready -U postgres" --health-interval 10s --health-timeout 5s --health-retries 5
75+
steps:
76+
- name: Checkout code
77+
uses: actions/checkout@v4
78+
79+
- name: Set up Node
80+
uses: actions/setup-node@v3
81+
with:
82+
node-version: "lts/*"
83+
84+
- name: Install dependencies
85+
run: npm clean-install
86+
87+
- name: Install Playwright Browsers
88+
run: npx playwright install --with-deps
89+
90+
- name: Build the application
91+
run: npm run build
92+
93+
- name: Migrate the database
94+
run: npm run test:prisma:migrate:deploy
95+
96+
- name: Seed the database
97+
run: npm run test:prisma:seed
98+
99+
- name: Run E2E Tests
100+
run: npm run test:e2e

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ dist-ssr
2626
# React Router
2727
.react-router/
2828

29-
.build/
29+
build/
3030
.env
3131

3232
# Playwright

package-lock.json

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,17 @@
1010
"preview": "vite preview",
1111
"start": "react-router-serve ./build/server/index.js",
1212
"type-check": "react-router typegen && tsc",
13-
"test": "vitest"
13+
"test": "vitest",
14+
"prisma": "prisma",
15+
"prisma:generate": "prisma generate",
16+
"prisma:migrate": "prisma migrate dev --name init",
17+
"prisma:migrate:deploy": "prisma migrate deploy",
18+
"prisma:migrate:status": "prisma migrate status",
19+
"prisma:studio": "prisma studio",
20+
"prisma:seed": "prisma db seed",
21+
"test:prisma:migrate:deploy": "dotenv -e .env.test -- prisma migrate deploy",
22+
"test:e2e": "playwright test",
23+
"test:prisma:seed": "dotenv -e .env.test prisma db seed"
1424
},
1525
"prisma": {
1626
"seed": "tsx ./prisma/seed.ts"
@@ -59,6 +69,7 @@
5969
"@typescript-eslint/parser": "^8.31.0",
6070
"@vitejs/plugin-react": "^4.3.4",
6171
"autoprefixer": "^10.4.21",
72+
"dotenv-cli": "^8.0.0",
6273
"eslint": "^9.25.1",
6374
"eslint-plugin-import": "^2.31.0",
6475
"eslint-plugin-react-hooks": "^5.0.0",

playwright.config.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { defineConfig, devices } from "@playwright/test";
22
import dotenv from "dotenv";
33

4+
import { baseUrl, command } from "@/e2e/utils-tests-e2e";
5+
46
// Load test environment variables
57
dotenv.config({ path: ".env.test" });
68

@@ -18,15 +20,16 @@ dotenv.config({ path: ".env.test" });
1820
export default defineConfig({
1921
testDir: "./src/e2e",
2022
/* Run tests in files in parallel */
21-
fullyParallel: true,
23+
// globalSetup: require.resolve("./src/e2e/setup.ts"),
24+
fullyParallel: false,
2225
/* Fail the build on CI if you accidentally left test.only in the source code. */
2326
forbidOnly: !!process.env.CI,
2427
/* Retry on CI only */
2528
retries: process.env.CI ? 2 : 0,
2629
/* Opt out of parallel tests on CI. */
27-
workers: process.env.CI ? 1 : undefined,
30+
workers: 1,
2831
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
29-
reporter: "html",
32+
reporter: "list",
3033
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
3134
use: {
3235
/* Base URL to use in actions like `await page.goto('/')`. */
@@ -43,15 +46,15 @@ export default defineConfig({
4346
use: { ...devices["Desktop Chrome"] },
4447
},
4548

46-
{
47-
name: "firefox",
48-
use: { ...devices["Desktop Firefox"] },
49-
},
49+
// {
50+
// name: "firefox",
51+
// use: { ...devices["Desktop Firefox"] },
52+
// },
5053

51-
{
52-
name: "webkit",
53-
use: { ...devices["Desktop Safari"] },
54-
},
54+
// {
55+
// name: "webkit",
56+
// use: { ...devices["Desktop Safari"] },
57+
// },
5558

5659
/* Test against mobile viewports. */
5760
// {
@@ -75,9 +78,9 @@ export default defineConfig({
7578
],
7679

7780
/* Run your local dev server before starting the tests */
78-
// webServer: {
79-
// command: 'npm run start',
80-
// url: 'http://localhost:3000',
81-
// reuseExistingServer: !process.env.CI,
82-
// },
81+
webServer: {
82+
command: command,
83+
url: baseUrl,
84+
reuseExistingServer: !process.env.CI,
85+
},
8386
});

src/e2e/demo.signin.spec.ts

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,34 @@ import { prisma } from "@/db/prisma";
44
import { hashPassword } from "@/lib/security";
55
import type { CreateUserDTO } from "@/models/user.model";
66

7-
test.describe("Visitante inicio sesion", () => {
8-
let testUserId: number;
7+
import { baseUrl, cleanDatabase } from "./utils-tests-e2e";
8+
9+
test.beforeEach(async () => {
10+
await cleanDatabase();
11+
});
912

10-
test.beforeAll(async () => {
13+
test.describe("Visitante inicio sesion", () => {
14+
test.beforeEach(async () => {
1115
const testUser: CreateUserDTO = {
1216
1317
name: null,
1418
password: await hashPassword("letmein"),
1519
isGuest: false,
1620
};
1721

18-
const existingUser = await prisma.user.findUnique({
19-
where: { email: testUser.email },
20-
});
21-
22-
if (existingUser) {
23-
await prisma.user.delete({
24-
where: { id: existingUser.id },
25-
});
26-
}
27-
28-
const user = await prisma.user.create({
22+
await prisma.user.create({
2923
data: testUser,
3024
});
31-
testUserId = user.id;
32-
});
33-
34-
test.afterAll(async () => {
35-
await prisma.user.delete({
36-
where: { id: testUserId },
37-
});
3825
});
3926

4027
test("test", async ({ page }) => {
41-
await page.goto("http://localhost:5173/");
28+
await page.goto(baseUrl);
4229
await page.getByTestId("login").click();
4330
await page.getByRole("textbox", { name: "Correo electrónico" }).click();
4431
await page
4532
.getByRole("textbox", { name: "Correo electrónico" })
4633
47-
await page
48-
.getByRole("textbox", { name: "Correo electrónico" })
49-
.press("Tab");
34+
5035
await page.getByRole("textbox", { name: "Contraseña" }).fill("letmein");
5136
await page.getByRole("button", { name: "Iniciar sesión" }).click();
5237

src/e2e/demo.spec.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import { test, expect } from "@playwright/test";
22

3+
import { baseUrl, cleanDatabase } from "./utils-tests-e2e";
4+
5+
test.beforeEach(async () => {
6+
await cleanDatabase();
7+
});
8+
39
test.describe("Visitor", () => {
410
test("can add a product to the cart", async ({ page }) => {
5-
await page.goto("http://localhost:5173/");
11+
await page.goto(baseUrl);
612

713
await expect(page).toHaveTitle(/inicio/i);
814

src/e2e/guest-create-order.spec.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
// import { createOrderFormData } from "@/lib/utils.tests";
22
import { expect, test } from "@playwright/test";
33

4-
import { createOrderFormData } from "./utils-tests-e2e";
4+
import { baseUrl, cleanDatabase, createOrderFormData } from "./utils-tests-e2e";
55

66
export type OrderFormData = Record<string, string>;
77

8+
test.beforeEach(async () => {
9+
await cleanDatabase();
10+
});
11+
812
test.describe("Guest", () => {
913
test("Guest can create an order", async ({ page }) => {
1014
// Navegar a la tienda y agregar un producto
11-
await page.goto("http://localhost:5173/");
15+
await page.goto(baseUrl);
1216

1317
await page.getByRole("menuitem", { name: "Polos" }).click();
1418
await page.getByTestId("product-item").first().click();

src/e2e/setup.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { cleanDatabase } from "./utils-tests-e2e";
2+
3+
export default async function globalSetup() {
4+
await cleanDatabase();
5+
}

0 commit comments

Comments
 (0)