Skip to content

Commit

Permalink
testintg tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MaximeDan committed May 24, 2024
1 parent 384cdeb commit 2e5950b
Show file tree
Hide file tree
Showing 7 changed files with 7,340 additions and 2,980 deletions.
79 changes: 79 additions & 0 deletions __test__/auth/apiClient/registerUserApi.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import request from "supertest";
import { spawn, ChildProcess } from "child_process";
import path from "path";

let server: ChildProcess;

beforeAll(async () => {
const serverScript = path.resolve(__dirname, "../../customServer.ts");
server = spawn("ts-node", [serverScript]);

// Wait for the server to be ready
await new Promise((resolve, reject) => {
server.stdout?.on("data", (data) => {
if (data.toString().includes("> Ready on http://localhost:3000")) {
resolve(null);
}
});
server.stderr?.on("data", (data) => {
reject(data.toString());
});
});
}, 30000); // Increase the timeout to 30 seconds

afterAll(() => {
if (server) {
server.kill();
}
});

describe("registerUser API Integration Test", () => {
it("registers a new user successfully", async () => {
const data = {
email: "[email protected]",
password: "Password@123",
username: "johndoe",
name: "John",
lastName: "Doe",
dateOfBirth: new Date(1990, 1, 1),
};

const response = await request("http://localhost:3000")
.post("/api/auth/register")
.send(data)
.set("Accept", "application/json");

expect(response.status).toBe(200);
expect(response.body).toEqual(
expect.objectContaining({
message: "User registered successfully",
// Other expected response properties
}),
);
});

it("returns error for already used email", async () => {
const data = {
email: "[email protected]",
password: "Password@123",
username: "johndoe",
name: "John",
lastName: "Doe",
dateOfBirth: new Date(1990, 1, 1),
};

const response = await request("http://localhost:3000")
.post("/api/auth/register")
.send(data)
.set("Accept", "application/json");

expect(response.status).toBe(400);
expect(response.body).toEqual(
expect.objectContaining({
message: "Email already in use",
}),
);
});

// Add more tests for other use cases as needed
});
120 changes: 0 additions & 120 deletions __test__/auth/register.test.tsx

This file was deleted.

19 changes: 19 additions & 0 deletions __test__/customServer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { createServer } = require("http");
const next = require("next");
const { parse } = require("url");

const dev = process.env.NODE_ENV !== "production";
const app = next({ dev });
const handle = app.getRequestHandler();

app.prepare().then(() => {
const server = createServer((req: { url: any }, res: any) => {
const parsedUrl = parse(req.url, true);
handle(req, res, parsedUrl);
});

server.listen(3004, (err: any) => {
if (err) throw err;
console.log("> Ready on http://localhost:3004");
});
});
Loading

0 comments on commit 2e5950b

Please sign in to comment.