Testing with NodeJS
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
export function sum(a: number,b: number): number{ | |
return a + b; | |
} |
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
import { sum } from './myFunction.ts'; | |
describe('Testing sum function', () => { | |
it('Should add two numbers correctly', () => { | |
// Arrange | |
const a: number = 4; | |
const b: number = 1; | |
//Act | |
const result = sum(a, b); | |
//Assert | |
expect(result).toBe(5); | |
}) | |
}) |
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
import * as request from 'supertest'; | |
import app from '../server.ts'; | |
describe('User', () => { | |
describe('Create user endpoint', () => { | |
it('Should return 201 HTTP status code and new user id', async () => { | |
const response = await request(app) | |
.post('/api/user') | |
.send({ | |
email: 'test@email.com', | |
password: 'supersecurepassword' | |
}) | |
expect(response.statusCode).toEqual(201); | |
expect(response.body).toEqual('new-user-id') | |
}) | |
it('Should return an error message if parameters are missing', async () => { | |
const response = await request(app) | |
.post('/api/user') | |
.send({}); | |
expect(response.statusCode).toEqual(422); | |
expect(response.body).toEqual('You must enter a valid email and password.') | |
}) | |
}) | |
}) |
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
import { DatabaseUtils } from '../utils/database.ts'; | |
import { CreateUserDto } from '../dtos/user.ts'; | |
export default class UserService{ | |
databaseUtils: DatabaseUtils; | |
constructor(){ | |
this.databaseUtils = new DatabaseUtils(); | |
} | |
public async createUser( | |
body: CreateUserDto | |
): Promise<string>{ | |
if(!body.email || !body.password){ | |
throw new Error('You must enter your email and password'); | |
} | |
const newUserId = await this.databaseUtils.createUser(body); | |
return newUserId; | |
} | |
} |
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
describe('User Service class', () => { | |
const userService = new UserService(); | |
describe('createUser method', () => { | |
it('Should throw an error if email and password are not sent', async () => { | |
const bodySent = {}; | |
await expect(userService.createUser(bodySent)).rejects.toThrowError( | |
new Error('You must enter your email and passowrd') | |
); | |
}) | |
it('Should return a string if user is created', async () => { | |
//Arrange | |
const body: CreateUserDto = { | |
email = 'test@email.com', | |
password = 'supersecurepassword' | |
}; | |
jest | |
.spyOn(userService.databaseUtils, 'createUser') | |
.mockResolvedValueOnce('created-user-id'); | |
//Act | |
const serviceResponse = await userService.createUser(body); | |
//Assert | |
expect(serviceResponse).toEqual('created-user-id'); | |
}) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment