-
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.
fix Unexpected empty object pattern.
- Loading branch information
Showing
6 changed files
with
105 additions
and
17 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,75 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { UserController } from '../user.controller'; | ||
import { UserService } from '../user.service'; | ||
import { getModelToken } from '@nestjs/mongoose'; | ||
import { User, UserDocument } from '../user.schema'; | ||
import { UserDto } from '../user.dto'; | ||
import { UserModule } from '../user.module'; | ||
import { omit } from 'lodash'; | ||
|
||
// Describe la suite de pruebas para el controlador de usuario. | ||
describe('UserController', () => { | ||
let userController: UserController; | ||
let userService: UserService; | ||
|
||
const mockUser: UserDocument = { username: 'newuser', email: '[email protected]', passwd: '1234', nickname: 'nickname' } as UserDocument; | ||
let mockUsers = [ | ||
{ username: 'user1', email: '[email protected]' }, | ||
{ username: 'user2', email: '[email protected]' }, | ||
]; | ||
|
||
// Configura el controlador y sus dependencias antes de cada prueba. | ||
beforeAll(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
UserController, | ||
UserService, | ||
{ | ||
// Simula el modelo de mongoose para User durante las pruebas. | ||
provide: getModelToken(User.name), | ||
useValue: { | ||
find: jest.fn(), // función simulada que imita "find" de mongoose | ||
findOne: jest.fn(), // función simulada que imita "findOne" de mongoose | ||
save: jest.fn(), // función simulada que imita "save" de mongoose, | ||
exec: jest.fn(), // función simulada que imita "save" de mongoose | ||
}, | ||
}, | ||
], | ||
}).compile(); // Compila el módulo de prueba y devuelve la instancia de TestingModule. | ||
|
||
userController = module.get<UserController>(UserController); | ||
userService = module.get<UserService>(UserService); | ||
|
||
}); | ||
|
||
// Prueba básica para verificar si el controlador está definido. | ||
it('should be defined', () => { | ||
expect(userController).toBeDefined(); | ||
}); | ||
|
||
// Prueba para verificar el si el tipo de los usuarios listados es correcto | ||
describe('getUsers', () => { | ||
it('should return an Array of type User', async () => { | ||
jest.spyOn(userService, 'readUsers').mockImplementation(() => Promise.resolve(mockUsers as unknown as User[])); | ||
const result = await userController.getUsers(); | ||
expect(result).toEqual(mockUsers); | ||
}) | ||
}) | ||
|
||
describe('createUser', () => { | ||
it('should create a new user', async () => { | ||
jest.spyOn(userService, 'createUser').mockResolvedValueOnce(mockUser); | ||
const result = await userController.createUser(mockUser as UserDto); | ||
expect(result).toEqual(mockUser); | ||
}); | ||
}); | ||
|
||
describe('getUserByUsername', () => { | ||
it('should return a user by username', async () => { | ||
jest.spyOn(userService, 'readUserByUsername').mockResolvedValueOnce(mockUser); | ||
const result = await userController.getUserByUsername('newuser'); | ||
const keysToDelete = ['passwd']; | ||
expect(result).toEqual(omit(mockUser, keysToDelete)); | ||
}); | ||
}); | ||
}); |
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,10 @@ | ||
{ | ||
"overrides": [ | ||
{ | ||
"files": ["*.spec.cy.ts"], | ||
"rules": { | ||
"indent": "off" | ||
} | ||
} | ||
] | ||
} |
Empty file.
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,4 @@ | ||
const testUser = { | ||
email: '[email protected]', | ||
password: 'testpassword', | ||
}; |
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