Skip to content

Commit b585918

Browse files
test: validar comportamento em cenário de exceção (500)
com a implementação de stub foi possível a validação de cenário de exceção e a retirada de exceção de code coverage. closes #182
1 parent e95668d commit b585918

File tree

4 files changed

+141
-6
lines changed

4 files changed

+141
-6
lines changed

package-lock.json

Lines changed: 119 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
"nodemon": "^2.0.3",
8888
"nyc": "^15.1.0",
8989
"semantic-release-docker": "^2.2.0",
90+
"sinon": "^9.2.1",
9091
"standard": "^14.3.3",
9192
"supertest": "^5.0.0"
9293
},
@@ -100,6 +101,7 @@
100101
},
101102
"standard": {
102103
"globals": [
104+
"afterEach",
103105
"beforeEach",
104106
"describe",
105107
"it",

src/middlewares/error-handler.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,10 @@ const montarMensagemDeErroDeSchema = require('../utils/montarMensagemDeErroDeSch
33

44
function errorHandler (error, _req, res, _next) {
55
const erroDeSchema = error.name === 'ValidationError'
6-
/* istanbul ignore else */
76
if (erroDeSchema) {
87
return res.status(400).json(montarMensagemDeErroDeSchema(error))
9-
} else if (error instanceof SyntaxError && error.status === 400) {
10-
return res.sendStatus(400)
11-
} else {
12-
console.error(error)
13-
return res.status(500).json({ message: INTERNAL_ERROR, error })
148
}
9+
return res.status(500).json({ message: INTERNAL_ERROR, error })
1510
}
1611

1712
module.exports = errorHandler

test/outros/error-handler.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const chai = require('chai')
2+
const sandbox = require('sinon').createSandbox()
3+
4+
const carrinhosService = require('../../src/services/carrinhos-service.js')
5+
6+
describe('Error handler', () => {
7+
afterEach(() => sandbox.restore())
8+
9+
it('Deve informar para abrir issue ao ocorrer erro 500', async () => {
10+
sandbox.stub(carrinhosService, 'getAll').throws('Teste de erro 500')
11+
12+
const { body } = await request.get('/carrinhos').expect(500)
13+
14+
chai.assert.deepEqual(body, {
15+
message: 'Abra uma issue informando essa resposta. https://github.com/PauloGoncalvesBH/ServeRest/issues',
16+
error: { name: 'Teste de erro 500' }
17+
})
18+
})
19+
})

0 commit comments

Comments
 (0)