Skip to content
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
42 changes: 23 additions & 19 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
const { config } = require('dotenv');
config({ path: '.env.test' });
import { config } from "dotenv";

config({ path: ".env.test" });

/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
roots: ['<rootDir>/tests'],
export default {
preset: "ts-jest",
testEnvironment: "node",
roots: ["<rootDir>/tests", "<rootDir>/src"],
transform: {
'^.+\\.tsx?$': [
'ts-jest',
"^.+\\.tsx?$": [
"ts-jest",
{
tsconfig: './tsconfig.json',
tsconfig: "./tsconfig.json",
},
],
},
testMatch: [
'**/tests/**/*.test.ts',
'**/__tests__/**/*.test.ts',
'(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',
"**/tests/**/*.test.ts",
"**/__tests__/**/*.test.ts",
"(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
"**/src/**/__tests__/**/*.test.ts",
"**/src/**/*.test.ts",
],
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1',
"^@/(.*)$": "<rootDir>/src/$1",
"^@prisma/client$": "<rootDir>/tests/__mocks__/prisma.ts",
},
collectCoverageFrom: [
'src/**/*.{js,ts}',
'!**/node_modules/**',
'!**/tests/**',
"src/**/*.{js,ts}",
"!**/node_modules/**",
"!**/tests/**",
],
coverageDirectory: 'coverage',
coverageDirectory: "coverage",
testTimeout: 30000,
};
};
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"type": "commonjs",
"main": "dist/index.js",
"scripts": {
"test:dto": "jest --runInBand src/modules/auth/__tests__/dto --detectOpenHandles",
"test:dto:independent": "jest --runInBand src/modules/auth/__tests__/dto --detectOpenHandles",
"dev": "ts-node-dev src/index.ts",
"build": "tsc",
"start": "node dist/index.js",
Expand Down
15 changes: 0 additions & 15 deletions src/dtos/emailVerification.dto.ts

This file was deleted.

6 changes: 0 additions & 6 deletions src/dtos/verifyEmailDTO.ts

This file was deleted.

56 changes: 56 additions & 0 deletions src/modules/auth/__tests__/dto/emailVerification.dto.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { RegisterDTO } from "../../dto/emailVerification.dto";

describe("RegisterDTO - DTO validation", () => {
it("should validate correct data", async () => {
const dto = new RegisterDTO({
name: "John Doe",
email: "john.doe@example.com",
password: "SecurePassword123",
wallet: "0x1234567890abcdef",
});
const errors = await RegisterDTO.validate(dto);
expect(errors).toHaveLength(0);
});

it("should validate required fields", async () => {
const dto = new RegisterDTO({
email: "john.doe@example.com",
password: "SecurePassword123",
});
const errors = await RegisterDTO.validate(dto);
expect(errors).toHaveLength(2);
expect(errors[0].property).toBe("name");
expect(errors[1].property).toBe("wallet");
});

it("should validate email format", async () => {
const dto = new RegisterDTO({
name: "John Doe",
email: "invalid-email",
password: "SecurePassword123",
wallet: "0x1234567890abcdef",
});
const errors = await RegisterDTO.validate(dto);
expect(errors).toHaveLength(1);
expect(errors[0].property).toBe("email");
});

it("should validate password length", async () => {
const dto = new RegisterDTO({
name: "John Doe",
email: "john.doe@example.com",
password: "short",
wallet: "0x1234567890abcdef",
});
const errors = await RegisterDTO.validate(dto);
expect(errors).toHaveLength(1);
expect(errors[0].property).toBe("password");
});

it("should fail with invalid email", async () => {
const dto = new RegisterDTO();
dto.email = "invalid-email";
const errors = await RegisterDTO.validate(dto);
expect(errors.length).toBeGreaterThan(0);
});
});
27 changes: 27 additions & 0 deletions src/modules/auth/__tests__/dto/resendVerificationDTO.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { validate } from "class-validator";
import { ResendVerificationDTO } from "../../dto/resendVerificationDTO";

describe("ResendVerificationDTO - DTO validation", () => {
it("should validate correct email", async () => {
const dto = new ResendVerificationDTO();
dto.email = "test@example.com";
const errors = await validate(dto);
expect(errors).toHaveLength(0);
});

it("should fail with invalid email", async () => {
const dto = new ResendVerificationDTO();
dto.email = "invalid-email";
const errors = await validate(dto);
expect(errors).toHaveLength(1);
expect(errors[0].property).toBe("email");
});

it("should fail with empty email", async () => {
const dto = new ResendVerificationDTO();
dto.email = "";
const errors = await validate(dto);
expect(errors).toHaveLength(1);
expect(errors[0].property).toBe("email");
});
});
28 changes: 28 additions & 0 deletions src/modules/auth/__tests__/dto/verifyEmailDTO.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { validate } from "class-validator";
import { VerifyEmailDTO } from "../../dto/verifyEmailDTO";

describe("VerifyEmailDTO - DTO validation", () => {
it("should validate correct token", async () => {
const dto = new VerifyEmailDTO();
dto.token = "valid-token-123";
const errors = await validate(dto);
expect(errors).toHaveLength(0);
});

it("should fail with empty token", async () => {
const dto = new VerifyEmailDTO();
dto.token = "";
const errors = await validate(dto);
expect(errors).toHaveLength(1);
expect(errors[0].property).toBe("token");
});

it("should fail with invalid type", async () => {
const dto = new VerifyEmailDTO();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
dto.token = 123 as any; // Invalid type
const errors = await validate(dto);
expect(errors).toHaveLength(1);
expect(errors[0].property).toBe("token");
});
});
46 changes: 46 additions & 0 deletions src/modules/auth/dto/emailVerification.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {
IsString,
IsEmail,
IsNotEmpty,
MinLength,
validate,
} from "class-validator";

export class RegisterDTO {
@IsString()
@IsNotEmpty()
name: string;

@IsEmail()
@IsNotEmpty()
email: string;

@IsString()
@IsNotEmpty()
@MinLength(8)
password: string;

@IsString()
@IsNotEmpty()
wallet: string;

constructor(partial: Partial<RegisterDTO> = {}) {
Object.assign(this, partial);
this.name = this.name || "";
this.email = this.email || "";
this.password = this.password || "";
this.wallet = this.wallet || "";
}

static validate(dto: RegisterDTO) {
return validate(dto);
}
}

export interface VerifyEmailDTO {
token: string;
}

export interface ResendVerificationDTO {
email: string;
}
7 changes: 7 additions & 0 deletions src/modules/auth/dto/verifyEmailDTO.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { IsString, IsNotEmpty } from "class-validator";

export class VerifyEmailDTO {
@IsString()
@IsNotEmpty()
token: string;
}
6 changes: 6 additions & 0 deletions tests/__mocks__/prisma.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const PrismaClient = class {
constructor() {}
};

export const prismaClientSingleton = () => new PrismaClient();
export const prisma = prismaClientSingleton();
58 changes: 58 additions & 0 deletions tests/dto-validation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { validate } from "class-validator";
import { RegisterDTO } from "../src/modules/auth/dto/emailVerification.dto";

describe("DTO Validation", () => {
describe("RegisterDTO", () => {
it("should validate correct data", async () => {
const dto: RegisterDTO = {
name: "John Doe",
email: "john.doe@example.com",
password: "SecurePassword123",
wallet: "0x1234567890abcdef",
};
const errors = await validate(dto);
expect(errors).toHaveLength(0);
});

it("should validate required fields", async () => {
const dto = new RegisterDTO({
email: "john.doe@example.com",
password: "SecurePassword123",
});
const errors = await validate(dto);
expect(errors).toHaveLength(2);
expect(errors[0].property).toBe("name");
expect(errors[1].property).toBe("wallet");
expect(errors[0].constraints?.required).toBe("Name is required");
expect(errors[1].constraints?.required).toBe("Wallet is required");
});

it("should validate email format", async () => {
const dto = new RegisterDTO({
name: "John Doe",
email: "invalid-email",
password: "SecurePassword123",
wallet: "0x1234567890abcdef",
});
const errors = await validate(dto);
expect(errors).toHaveLength(1);
expect(errors[0].property).toBe("email");
expect(errors[0].constraints?.email).toBeDefined();
});

it("should validate password length", async () => {
const dto = new RegisterDTO({
name: "John Doe",
email: "john.doe@example.com",
password: "short", // Too short
wallet: "0x1234567890abcdef",
});
const errors = await validate(dto);
expect(errors).toHaveLength(1);
expect(errors[0].property).toBe("password");
expect(errors[0].constraints?.minlength).toBe(
"Password must be at least 8 characters long"
);
});
});
});
Loading