apuntes:testing
This is an old revision of the document!
Table of Contents
Testing

Test unitarios
const expect = require('chai').expect; const { getDaysFromNow, getDays} = require('../../dateUtils'); beforeAll(() => { jest.useFakeTimers(); jest.setSystemTime(new Date('2025-02-13')); }); afterAll(() => { jest.useRealTimers(); }); describe('dateUtils', () => { // TODO Añadir casos de prueba para la función getYearsFromNow it('getDaysFromNow', () => { let days = getDaysFromNow(new Date('2025-02-12')); expect(days).equal(1); days = getDaysFromNow(new Date('2025-01-13')); expect(days).equal(31); }); it('getDays', () => { let days = getDays(new Date('2025-01-12'), new Date('2025-01-15')); expect(days).equal(3); days = getDays(new Date('2025-01-01'), new Date('2024-01-01')); expect(days).equal(366); days = getDays(new Date('2024-01-01'), new Date('2024-01-01')); expect(days).equal(0); }); });
. . . "scripts": { "unit-test": "jest src/test/unit", . . . . . .
santi@zenbook:$ npm install -g jest
Test de integración
- Caso de uso: Ok (200 OK)
const chai = require('chai'); const expect = chai.expect; const chaiHttp = require('chai-http'); const app = require('../../app').app; chai.use(chaiHttp); chai.should(); describe('cities', () => { describe('GET /cities', () => { it('should get all cities', (done) => { chai.request(app) .get('/cities') .end((error, response) => { response.should.have.status(200); response.body.should.be.a('array'); expect(response.body[0]).to.have.property('name'); expect(response.body[0]).to.have.property('altitude'); expect(response.body[0]).to.have.property('population'); expect(response.body[0]).to.have.property('foundationDate'); expect(response.body[0]).to.have.property('age'); expect(response.body[0]).to.have.property('area'); expect(response.body[0]).to.have.property('density'); expect(response.body[0].name).to.equal('Zaragoza'); expect(response.body[1].name).to.equal('Madrid'); done(); }); }); }); });
- Caso de uso: Registro (201 Created)
it('should register a new city', (done) => { chai.request(app) .post('/cities') .send({ name: 'cityName', population: 100, altitude: 200, foundationDate: '2005-10-10', area: 1000 }) .end((error, response) => { response.should.have.status(201); expect(response.body).to.have.property('id'); expect(response.body).to.have.property('name'); expect(response.body).to.have.property('population'); expect(response.body).to.have.property('altitude'); expect(response.body).to.have.property('foundationDate'); expect(response.body).to.have.property('age'); expect(response.body).to.have.property('area'); expect(response.body).to.have.property('density'); done(); }); });
- Caso de uso: Validación (400 Bad Request)
it('validation should fail because name is mandatory', (done) => { chai.request(app) .post('/cities') .send({ population: 100, altitude: 200, foundationDate: '2005-10-10', area: 1000 }) .end((error, response) => { response.should.have.status(400); expect(response.body.status).to.equal('bad-request'); expect(response.body.message).to.equal('name field is mandatory'); done(); }); });
© 2024 Santiago Faci
apuntes/testing.1742751535.txt.gz · Last modified: 2025/03/23 17:38 by Santiago Faci