-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
193 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import { Test } from '@nestjs/testing'; | ||
import { AppModule } from './app.module'; | ||
import { ConfigModule, ConfigService } from '@nestjs/config'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { CacheModule } from '@nestjs/cache-manager'; | ||
|
||
// Mock the ConfigService | ||
const mockConfigService = { | ||
get: jest.fn((key) => { | ||
if (key === 'cache') { | ||
return { | ||
host: 'localhost', | ||
port: 6379, | ||
ttl: 300, | ||
}; | ||
} | ||
if (key === 'database') { | ||
return { | ||
type: 'postgres', | ||
host: 'localhost', | ||
port: 5432, | ||
username: 'test', | ||
password: 'test', | ||
database: 'test', | ||
synchronize: false, | ||
}; | ||
} | ||
return null; | ||
}), | ||
}; | ||
|
||
// Create mock repository | ||
const mockRepository = { | ||
find: jest.fn(), | ||
findOne: jest.fn(), | ||
save: jest.fn(), | ||
create: jest.fn(), | ||
update: jest.fn(), | ||
delete: jest.fn(), | ||
}; | ||
|
||
// Mock @nestjs/typeorm | ||
jest.mock('@nestjs/typeorm', () => ({ | ||
TypeOrmModule: { | ||
forRootAsync: jest.fn().mockReturnValue({ | ||
module: class MockTypeOrmModule {}, | ||
}), | ||
forFeature: jest.fn().mockReturnValue({ | ||
module: class MockTypeOrmFeatureModule {}, | ||
}), | ||
}, | ||
getRepositoryToken: jest.fn().mockReturnValue('MockRepository'), | ||
InjectRepository: () => () => mockRepository, | ||
})); | ||
|
||
// Mock feature modules | ||
jest.mock('./auth/auth.module', () => ({ | ||
AuthModule: class MockAuthModule {}, | ||
})); | ||
|
||
jest.mock('./users/users.module', () => ({ | ||
UsersModule: class MockUsersModule {}, | ||
})); | ||
|
||
jest.mock('./employees/employees.module', () => ({ | ||
EmployeesModule: class MockEmployeesModule {}, | ||
})); | ||
|
||
jest.mock('./services/services.module', () => ({ | ||
ServicesModule: class MockServicesModule {}, | ||
})); | ||
|
||
jest.mock('./bookings/bookings.module', () => ({ | ||
BookingsModule: class MockBookingsModule {}, | ||
})); | ||
|
||
// Mock entities | ||
jest.mock('./users/entities/user.entity', () => ({ | ||
User: class MockUser {}, | ||
})); | ||
|
||
jest.mock('./employees/entities/employee.entity', () => ({ | ||
Employee: class MockEmployee {}, | ||
})); | ||
|
||
jest.mock('./services/entities/service.entity', () => ({ | ||
Service: class MockService {}, | ||
})); | ||
|
||
jest.mock('./bookings/entities/booking.entity', () => ({ | ||
Booking: class MockBooking {}, | ||
})); | ||
|
||
// Mock CacheModule | ||
jest.mock('@nestjs/cache-manager', () => ({ | ||
CacheModule: { | ||
registerAsync: jest.fn().mockReturnValue({ | ||
module: class MockCacheModule {}, | ||
}), | ||
}, | ||
})); | ||
|
||
describe('AppModule', () => { | ||
let app; | ||
let configService; | ||
|
||
beforeEach(async () => { | ||
const moduleRef = await Test.createTestingModule({ | ||
imports: [AppModule], | ||
}) | ||
.overrideProvider(ConfigService) | ||
.useValue(mockConfigService) | ||
.compile(); | ||
|
||
app = moduleRef.createNestApplication(); | ||
await app.init(); | ||
|
||
configService = moduleRef.get<ConfigService>(ConfigService); | ||
}); | ||
|
||
afterEach(async () => { | ||
await app.close(); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(app).toBeDefined(); | ||
}); | ||
|
||
it('should configure cache with correct default values', () => { | ||
const cacheConfig = configService.get('cache'); | ||
expect(cacheConfig).toBeDefined(); | ||
expect(cacheConfig.ttl).toBe(300); | ||
expect(cacheConfig.host).toBe('localhost'); | ||
expect(cacheConfig.port).toBe(6379); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { Test } from '@nestjs/testing'; | ||
import { UsersModule } from './users.module'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { User } from './entities/user.entity'; | ||
import { UsersService } from './users.service'; | ||
|
||
describe('UsersModule', () => { | ||
let module: UsersModule; | ||
|
||
beforeEach(async () => { | ||
const moduleRef = await Test.createTestingModule({ | ||
imports: [ | ||
TypeOrmModule.forRoot({ | ||
type: 'postgres', | ||
host: 'localhost', | ||
port: 5432, | ||
username: 'test', | ||
password: 'test', | ||
database: 'test', | ||
entities: [User], | ||
synchronize: true, | ||
}), | ||
UsersModule, | ||
], | ||
}).compile(); | ||
|
||
module = moduleRef.get(UsersModule); | ||
}, 10000); // Increased timeout to 10 seconds | ||
|
||
it('should be defined', () => { | ||
expect(module).toBeDefined(); | ||
}); | ||
|
||
it('should export UsersService', () => { | ||
const moduleExports = Reflect.getMetadata('exports', UsersModule); | ||
expect(moduleExports).toContain(UsersService); | ||
}); | ||
|
||
it('should export TypeOrmModule', () => { | ||
const moduleExports = Reflect.getMetadata('exports', UsersModule); | ||
expect(moduleExports).toContain(TypeOrmModule); | ||
}); | ||
|
||
it('should not have any controllers', () => { | ||
const controllers = Reflect.getMetadata('controllers', UsersModule); | ||
expect(controllers).toEqual([]); | ||
}); | ||
}); |